An openqasm compiler called oqasm2circ
that compiles openqasm code into a QLM circuit, then dumps it in a file.
A few examples are available, to compile an openqasm .qasm
file use the following command:
!oqasm2circ --help
%%writefile inverseqft1.qasm
// QFT and measure, version 1
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[4];
h q;
barrier q;
h q[0];
measure q[0] -> c[0];
if(c==1) u1(pi/2) q[1];
h q[1];
measure q[1] -> c[1];
if(c==1) u1(pi/4) q[2];
if(c==2) u1(pi/2) q[2];
if(c==3) u1(pi/2+pi/4) q[2];
h q[2];
measure q[2] -> c[2];
if(c==1) u1(pi/8) q[3];
if(c==2) u1(pi/4) q[3];
if(c==3) u1(pi/4+pi/8) q[3];
if(c==4) u1(pi/2) q[3];
if(c==5) u1(pi/2+pi/8) q[3];
if(c==6) u1(pi/2+pi/4) q[3];
if(c==7) u1(pi/2+pi/4+pi/8) q[3];
h q[3];
measure q[3] -> c[3];
!oqasm2circ inverseqft1.qasm inverseqft1.circ
It is also possible to use the parser's commands to directly parse a string and recover the QLM circuit object in python for further use.
Note: This however doesn't thoroughly process include statements, because those are done with the binary.
from qat.interop.openqasm.qasm_parser import OqasmParser
# We will use the adder openqasm example from github
file = open("inverseqft1.qasm")
data = file.read()
# Building our parser
oq_parser = OqasmParser()
#oq_parser.build()
# Parsing
#oq_parser.parse(data)
qlm_circuit = oq_parser.compile(data)
from qat.core.util import get_syntax
for index, op in enumerate(qlm_circuit.ops):
print("Gate {} with params {} on qubits {} and cbits {}".format(*get_syntax(qlm_circuit, index),
op.qbits, op.cbits))