2.7. Conversion to OpenQASM3#
For using many API’s, it is helpful to convert geqo code to the OpenQASM3 format. geqo supports the conversion of a Sequence
to OpenQASM3 code with the method sequence_to_qasm3
of SymPy and NumPy based simulators. The function is part of the simulators, because generating OpenQASM3 code needs the corresponding values of parameters of gates like angles or unitary matrices. These values have to be set by the methods setValue
or prepareBackend
before using these values.
import sympy as sym
from geqo.gates.rotation_gates import Rx
from geqo.gates import PauliX
from geqo.core import Sequence
from geqo.operations import Measure
from geqo.simulators.sympy import ensembleSimulatorSymPy
# Generate 2 unitary gates and one measurement.
gate1 = Rx("φ")
gate2 = PauliX()
meas = Measure(2)
seq = Sequence(
[0, 1], [0, 1], [(gate1, [0]), (gate2, [1]), (meas, [0, 1], [0, 1])]
) # This sequence should be converted to OpenQASM3 code.
sim = ensembleSimulatorSymPy(2, 2) #
sim.setValue(
"φ", sym.sqrt(2)
) # Set the value of the rotation angle with the name φ to a symbolic value.
qasm = sim.sequence_to_qasm3(seq)
print(qasm)
OPENQASM 3.0;
include 'stdgates.inc';
qubit[2] q;
bit[2] c;
rx(sqrt(2)) q[0];
x q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];