Package: qiskit-qasm2
Importer from OpenQASM 2 to QiskitThis repository provides the Python packageqiskit_qasm2, which provides a
fast parser of OpenQASM 2 into Qiskit'sQuantumCircuit.  It is often 10x or
more faster than Qiskit's native parser.  The API is simple:qiskit_qasm2.loadtakes a filename, and returnsQuantumCircuit;qiskit_qasm2.loadstakes an OpenQASM 2 program in a string, and returnsQuantumCircuit.The full documentation is published tohttps://jakelishman.github.io/qiskit-qasm2.A simple parsing example:importqiskit_qasm2program="""OPENQASM 2.0;include "qelib1.inc";qreg q[2];creg c[2];h q[0];cx q[0], q[1];measure q -> c;"""qiskit_qasm2.loads(program).draw()┌───┐     ┌─┐
q_0: ┤ H ├──■──┤M├───
     └───┘┌─┴─┐└╥┘┌─┐
q_1: ─────┤ X ├─╫─┤M├
          └───┘ ║ └╥┘
c: 2/═══════════╩══╩═
                0  1FeaturesThe parser supports all ofthe OpenQASM 2
specification,
including:register definitions and usage (qregandcreg);theqelib1.incas a special builtin include, precisely as described in the
paper;general includes, with an option to specify the search path;customgateandopaquedeclarations;gate, measurement and reset broadcasting;conditioned gate applications, measurements and reset;constant folding with the scientific calculator functions in gate parameter
lists;mathematical expressions on parameters within custom gate bodies.In addition, the parser also includes options to:modify the search path forincludestatements in OpenQASM 2;define overrides for how some named OpenQASM 2 gate applications should be
converted into Qiskit form;define new builtin quantum instructions for OpenQASM 2;define new builtin classical scientific-calculator functions.Qiskit's builtin parser makes some extra-spec additions by default, with no
option to disable them.  This mostly takes the form of custom gate overrides,
and various additional gates in Terra's vendored version ofqelib1.inccompared to the description in the paper.  This parser is more type-safe than
Qiskit's, but does includea compatibility modeto ease the transition from using Qiskit's parser.InstallationInstall the latest release of theqiskit_qasm2package from pip:pipinstallqiskit_qasm2DevelopingIf you're looking to contribute to this project, please first readour contributing guidelines.Set up your development environment by installing the development requirements
with pip:pipinstall-rrequirements-dev.txttoxThis installs a few more packages than the dependencies of the package at
runtime, because there are some tools we use for testing also included, such astoxandpytest.You will also need a working Rust toolchain.  The easiest way to install one isby using rustupon Linux, macOS or Windows.After the development requirements are installed, you can install an editable
version of the package withpipinstall-e.After this, any changes you make to the library code will immediately be present
when you open a new Python interpreter session.This package was mostly an excuse for me to learn a bit more about how lexers
are written at a low level.  This is why the Rust crate doesn't use any
lexer-generation libraries.  You can read a bit more about the architecture and
some of the design decisions inthe developer section of the
documentation.Building documentationAfter the development requirements have been installed, the commandtox-edocswill build the HTML documentation, and place it indocs/_build/html.  The
documentation state of themainbranch of this repository is published tohttps://jakelishman.github.io/qiskit-qasm2.Code style and lintingThe Python components of this repository are formatted usingblack, and the
Rust components withrustfmt.  You can run these on the required files by
runningtox-estyleThe full lint suite can be run withtox-elintLicenseThis project is licensed underversion 2.0 of the Apache License.