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

Uploaded Python 3

File details

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

File metadata

  • Download URL: qasm2python-0.3.0.dev0.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.dev0.tar.gz
Algorithm Hash digest
SHA256 763b215fa7942aa3812e8025fb6a65c0a6480f1a287ebe6086f82ac38163033c
MD5 e71b691f777092ac9f0057e61161c60d
BLAKE2b-256 d84c8dc6e213035718f9c08437473c28fe2652e457c3c445c9284ae83e8c402a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for qasm2python-0.3.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2c4345c898c271857773def0dd4f495627ba1c27c3fa31611e7ec623da8256a
MD5 ba1a364b42a9ddef327ef76a2182b071
BLAKE2b-256 07c4747d8cb000e7c146cc23866aefff568f61c05921f8cf0166f92baca0f4fd

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