forked from QoroQuantum/maestro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_simulation.cpp
More file actions
76 lines (64 loc) · 2.3 KB
/
advanced_simulation.cpp
File metadata and controls
76 lines (64 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @file advanced_simulation.cpp
* @brief Advanced usage example of the Maestro library using the C interface
* for manual control.
*
* This example demonstrates:
* 1. Creating a specific simulator type (Statevector).
* 2. Manually applying gates (H, CX).
* 3. Performing a measurement without collapsing the state (if supported) or
* standard measurement.
* 4. Cleaning up resources.
*/
#include "maestrolib/Interface.h"
#include <iostream>
#include <vector>
int main() {
// 1. Initialize Maestro
void *maestro = GetMaestroObject();
if (!maestro) {
std::cerr << "Failed to initialize Maestro." << std::endl;
return 1;
}
// 2. Create a Simulator
// Create a Statevector simulator.
// Simulators::SimulatorType::Statevector is typically 0 (check
// Simulators/Simulator.h or documentation).
// Simulators::SimulationType::QiskitAer is typically 0.
// We use 0, 0 here for demonstration.
unsigned long int simHandle = CreateSimulator(0, 0);
if (simHandle == 0) {
std::cerr << "Failed to create simulator." << std::endl;
return 1;
}
void *sim = GetSimulator(simHandle);
if (!sim) {
std::cerr << "Failed to get simulator instance." << std::endl;
DestroySimulator(simHandle);
return 1;
}
std::cout << "Simulator created with handle: " << simHandle << std::endl;
// 3. Apply Gates Manually
// We will create a Bell state: |00> -> H(0) -> |+0> -> CX(0,1) -> (|00> +
// |11>) / sqrt(2)
// Apply Hadamard on qubit 0
ApplyH(sim, 0);
std::cout << "Applied H on qubit 0" << std::endl;
// Apply CNOT with control 0 and target 1
ApplyCX(sim, 0, 1);
std::cout << "Applied CX on 0 -> 1" << std::endl;
// 4. Measure
// MeasureNoCollapse returns the outcome without collapsing the state vector
// (useful for debugging/inspection) Note: This might not be supported by all
// backends.
unsigned long long int outcome = MeasureNoCollapse(sim);
std::cout << "Measurement outcome (no collapse): " << outcome << std::endl;
// Standard Measure (collapses state)
// We need to specify which qubits to measure.
unsigned long int qubits[] = {0, 1};
unsigned long long int measurement = Measure(sim, qubits, 2);
std::cout << "Measurement outcome (collapsed): " << measurement << std::endl;
// 5. Cleanup
DestroySimulator(simHandle);
return 0;
}