#!/usr/bin/env python # coding: utf-8 # # Openqasm Tutorials # # 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: # # In[ ]: get_ipython().system('oqasm2circ --help') # In[ ]: get_ipython().run_cell_magic('writefile', 'inverseqft1.qasm', '// QFT and measure, version 1\nOPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[4];\ncreg c[4];\nh q;\nbarrier q;\nh q[0];\nmeasure q[0] -> c[0];\nif(c==1) u1(pi/2) q[1];\nh q[1];\nmeasure q[1] -> c[1];\nif(c==1) u1(pi/4) q[2];\nif(c==2) u1(pi/2) q[2];\nif(c==3) u1(pi/2+pi/4) q[2];\nh q[2];\nmeasure q[2] -> c[2];\nif(c==1) u1(pi/8) q[3];\nif(c==2) u1(pi/4) q[3];\nif(c==3) u1(pi/4+pi/8) q[3];\nif(c==4) u1(pi/2) q[3];\nif(c==5) u1(pi/2+pi/8) q[3];\nif(c==6) u1(pi/2+pi/4) q[3];\nif(c==7) u1(pi/2+pi/4+pi/8) q[3];\nh q[3];\nmeasure q[3] -> c[3];\n') # In[ ]: get_ipython().system('oqasm2circ inverseqft1.qasm inverseqft1.circ') # ## Recovering circuits directly # # 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. # # # In[ ]: 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))