forked from QoroQuantum/maestro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_example_1.py
More file actions
55 lines (42 loc) · 1.32 KB
/
python_example_1.py
File metadata and controls
55 lines (42 loc) · 1.32 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
import maestro
def main():
print("Maestro Python Bindings Example")
# Create the Maestro instance
m = maestro.Maestro()
# Create a simulator
sim_handler = m.CreateSimulator(
maestro.SimulatorType.QCSim, maestro.SimulationType.Statevector
)
simulator = m.GetSimulator(sim_handler)
circ = (
"OPENQASM 2.0;\n"
+ 'include "qelib1.inc";\n'
+ "qreg q[2];\n"
+ "creg c[2];\n"
+ "h q[0];\n"
+ "cx q[0], q[1];\n"
+ "measure q -> c;\n"
)
circuit_parser = maestro.QasmToCirc()
circuit = circuit_parser.ParseAndTranslate(circ)
if simulator:
print("Simulator object obtained successfully")
# Allocate qubits
num_qubits = circuit.GetMaxQubitIndex() + 1
simulator.AllocateQubits(num_qubits)
simulator.Initialize()
print(f"Allocated {num_qubits} qubits")
# Apply gates (Bell State)
simulator.ApplyH(0)
simulator.ApplyCX(0, 1)
print("Applied H(0) and CX(0, 1)")
# Measure
results = simulator.SampleCounts([0, 1], 1000)
print(f"Measurement results: {results}")
else:
print("Failed to get simulator object")
# Clean up
m.DestroySimulator(sim_handler)
print("Simulator destroyed")
if __name__ == "__main__":
main()