OpenQASM Compiler

Translating quantum circuits

myQLM provides a script to translate a QASM file into a myQLM circuit file. For instance, the following bash command generate a file bell.circ containing a myQLM circuit from a text file bell.qasm

# Bash command
oqasm2circ bell.qasm bell.cirq

The Python code corresponding to this translate uses the class OqasmParser

from qat.interop.openqasm import OqasmParser

# Define a bell pair QASM circuit
bell_data = """
OPENQASM 2.0;

// Allocate qubits and cbits
qreg q[2];
creg c[2];

// Define the circuit
h q[0];
cx q[0],q[1];

// Measure
measure q[0] -> c[0];
measure q[1] -> c[1];
"""

# Translate into a myQLM circuit
parser = OqasmParser()
circuit = parser.compile(bell_data)
print("The circuit is composed of gates",
      list(circuit.iterate_simple()))
The circuit is composed of gates [('H', [], [0]), ('CNOT', [], [0, 1]), ('MEASURE', [0], [0]), ('MEASURE', [1], [1])]

Note

The 2.0 version of OPENQASM is the only one fully supported, other versions might work flawlessly, but they haven’t been tested.

Source code documentation

class qat.interop.openqasm.OqasmParser

OPENQASM Parser.

compile(string, write_tables=False, debug=False, tabmodule='oqasm_tab', **kwargs)

Compiles a chunk of openqasm code sent as a parameter, and returns the corresponding QLM circuit

Parameters
  • string – input openqasm code to parse

  • debug – whether to activate debug output or not

  • write_tables – generate parser table file or not (default False)

  • tabmodule – parser tab to use (default oqasm_tab.py)

Returns

Corresponding QLM circuit