Convert OpenQASM 3 to Python Qiskit code dynamically.
Project description
qasm2python
Convert OpenQASM 2.0 / 3.0 to executable Python Qiskit code.
Supports:
- OpenQASM 2 and 3
- Standard gates (h, x, cx, ccx, rx, ry, rz, etc.)
- Custom gate definitions
- Automatic modifier sanitization (e.g.
ctrl @,ctrl(2) @) - Dynamic Python code generation
🚀 Installation
pip install qasm2python
Upgrade:
pip install --upgrade qasm2python
🔥 Basic Usage
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, var_name="qc")
print(python_code)
🧠 Advanced Example (Custom Gate + Modifiers)
Even if your QASM includes modifiers like ctrl @, the library will sanitize them automatically.
from qasm2python import convert_qasm_to_python
qasm_custom = """
OPENQASM 3;
include "stdgates.inc";
qubit[5] q;
bit[5] c;
gate kinggate a, b, c, d, e {
h a;
h b;
h a;
ctrl @ cx b, e;
ctrl @ cx a, d;
ctrl(2) @ ccx a, b, c;
x a;
rx(1.5708) a;
}
kinggate q[0],q[1],q[2],q[3],q[4];
c[1] = measure q[1];
c[2] = measure q[2];
"""
python_code = convert_qasm_to_python(qasm_custom, var_name="qc")
print(python_code)
▶ Run Generated Circuit
namespace = {}
exec(python_code, namespace)
qc = namespace["qc"]
from qiskit_aer import AerSimulator
from qiskit import transpile
sim = AerSimulator()
tqc = transpile(qc, sim)
result = sim.run(tqc, shots=1024).result()
print(result.get_counts())
🔧 Local Development
Clean previous builds:
rm -rf dist build *.egg-info
Build:
pip install build
python -m build
Install locally:
pip install dist/qasm2python-0.2.0-py3-none-any.whl
🧩 Features
- QASM2 & QASM3 detection
- Gate conversion to Python Qiskit API
- Custom gate extraction
- Modifier stripping for compatibility
- Ready for Aer simulation
👤 Author
Muyleang Ing
AI Convergence Researcher | Quantum Computing | Software & DevOps Engineer
🌐 https://muyleanging.com
💻 https://github.com/muyleanging/qasm2python
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qasm2python-0.2.2.tar.gz.
File metadata
- Download URL: qasm2python-0.2.2.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c80c69c2643430d887b5eafe837b3bedfdabbcd2b383448ee92aaba94049dddc
|
|
| MD5 |
ac5a9482faaf3a7fd534bcf37ff14c36
|
|
| BLAKE2b-256 |
98b0ac1b15eac2cc8fb7cdaa3fb6d754b0d45fb46a1cf759bef919d4d38b2748
|
File details
Details for the file qasm2python-0.2.2-py3-none-any.whl.
File metadata
- Download URL: qasm2python-0.2.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d621326063ec3b42846f469aa0177b5664a5a0a85abf2af97059a3e5582345eb
|
|
| MD5 |
ecdf64ed7e15d1b9f242bcdbf1f9031b
|
|
| BLAKE2b-256 |
9716bbdedbb7bca16576829458a05cab27b53e76e3d620289e6e5c1354c60a5f
|