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.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.3.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qasm2python-0.3.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for qasm2python-0.3.1.tar.gz
Algorithm Hash digest
SHA256 9441204f0efc989d65aeec6c854d4351e1e04ccb62ec4ea85059020e663471a3
MD5 2906877ce60bf54ea7bca230c40ba46d
BLAKE2b-256 f0bfb7257243da8d2b95e7a0b1b6c7ba9f13f948aeb0f8989bdb8f98c85f2797

See more details on using hashes here.

Provenance

The following attestation bundles were made for qasm2python-0.3.1.tar.gz:

Publisher: publish.yml on MuyleangIng/qasm2python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: qasm2python-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for qasm2python-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a266140aedde0d858122466384fa136b7609bdec3ed641c599db5cb5a561ee47
MD5 43061e8fe6c97cd0a6ceb276ca6c117f
BLAKE2b-256 9d8e66e6bd451fcd3b952578763d90a8c6b8d3c73dd9e30d8dbd14a8fe1815ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for qasm2python-0.3.1-py3-none-any.whl:

Publisher: publish.yml on MuyleangIng/qasm2python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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