Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qasm2python-0.2.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qasm2python-0.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file qasm2python-0.2.1.tar.gz.

File metadata

  • Download URL: qasm2python-0.2.1.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

Hashes for qasm2python-0.2.1.tar.gz
Algorithm Hash digest
SHA256 16b72b643ba3ed89a0e12d7de17964ece3e4f8f7c13be2d8287a158a7ea51bf9
MD5 f4d75f51dd35f749bc33020ca9f8d019
BLAKE2b-256 a6f154a0f746d531588bfc13f65d144f2bbcde80212d0844999f53c4440c0b7c

See more details on using hashes here.

File details

Details for the file qasm2python-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: qasm2python-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for qasm2python-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a8e17ed094fa65edaccc9fe431d451e5a5bfb831ce64f2518a5971f2b9d0aa7
MD5 ee8310d15a18fa9fb6c9287007661d51
BLAKE2b-256 f5977329b690d531173d95e02e263177a928640faa7a815c0e7a34abc8ab192c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page