Skip to main content

Importer for parsing OpenQASM 3 strings into Qiskit circuits

Project description

Importer from OpenQASM 3 to Qiskit

LicenseReleaseDownloads

This repository provides the Python package qiskit_qasm3_import, which is a basic and temporary importer from OpenQASM 3 into Qiskit's QuantumCircuit.

Qiskit itself does not yet have this functionality due to ongoing technical discussions about the future direction of various data structures within Terra. This project is a stop-gap measure until those issues 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.

Example

The principal entry point to the package is the top-level parse function, 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 variable program, we then import it into a QuantumCircuit by doing:

from qiskit_qasm3_import import parse
circuit = parse(program)

circuit is now a complete QuantumCircuit, 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: ════════╩═══════════════════════════════════════════════

Installation

Install the latest release of the qiskit_qasm3_import package from pip:

pip install qiskit_qasm3_import

This will automatically install all the dependencies as well (an OpenQASM 3 parser, for example) if they are not already installed.

Developing

If you're looking to contribute to this project, please first read our contributing guidelines.

Set up your development environment by installing the development requirements with pip:

pip install -r requirements-dev.txt tox

This 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 as tox and stestr.

After the development requirements are installed, you can install an editable version of the package with

pip install -e .

After this, any changes you make to the library code will immediately be present when you open a new Python interpreter session.

Building documentation

After the development requirements have been installed, the command

tox -e docs

will build the HTML documentation, and place it in docs/_build/html.

Code style and linting

The Python components of this repository are formatted using black. You can run this on the required files by running

tox -e black

The full lint suite can be run with

tox -e lint

License

This project is licensed under version 2.0 of the Apache 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

qiskit-qasm3-import-0.1.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

qiskit_qasm3_import-0.1.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file qiskit-qasm3-import-0.1.0.tar.gz.

File metadata

  • Download URL: qiskit-qasm3-import-0.1.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for qiskit-qasm3-import-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b3313abec88e6e68eced883ff3b2d210fb4d2675e13497df0105efbf2e309472
MD5 4a2c6d36f25c535fad57638cccc0c42e
BLAKE2b-256 1e2e6d2f4cd1466092e6eabf91081f97d4cf26b11ea8083af2b388c12de42449

See more details on using hashes here.

Provenance

File details

Details for the file qiskit_qasm3_import-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for qiskit_qasm3_import-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1f1cea81b6663f7977c8e7e5911543d43954b1c165ca6b2ca59db21ac37c864
MD5 c52079692adfc401f419a986e90e85c8
BLAKE2b-256 a32e95c436fde044b0020be1faa51781bd67a6441ffd2198abede3e66066836c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page