qasm2python
Convert OpenQASM 2.0 / 3.0 into executable Python Qiskit code.
Supports:
- OpenQASM 2 and 3
- Standard gates (h, x, cx, ccx, rx, ry, rz, etc.)
- Custom gate definitions
- Modifier sanitization (
ctrl @,ctrl(2) @) - Optional variable naming
🚀 Installation
pip install qasm2python
Upgrade:
pip install --upgrade qasm2python
🔥 Basic Usage (Default Variable Name)
If you do not specify var_name, it defaults to qc.
from qasm2python import convert_qasm_to_python
qasm = """
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
h q[0];
cx q[0], q[1];
"""
python_code = convert_qasm_to_python(qasm)
print(python_code)
Output:
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 0)
qc.h(0)
qc.cx(0, 1)
🧠 Custom Variable Name (Optional)
You can override the default circuit variable:
python_code = convert_qasm_to_python(qasm, var_name="my_circuit")
print(python_code)
Output:
my_circuit = QuantumCircuit(2, 0)
🧪 Execute Generated Circuit
namespace = {}
exec(python_code, namespace)
qc = namespace["qc"] # or namespace["my_circuit"]
🧠 Advanced Example (Custom Gate + Modifier Sanitization)
Even if QASM includes modifiers like ctrl @, they are automatically sanitized.
qasm_custom = """
OPENQASM 3;
include "stdgates.inc";
qubit[5] q;
bit[5] c;
gate kinggate a, b, c, d, e {
h a;
ctrl @ cx b, e;
ctrl(2) @ ccx a, b, c;
}
kinggate q[0],q[1],q[2],q[3],q[4];
"""
python_code = convert_qasm_to_python(qasm_custom)
print(python_code)
⚙ Function Signature
convert_qasm_to_python(
qasm_source: str,
var_name: str | None = None,
include_imports: bool = True
)
Parameters
| Parameter | Description |
|---|---|
qasm_source |
QASM 2.0 or 3.0 input string |
var_name |
Optional QuantumCircuit variable name (default: "qc") |
include_imports |
Whether to include from qiskit import QuantumCircuit |
👤 Author
Muyleang Ing
AI Convergence Researcher | Quantum Computing | Software & DevOps Engineer
🌐 https://muyleanging.com
💻 https://github.com/muyleanging/qasm2python
Metadata
Release files for qasm2python 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| qasm2python-0.3.1.tar.gz | 5.6 kB | With provenance | Details for qasm2python-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| qasm2python-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for qasm2python-0.3.1-py3-none-any.whl |
Total release size: 11.7 kB
Release files / qasm2python-0.3.1.tar.gz
| Download URL | qasm2python-0.3.1.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9441204f0efc989d65aeec6c854d4351e1e04ccb62ec4ea85059020e663471a3
|
|
BLAKE2b-256 checksum How to use checksums |
f0bfb7257243da8d2b95e7a0b1b6c7ba9f13f948aeb0f8989bdb8f98c85f2797
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 6, 2026.
Transparency logRelease files / qasm2python-0.3.1-py3-none-any.whl
| Download URL | qasm2python-0.3.1-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a266140aedde0d858122466384fa136b7609bdec3ed641c599db5cb5a561ee47
|
|
BLAKE2b-256 checksum How to use checksums |
9d8e66e6bd451fcd3b952578763d90a8c6b8d3c73dd9e30d8dbd14a8fe1815ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 6, 2026.
Transparency logSecurity for qasm2python 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for qasm2python-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.