Skip to main content

Python toolkit providing an OpenQASM 3 semantic analyzer and utilities for program analysis and compilation.

Project description

PyQASM Logo (Light Mode) PyQASM Logo (Dark Mode)

CI codecov GitHub Pages PyPI version PyPI Downloads Python verions License

Python toolkit providing an OpenQASM 3 semantic analyzer and utilities for program analysis and compilation.

Motivation

OpenQASM is a powerful language for expressing hybrid quantum-classical programs, but it lacks a comprehensive tool supporting the full capabilities of the language. PyQASM aims to fill this gap by building upon the openqasm3.parser, and providing support for semantic analysis and utilities for program compilation.

Installation

PyQASM requires Python 3.10 or greater, and can be installed with pip as follows:

pip install pyqasm

Optional Dependencies

PyQASM offers optional extras such as -

  1. cli : command-line interface (CLI) functionality
  2. visualization: for program visualization
  3. pulse : pulse/calibration features (in progress)

To install these features along with the core package, you can use the following command:

pip install 'pyqasm[cli,pulse,visualization]'

You can also install them individually. For example, to install the CLI features only, you can run:

pip install 'pyqasm[cli]'      

Install from source

You can also install from source by cloning this repository and running a pip install command in the root directory of the repository:

git clone https://github.com/qBraid/pyqasm.git
cd pyqasm
pip install .

Check version

You can view the version of pyqasm you have installed within a Python shell as follows:

>>> import pyqasm
>>> pyqasm.__version__

Key Features: Unroll & Validate OpenQASM 3 Programs

PyQASM offers robust support for the extensive grammar of OpenQASM 3, including custom gates, subroutines, loops, conditionals, classical control, and more. Its core utilities allow you to validate programs for semantic correctness and unroll (flatten) all high-level constructs into a linear sequence of basic quantum operations, ready for execution or further compilation.

Highlights

  • Comprehensive Grammar Support: PyQASM supports the full breadth of OpenQASM 3 operations, and backward compatibility with OpenQASM 2, including:

    • Classical and quantum registers
    • Loops (for, while)
    • Conditionals (if, else)
    • Parameterized and custom gates
    • Subroutine inlining
    • Switch statements
    • Classical expressions and assignments
    • Variables and arrays
    • Type casting and conversions
    • Built-in and user-defined constants
    • Quantum-classical interaction (measurement, reset, etc.)
    • Inclusion of standard libraries (stdgates.inc, etc.)
  • Unrolling:
    Expands all custom gates, loops, subroutines, branches, etc. into a flat, hardware-ready sequence of instructions.

  • Validation:
    Performs semantic analysis to ensure programs are correct and conform to the OpenQASM 3 specification.


Example: Unroll and Validate a Complex QASM Program

Below is an example demonstrating PyQASM's efficacy on a non-trivial OpenQASM 3 program. This program uses custom gates, loops, and classical control, and is fully unrolled and validated by PyQASM:

from pyqasm import loads, dumps

qasm = """
OPENQASM 3;
include "stdgates.inc";

gate h2 a, b { h a; h b; }
def parity_flip(qubit[4] q) {
    for int i in [0:3] {
        if (i % 2 == 0) {
            x q[i];
        }
    }
}

qubit[4] q;
bit[4] c;

h2 q[0], q[1];
parity_flip(q);

for int i in [0:3] {
    measure q[i] -> c[i];
}
"""

module = loads(qasm)      # Parse and build the program
module.validate()         # Check for semantic errors
module.unroll()           # Flatten all gates, loops, and subroutines

print(dumps(module))      # Output the fully unrolled QASM

Output (fully unrolled QASM):

OPENQASM 3.0;
include "stdgates.inc";
qubit[4] q;
bit[4] c;
h q[0];
h q[1];
x q[0];
x q[2];
c[0] = measure q[0];
c[1] = measure q[1];
c[2] = measure q[2];
c[3] = measure q[3];

PyQASM ensures your OpenQASM programs are semantically correct and hardware-ready, supporting the language's most advanced features with ease.

Resources

Contributing

GitHub QCSE Discord

License

Apache-2.0 License

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

pyqasm-1.0.2.tar.gz (108.9 kB view details)

Uploaded Source

Built Distributions

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

pyqasm-1.0.2-cp314-cp314-win_amd64.whl (210.4 kB view details)

Uploaded CPython 3.14Windows x86-64

