Qiskit OpenQASM 3 Importer
The `qiskit-qasm3-import` library provides functionality to parse OpenQASM 3 strings into Qiskit circuits, enabling interoperability between QASM 3 programs and the Qiskit ecosystem. It is currently at version 0.6.0 and releases new minor versions periodically, typically in conjunction with Qiskit's major releases or to add support for new QASM 3 features.
Common errors
-
ModuleNotFoundError: No module named 'qiskit_qasm3_import'
cause The `qiskit-qasm3-import` package is not installed in your Python environment.fixInstall the package using pip: `pip install qiskit-qasm3-import` -
AttributeError: module 'qiskit_qasm3_import' has no attribute 'parse'
cause You are trying to call a function named `parse`, which does not exist. The correct function for parsing a QASM 3 string is `loads`.fixChange `qiskit_qasm3_import.parse(...)` to `qiskit_qasm3_import.loads(...)`. -
qiskit.exceptions.QiskitError: 'OpenQASM 3 circuits from this importer require Qiskit Terra >= 0.24.0 and < 1.0.0 for qiskit-qasm3-import < 0.6.0, or Qiskit >= 1.0.0 for qiskit-qasm3-import >= 0.6.0.'
cause There is a version mismatch between your installed `qiskit` and `qiskit-qasm3-import` packages. This error specifically points to the required compatibility.fixIf using Qiskit 1.0.0 or later, ensure `qiskit-qasm3-import` is version 0.6.0 or newer: `pip install --upgrade qiskit-qasm3-import`. If using an older Qiskit Terra, ensure `qiskit-qasm3-import` is compatible (typically <0.6.0).
Warnings
- breaking Version 0.6.0 of `qiskit-qasm3-import` introduced compatibility with Qiskit 1.0.0. If you are using an older version of `qiskit-qasm3-import` (e.g., <0.6.0) with Qiskit 1.0.0+, you may encounter errors related to API changes or an incompatible `QuantumCircuit` object structure.
- gotcha The `qiskit-qasm3-import` library does not currently support all features defined by the OpenQASM 3 specification. Complex or less common QASM 3 constructs might lead to parsing errors or incomplete circuit representations.
- gotcha The `loads` function expects a string as input. Providing a file path or an object that is not a string will result in a `TypeError` or a parsing failure.
Install
-
pip install qiskit-qasm3-import
Imports
- loads
from qiskit_qasm3_import import loads
Quickstart
from qiskit_qasm3_import import loads
qasm3_str = """
OPENQASM 3.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0], q[1];
measure q -> c;
"""
# Parse the QASM 3 string into a Qiskit QuantumCircuit
circuit = loads(qasm3_str)
# Print a summary of the resulting circuit
print(circuit.draw('text', idle_wires=False))