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.3.tar.gz (109.0 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.3-cp314-cp314-win_amd64.whl (210.5 kB view details)

Uploaded CPython 3.14Windows x86-64

pyqasm-1.0.3-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.3-cp314-cp314-macosx_11_0_arm64.whl (210.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.15+ x86-64

pyqasm-1.0.3-cp313-cp313-win_amd64.whl (208.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyqasm-1.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (696.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.13+ x86-64

pyqasm-1.0.3-cp312-cp312-win_amd64.whl (208.6 kB view details)

Uploaded CPython 3.12Windows x86-64

pyqasm-1.0.3-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.3-cp312-cp312-macosx_11_0_arm64.whl (211.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyqasm-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl (221.5 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

pyqasm-1.0.3-cp311-cp311-win_amd64.whl (208.4 kB view details)

Uploaded CPython 3.11Windows x86-64

pyqasm-1.0.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (708.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyqasm-1.0.3-cp311-cp311-macosx_11_0_arm64.whl (211.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.9+ x86-64

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

Uploaded CPython 3.10Windows x86-64

pyqasm-1.0.3-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.3-cp310-cp310-macosx_11_0_arm64.whl (211.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pyqasm-1.0.3-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.3.tar.gz.

File metadata

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

File hashes

Hashes for pyqasm-1.0.3.tar.gz
Algorithm Hash digest
SHA256 2e2fe05e73cf2f6e8a3f88a047361bee8c117a9cfe49a413decb82868adbfe75
MD5 538f5ac4db4f568f5c2b5b49c60e94f1
BLAKE2b-256 489be5d8ac2f5c3330d00f758aab5e43c7df4081ab5c44cf274391662cae3d09

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyqasm-1.0.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 85430d000888d0b5a5568ac273780c503bc2877db90613d558b8c14da111a3b1
MD5 4f885f68e80763cb0ada5ea17ede6d0f
BLAKE2b-256 f32fa7be8417ee8e70180f18249d8d3bdba63b6dad79a44c2b1107041b1f5ad4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 559fa0b19f5dc8313d8bbebd8f68e04baa70872c48289cd6080d97485f04d261
MD5 e3077c334824352e13cd81242d9db0d4
BLAKE2b-256 33a767e3d9994314c90d45baa6dc4f1f88dce6b6245e68d1d0b72cc49723bfb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ad531317b8efbbff6335979ac09976658c8f5051545dd4789532e73ed5b425a9
MD5 58c827edec78bc390c60bb47532e023d
BLAKE2b-256 12d76b43af6708e01ee8dcbb2cd702e4130efcac9ce652df10c7e657f93dc382

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 03f6ba8193e4cd665068799a1032fa6558509e3e4468f6d01671be9b3816994e
MD5 5b0188a7943713e0bb77a34838627c3b
BLAKE2b-256 880d13eb33f5e22ff657b06cbabcfd0e49b56d73f22bc2953a1996909ec6e949

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyqasm-1.0.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 355cc32185925ef8416afdc21941fb28aacadf4072ae8a7fad2382af40b3d4d1
MD5 8c77e5933a6e4ae64ae21ed8eacb3fb2
BLAKE2b-256 9ae1861ef6d10aa46605d136caa6e1e2c09affaa7ea0c574e7b18b918fdd612b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 530a30c0e5b8f23306c13c74e7749b9031aaeb66fa367092b6e03e8472fafb33
MD5 fa119d38105b564c7f7d35b1deff1798
BLAKE2b-256 eab79b02fbfa5812af13c65aad3814366b4790bf89bae455e7ce957cd810e28f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eee18c3fbd61c895bc78c8500ab5bc1c833f669a7a96e023794d6d4402bf1647
MD5 e3f5bc94c6099fc9cd3e7fbba2f924b6
BLAKE2b-256 88cd25cee044e7129429e72d88d725ba953a2b34ac0e93e48c28feaa58694b48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d9a11c01b7aa6bea7fd36615cec950f821cb1e53815b744027def51c946cbff6
MD5 2aec27d1659b9754152038f05bff2320
BLAKE2b-256 eec2263338cdc793c6a5fa2822b16171edf4d2a2bdcc880a9f2a835970b0f30e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyqasm-1.0.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a30bac573461ff6b3cfe68eeb7aa895145445f8b03d540c0a2e492da22598b79
MD5 21edae879e912ccf942c5e1fa6123a31
BLAKE2b-256 0a4f739192c1a8c92e1dfa46900f343ccb4b10b1cdaa930ddc2abd4e8f98192f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 49498b10b6a3bc9cd0e342d31e12ce24dee50b5fd31f8a6a227c2d5e5a67d426
MD5 4a97fc036f87b3450c068fea6c109f8c
BLAKE2b-256 4a23a334d1a16aeef39c5271f08431a58643b0307c6ec7663b4baab8ce61b116

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37d5a5599160326aa010f74c749f1ee1eb2387a7b5ea2fec34f7fe2ef175874f
MD5 949e1fae1d6f9f039001a55fcf21b1dc
BLAKE2b-256 7db0155540dd757e2a5dfbf355d938d9dde58b1e735c949bb954a3b9927b95a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 96e99a47e9c761f02fc0c245f9ab6af3f928d768b7e04cfb3461080bb33d9e4e
MD5 052a8ff8b91ce120d62a6ca854c21496
BLAKE2b-256 d74f16bcc0e7d212e8e11ae9549edd185f1df1501f2e3385c2905ecf168ea25a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyqasm-1.0.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c50e029678453f67ed4aa4948769029d8bc2e8cfd522d13e9967cdb5214d3286
MD5 eb869470a01f17eab321241e549b209b
BLAKE2b-256 b778bf5f09d5e5559f5c6d3a688e471c436a87d10c85e461c7daa3e2b843dec8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 d9590fe665e326336a617aef591c3b7b108207aca76b6294ce85d98f0f0f26e1
MD5 01a6c2584968aa7a5b2224b81a6224b8
BLAKE2b-256 d2626825ea2699a3de1c9bc459ad22b4bf2ea741ceead5122196c9de25426541

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e26c488c3bc9db83c2226861dede67084d17a981b3b4195aae5d4bb4e756cb73
MD5 3e383ed1ce6b78dabf7fcdea319a7747
BLAKE2b-256 0b6553e57ab522a53f1e60aaff99e690ba42d3fd44a4b54cb5587298a4ecf012

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6b0f154aa70a89b56b3e4845a2097b0d670c34b0ab7f5d42656f9a85a61aca3a
MD5 c17bfa0a78601d54606658394233fcb9
BLAKE2b-256 90d58b1f53e2cdc24fdf6b45bb7afc4d80be47642637cc786ed7a54896ea7ec1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyqasm-1.0.3-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.12

File hashes

Hashes for pyqasm-1.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 29ffd09a20bb30fc894314110e27a034de4834f9c2e4ad853843e7e8078e8c6e
MD5 fdbfe5f84e3dd5ba0b8c72846791886b
BLAKE2b-256 b88e392ffc319828977cb534745275bcac0ace991645b629c02319f29916f6b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1ce16e437d8a5c87edd699448e3ef2798e92c6c5bd6b110f5836cf65caff7226
MD5 a7d1d89f453bd5d208e0fb6846570471
BLAKE2b-256 895bd6662450671cd866cd38e80385a1f3fc7b32031298eb087ab213886c7bb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9694020372578078df23736d9e163b8c55339e694c95a12be92d55eb52fa285
MD5 68a7b2abf2ec84e1342602ee662b3986
BLAKE2b-256 214d01f3cf530616e71771b0cae28c5b1afba0036687e8db3fb56e457229bf9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyqasm-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 12823211dc95eef11ef84a36d7acd9d454a84597b7dfe80d0bcca1e620967989
MD5 a1c5cec514c60ef8c635773b4c84edd3
BLAKE2b-256 13f466a9616503ecf7df2a3f7c0c3131517b98bc232d4f8ff893c5ea5f0808c4

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