qat.interop.openqasm.OqasmParser
- class qat.interop.openqasm.OqasmParser(gates: Optional[dict] = None, include_matrices: bool = True)
Parser of OpenQASM 2.0 files. This class provides tools to translate a string object (containing OpenQASM 2.0 source code) into a
Circuit
from qat.interop.openqasm import OqasmParser # Define a dummy circuit data = """ OPENQASM 2.0; // Allocate qubits and cbits qreg q[1]; creg c[1]; // Apply gates and measure x q[0]; measure q[0] -> c[0]; """ # Translate into a myQLM circuit parser = OqasmParser() circuit = parser.compile(data) print("The circuit is composed of gates", list(circuit.iterate_simple()))
The circuit is composed of gates [('X', [], [0]), ('MEASURE', [0], [0])]
from qat.lang.AQASM import AbstractGate # Define a circuit using OpenQASM OPENQASM_CODE = """ OPENQASM 2.0; // Allocating registers qreg q[1]; creg c[1]; // Dummy circuit p(pi/4) q[0]; my_custom_gate q[0]; """ # Register gates (i.e. "p" gate is an alias for "PH" gate, "my_custom_gate" is defined by an abstract gate) custom_gate = AbstractGate("custom_gate", [], arity) parser = OqasmParser(gates={"p": "PH", "my_custom_gate": custom_gate}, include_matrices=False) # Compile circuit and display it for gate, angles, qubits in parser.compile(OQASM_CODE).iterate_simple(): if angles: print(f"Apply {gate}{angles} on qubits {qubits}") else: print(f"Apply {gate} on qubits {qubits}")
Traceback (most recent call last): File "<stdin>", line 17, in <module> NameError: name 'arity' is not defined
- Parameters
gates (dict[str, str or
Gate
], optional) – definition of custom gates. These gates are defined using a dictionary, a key corresponding to the OpenQASM gate identifier and the key being: - a str: the name of the equivalent gate in myQLM (e.g. “PH”, “X”, “U”, etc.) - aGate
: a custom gateinclude_matrices (bool, optional) – include matrices in the generated circuit (default: True)
- 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