OpenQASM

Load circuits from OpenQASM 2.0 / 3.0 so they can be fed straight into qtest’s assertions — handy when a circuit arrives as a compiler artifact, an export from another tool, or a checked-in fixture.

from qtest import load_qasm, assert_state_close

program = '''
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
h q[0];
cx q[0], q[1];
'''
qc = load_qasm(program)          # version auto-detected from the header
assert_state_close(qc, "bell")

OpenQASM 2.0 uses Qiskit’s built-in parser. OpenQASM 3.0 additionally requires the qiskit-qasm3-import package — install the optional extra:

pip install 'qtest-quantum[qasm3]'

Public API

load_qasm

Parse an OpenQASM program string into a qiskit.QuantumCircuit.

load_qasm_file

Read an OpenQASM file and parse it into a qiskit.QuantumCircuit.

Reference

Load circuits from OpenQASM so they can be tested directly.

A common workflow is to receive a circuit as an OpenQASM program — emitted by a compiler, exported from another tool, or checked into a fixtures directory — and want to assert something about it. These helpers parse OpenQASM 2.0 / 3.0 into a qiskit.QuantumCircuit that plugs straight into qtest’s assertions:

from qtest import load_qasm, assert_state_close

qc = load_qasm(qasm_program)          # version auto-detected from the header
assert_state_close(qc, "bell")

OpenQASM 2.0 loading uses Qiskit’s built-in parser. OpenQASM 3.0 loading additionally requires the qiskit-qasm3-import package (the optional qtest[qasm3] extra); a clear error is raised if it is missing.

Imports are lazy, so importing qtest.qasm (or qtest) does not require Qiskit.

qtest.qasm.load_qasm(program, version='auto')[source]

Parse an OpenQASM program string into a qiskit.QuantumCircuit.

Parameters:
  • program (str) – The OpenQASM source as a string.

  • version (str) – "auto" (default) detects the version from the OPENQASM header; pass "2" or "3" to force a parser.

Return type:

qiskit.QuantumCircuit

Raises:
  • ValueError – If program is empty, the version cannot be determined, or it is unsupported.

  • ImportError – If OpenQASM 3.0 loading is requested but qiskit-qasm3-import is not installed.

qtest.qasm.load_qasm_file(path, version='auto')[source]

Read an OpenQASM file and parse it into a qiskit.QuantumCircuit.

Parameters:
Return type:

qiskit.QuantumCircuit