pyqasm-1.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (692.2 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

pyqasm-1.0.2-cp314-cp314-macosx_11_0_arm64.whl (210.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pyqasm-1.0.2-cp314-cp314-macosx_10_15_x86_64.whl (221.4 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

pyqasm-1.0.2-cp313-cp313-win_amd64.whl (208.4 kB view details)

Uploaded CPython 3.13Windows x86-64

pyqasm-1.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (696.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyqasm-1.0.2-cp313-cp313-macosx_11_0_arm64.whl (210.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyqasm-1.0.2-cp313-cp313-macosx_10_13_x86_64.whl (220.9 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

pyqasm-1.0.2-cp312-cp312-win_amd64.whl (208.5 kB view details)

Uploaded CPython 3.12Windows x86-64

pyqasm-1.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (696.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyqasm-1.0.2-cp312-cp312-macosx_11_0_arm64.whl (211.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyqasm-1.0.2-cp312-cp312-macosx_10_13_x86_64.whl (221.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

pyqasm-1.0.2-cp311-cp311-win_amd64.whl (208.3 kB view details)

Uploaded CPython 3.11Windows x86-64

pyqasm-1.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (708.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyqasm-1.0.2-cp311-cp311-macosx_11_0_arm64.whl (211.2 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyqasm-1.0.2-cp311-cp311-macosx_10_9_x86_64.whl (221.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

pyqasm-1.0.2-cp310-cp310-win_amd64.whl (208.2 kB view details)

Uploaded CPython 3.10Windows x86-64

pyqasm-1.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (671.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyqasm-1.0.2-cp310-cp310-macosx_11_0_arm64.whl (211.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pyqasm-1.0.2-cp310-cp310-macosx_10_9_x86_64.whl (221.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file pyqasm-1.0.2.tar.gz.

File metadata

  • Download URL: pyqasm-1.0.2.tar.gz
  • Upload date:
  • Size: 108.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8a400369eb01751c0dd9c0c6849ae842b176b6128dc564b0ed26bffa5edffdd6
MD5 9515a33bce06464047d779907910a6fe
BLAKE2b-256 7397d1117ddf856f61377294497ffd3773fdee81c93dbecf8adc7c16e15ef6df

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: pyqasm-1.0.2-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 210.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 43efdea96eb906b4c33d8f73157b2eec3452d152d9262c270d9a867e314eed14
MD5 f953ac50ec8b96ad2f2f7b466f466c9e
BLAKE2b-256 cfb35e246be08eb1846a7cdc5cbf40cd5cb3763dee4edd9eafa53d0d3ed474d4

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 cc0505c64a5aadc5c52ea95344d0889c72c730c216105c60d625cd8888ad4eee
MD5 ee222b1a9f1b0895c48d4f6a0ae4d395
BLAKE2b-256 5764180fcd0512b9d5696eb6fed6337df69584f09ad8c952d93981022409a894

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d44035d1b227c6e9b08c850cc80c9c95eb1739f8a0e844211b44ca2e1a3379cf
MD5 9818924e7ac5b3635332098cf45050a3
BLAKE2b-256 bc8c01ff2c1f257b8f65aae870cbad3514a6e910a2b9d022bd168f9d675bde2c

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9ca2126b35cb068ca61a0a18819dd7a9b5becde2ac63e4745ae6789500789395
MD5 01ff869f78c1efffb60ccadf6f56c768
BLAKE2b-256 f661ba19a99cfc5a57f4ceec4ad771cb52bcf2b5f04a11d87ec62d2debb80d64

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: pyqasm-1.0.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 208.4 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 34aa53d4f9cb5eb5a06dbe06317bf687ad36280dde856853313748ae99d66a1d
MD5 d2c25c6286b74846509c90d5a449f680
BLAKE2b-256 b946a3ee519eeec883dc25f40f23c4013d7465d8524427c0bc3640b2a01d7030

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 98dc7e8f62b715e16370285904f2323e71a80c48a43910cd392738c7208dcd93
MD5 785aade6d482f2a4f3c8c4cc5bc82f9e
BLAKE2b-256 d5a5b57cd6002648ab91c8ec7eedd4a56bdcd222e6e6ea7feee7edcf4726d096

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e3f1bf9a9d2d0eb0a761c1ca03df70ae4cbae0c09b9b97fed455368e0dede8f3
MD5 65ff0d34a3d73bb2a271eab5e701a1b3
BLAKE2b-256 cc7d5a427d8fbccf886f1aba393793f6a8d37a11b3f7bc607984302691d24461

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4a08f90cfeb0883907f468ca265668e022d2d2c53cb9e4d8ce06288f0be033a4
MD5 de3c4eacf0fc8e8a5b4af9f887235d31
BLAKE2b-256 42a0dcfb5a7dbc75b1cc6f2b853ff0a97acb77311ea9dec7b54123c7c67073f9

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pyqasm-1.0.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 208.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 00c26a747f814381763d3216fb4026ac64971ac1641e4c4ef032f7184c16dcd1
MD5 1dd289ea2a94bc7c80adaaa59d45e9e1
BLAKE2b-256 f8b77dec3f3ead4f62f0330edaf5acf65aca9f319a0f154ce852139973c0e3fa

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4c1831a474308b178e203dcb9fff86971057b1029d4570554dc61a3ce6812384
MD5 361e65946ea7efdca64b56ad138a7a50
BLAKE2b-256 db3c3f3eccb2f2732c08f07eafd24fb3e5c9f1141afa0041076c64387aeef549

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0030bd324dd648d619fc216152cc98d1f978618874f7581318accb8f904a946f
MD5 26b2073c2156c69d397b6119dfaeb7e6
BLAKE2b-256 c2265e690ec4e73a862342ef46a783967549b727be31a505dd56fccf0830a8b5

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fcd46e5063b67612b03cdba1aa1c6c1b52239692c39beeab5a29e9ce9e2dfa72
MD5 69197ec00cae1111210ac2390a721eba
BLAKE2b-256 8cb03456a299b98c4bb3be43b6b0170f70f429bfea0c87de70f4e0eb46cdaf0c

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pyqasm-1.0.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 208.3 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6bd92df392711d269e9da48d093269db9e24ddf3f8dfec599f841f89f6a705df
MD5 8712e2e5c16bad382aafe5e834a65595
BLAKE2b-256 a7942c2e6daa5721176ab2a8fdf13c86e3e374b7997fabe19375b360b99021c6

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 04640fcc70e956e5a92c2faf34ef345ac3922ff8bcf39b1905ef3d6387ae9cca
MD5 20115ef48fdddb0529de127584132afa
BLAKE2b-256 3cdc9c4d9bff2895a188e30f2316ed4917936a419286b2f3bd9f0a7b605a1113

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 695b233eacba85a2da97e91b5e0b0843d8d7b595b639e0102bc7012cad023dd5
MD5 7b552832f7f179fed2efca2da2836ca1
BLAKE2b-256 ce18ce445d8f406f667bbdb8aad1971ce906c885048e34fa2932081861f44a9d

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f39109628890a5df05f86a4fa083f046e5bcf7ceef93eee1cf29cc870cab7b5c
MD5 82854a8ae31bd999f603a7481554cfe3
BLAKE2b-256 9e423378defbdab39541bdef50ab42ec5752cbdd2de80ee771c8da0b3fbd7eb0

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pyqasm-1.0.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 208.2 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqasm-1.0.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f46bcc3adac4e2364a14ca44c5b051a3a61ebad17e97b33d6caa0bc078d0d805
MD5 6673b6b37e78fe6a13c5e56fa15b019c
BLAKE2b-256 a55c8248107d8eac87e60f976c5effbe8077115c212fe0c19a4831959e5c5893

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 eb46be009c89fdbed5f8ca04eafa04e8236aff8e8499882787457fb34542a490
MD5 61d1414b21aa422b577235663d5b65e3
BLAKE2b-256 8112c3782b2c87f2ba1cc0df2a945718f2d5d6e34e5d470b329ef3588acaa8c3

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db602cdd15c582cf3baf925bea8435994f5a480e96746fa12fa7bc8ef505bce3
MD5 1d1de7000f9c2818bfdbbaa0fe76263e
BLAKE2b-256 d2ac6257fb688fcf53ff203672d7c3f1b4563614ea1ba49d5fb34257360fd57b

See more details on using hashes here.

File details

Details for the file pyqasm-1.0.2-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyqasm-1.0.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a49a54df89dc1a95edd7363412f8f00c7aca104a98374d5c1c935ce0c09a149e
MD5 7c21067a59fc976fe96a039168429c7e
BLAKE2b-256 ee9a1de69e727120c5d0c8602c275d367781ea3e13deccc742309324e4f9237c

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