The `openqasm3` library provides a reference Python implementation of the OpenQASM 3 Abstract Syntax Tree (AST). It enables parsing OpenQASM 3 programs into an AST and offers tools for manipulating this AST, facilitating the development of OpenQASM 3 compiler passes and analysis tools. The library, currently at version 1.0.1, actively supports the OpenQASM 3.1.0 specification, with releases often coinciding with updates to the OpenQASM specification.
pip install openqasm3Verified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse a simple OpenQASM 3 program string into its Abstract Syntax Tree representation using the `openqasm3.parser.parse` function. The resulting `Program` object can then be inspected or traversed for further processing.
Ensure your quantum programs adhere to the OpenQASM 3 specification. Consult the OpenQASM 3 specification for syntax and feature differences.
Install `openqasm3` with the `[parser]` extra (`pip install openqasm3[parser]`) to ensure the correct `antlr4-python3-runtime` version is installed. If encountering issues, check the `antlr4-python3-runtime` version and `openqasm3`'s `ANTLR_VERSIONS.txt` on GitHub for supported ranges.
Migrate from `qreg`/`creg` to `qubit[N]` and `bit[N]` declarations for future compatibility with OpenQASM 3.
Understand that this library processes the OpenQASM 3 language structure. For execution or simulation, integrate with a compatible quantum SDK or platform (e.g., Qiskit, Amazon Braket) that supports OpenQASM 3.
Pin the exact version of `openqasm3` in your `requirements.txt` (`openqasm3==X.Y.Z`) and be prepared for potential API changes when upgrading. Regularly review release notes for breaking changes.