Skip to main content

Convert OpenQASM 3 to Python Qiskit code dynamically.

Project description

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

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.3.0.tar.gz (5.7 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.3.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qasm2python-0.3.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for qasm2python-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5ec91cc277d08c4b3046fced1b435d5172a2747be81dbdc7b9e002bfc2d61b40
MD5 eadeea66ae81802ec4dc169cec23fe16
BLAKE2b-256 9c56ce01483b5022de64625ef48a8346622c85068bdbde9a0d768d1749d1aad1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: qasm2python-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a5c680ef5082a83f7c9e19f89a4bba97fea916a2d63e647bfb5acc6b758dc09
MD5 76552221b9232993609fcda2165e24c4
BLAKE2b-256 4eefef8123635450aeeda3e9419d49aea6615d5b2f910b8c64439d9297cb8821

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