Package: qiskit-qasm3-import
Importer from OpenQASM 3 to QiskitThis repository provides the Python packageqiskit_qasm3_import, which is a
basic and temporary importer from OpenQASM 3 into Qiskit'sQuantumCircuit.Qiskit itself accepts this package as an optional dependency if it is installed.
In that case, Qiskit exposes the functionsqiskit.qasm3.loadandqiskit.qasm3.loads, which are wrappers aroundqiskit_qasm3_import.parse.
This project is a stop-gap measure until various technical decisions can be
resolved the correct way; Terra makes strong guarantees of stability and support
in its interfaces, and we are not yet ready to make that commitment for this
project, hence the minimal wrappers.ExampleThe principal entry point to the package is the top-levelparsefunction,
which accepts a string containing a complete OpenQASM 3 programme.  This complex
example shows a lot of the capabilities of the importer.OPENQASM 3.0;
// The 'stdgates.inc' include is supported, and the gates are only available
// if it has correctly been included.
include "stdgates.inc";

// Parametrised inputs are supported.
input float[64] a;

qubit[3] q;
bit[2] mid;
bit[3] out;

// Aliasing and re-aliasing are supported.
let aliased = q[0:1];

// Parametrised gates that make use of the stdlib.
gate my_gate(a) c, t {
  gphase(a / 2);
  ry(a) c;
  cx c, t;
}

// Gate modifiers work as well; this gate is equivalent to `p(-a) c;`.
gate my_phase(a) c {
  ctrl @ inv @ gphase(a) c;
}

// We handle mathematical expressions on gate creation and complex indexing
// of temporary collections.
my_gate(a * 2) aliased[0], q[{1, 2}][0];
measure q[0] -> mid[0];
measure q[1] -> mid[1];

while (mid == "00") {
  reset q[0];
  reset q[1];
  my_gate(a) q[0], q[1];
  // We support the builtin mathematical symbols.
  my_phase(a - pi/2) q[1];
  mid[0] = measure q[0];
  mid[1] = measure q[1];
}

// The condition resolver can also handle simple cases that don't look
// _exactly_ like equality conditions.
if (mid[0]) {
  // There is limited support for aliasing within nested scopes.
  let inner_alias = q[{0, 1}];
  reset inner_alias;
}

out = measure q;Assuming this program is stored as a string in a variableprogram, we then
import it into aQuantumCircuitby doing:fromqiskit_qasm3_importimportparsecircuit=parse(program)circuitis now a completeQuantumCircuit, so we can see exactly what it
turned into:circuit.draw()┌───────────────┐┌─┐   ┌─────────────┐┌──────────┐┌─┐
  q_0: ┤0              ├┤M├───┤0            ├┤0         ├┤M├───
       │  my_gate(2*a) │└╥┘┌─┐│             ││          │└╥┘┌─┐
  q_1: ┤1              ├─╫─┤M├┤1            ├┤1         ├─╫─┤M├
       └──────┬─┬──────┘ ║ └╥┘│             ││  If_else │ ║ └╥┘
  q_2: ───────┤M├────────╫──╫─┤  While_loop ├┤          ├─╫──╫─
              └╥┘        ║  ║ │             ││          │ ║  ║
mid_0: ════════╬═════════╩══╬═╡1            ╞╡0         ╞═╬══╬═
               ║            ║ │             │└──────────┘ ║  ║
mid_1: ════════╬════════════╩═╡0            ╞═════════════╬══╬═
               ║              └─────────────┘             ║  ║
out_0: ════════╬══════════════════════════════════════════╩══╬═
               ║                                             ║
out_1: ════════╬═════════════════════════════════════════════╩═
               ║
out_2: ════════╩═══════════════════════════════════════════════InstallationInstall the latest release of theqiskit_qasm3_importpackage from pip:pip install qiskit_qasm3_importThis will automatically install all the dependencies as well (an OpenQASM 3
parser, for example) if they are not already installed.  Alternatively, you can
install Qiskit Terra directly with this package as an optional dependency by
doingpip install qiskit-terra[qasm3-import]DevelopingIf 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.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.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://qiskit.github.io/qiskit-qasm3-import.Code style and lintingThe Python components of this repository are formatted usingblack.  You can
run this on the required files by runningtox-eblackThe full lint suite can be run withtox-elintLicenseThis project is licensed underversion 2.0 of the Apache License.