Skip to main content

PyQIR parses, generates and evaluates the Quantum Intermediate Representation.

Project description

PyQIR

The pyqir package provides the ability to generate QIR as well as an easy way to execute generated QIR.

QIR generation is intended to be used by code automating translation processes enabling the conversion in some format to QIR via Python; i.e., this is a low-level API intended to be used as a bridge to existing Python frameworks enabling the generation of QIR rather than directly consumed by an end-user. It is not intended to be used as a framework for algorithm and application development.

QIR evaluation can be used for:

  1. easily testing and experimenting with QIR code
  2. connecting it to low-level Python-based lab software such as e.g. QCoDeS

It contains the necessary just-in-time compilation infrastructure as well as an extensibility mechanism to define what actions to perform when a gate is applied in Python. Right now the evaluator does not have a full runtime environment and can JIT QIR produced by the pyqir-generator, but cannot use any external function calls.

Installation

The package is released on PyPI and can be installed via pip:

pip install pyqir

Examples

PyQIR examples can be found in this repository's examples folder.

Generation

The following code creates QIR for a Bell pair before measuring each qubit and returning the result. The unoptimized QIR is displayed in the terminal when executed:

from pyqir.generator import BasicQisBuilder, SimpleModule

module = SimpleModule("Bell", num_qubits=2, num_results=2)
qis = BasicQisBuilder(module.builder)

qis.h(module.qubits[0])
qis.cx(module.qubits[0], module.qubits[1])

qis.mz(module.qubits[0], module.results[0])
qis.mz(module.qubits[1], module.results[1])

print(module.ir())

The QIR output will look like:

; ModuleID = 'Bell'
source_filename = "Bell"

%Qubit = type opaque
%Result = type opaque

define void @main() #0 {
entry:
  call void @__quantum__qis__h__body(%Qubit* null)
  call void @__quantum__qis__cnot__body(%Qubit* null, %Qubit* inttoptr (i64 1 to %Qubit*))
  call void @__quantum__qis__mz__body(%Qubit* null, %Result* null)
  call void @__quantum__qis__mz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*))
  ret void
}

declare void @__quantum__qis__h__body(%Qubit*)

declare void @__quantum__qis__cnot__body(%Qubit*, %Qubit*)

declare void @__quantum__qis__mz__body(%Qubit*, %Result*)

attributes #0 = { "EntryPoint" "requiredQubits"="2" "requiredResults"="2" }

Evaluation

Let's look at how to log the gate sequence for the Bernstein-Vazirani example.

We can evaluate the generated bitcode with NonadaptiveEvaluator and GateLogger to print out a simple log of the quantum application:

from pyqir.evaluator import NonadaptiveEvaluator, GateLogger

from pathlib import Path
import os

path = Path(__file__).parent
file = os.path.join(path, "bernstein_vazirani.bc")

evaluator = NonadaptiveEvaluator()
logger = GateLogger()

evaluator.eval(file, logger)

print("# output from GateLogger")
logger.print()

This would generate the following output:

# output from GateLogger
qubits[9]
out[9]
x qubit[8]
h qubit[0]
h qubit[1]
h qubit[2]
h qubit[3]
h qubit[4]
h qubit[5]
h qubit[6]
h qubit[7]
h qubit[8]
cx qubit[2], qubit[8]
cx qubit[3], qubit[8]
h qubit[0]
h qubit[1]
h qubit[2]
h qubit[3]
h qubit[4]
h qubit[5]
h qubit[6]
h qubit[7]
measure qubits[0] -> out[0]
measure qubits[1] -> out[1]
measure qubits[2] -> out[2]
measure qubits[3] -> out[3]
measure qubits[4] -> out[4]
measure qubits[5] -> out[5]
measure qubits[6] -> out[6]
measure qubits[7] -> out[7]
measure qubits[8] -> out[8]

Contributing

There are many ways in which you can contribute to PyQIR, whether by contributing a feature or by engaging in discussions; we value contributions in all shapes and sizes! We refer to this document for guidelines and ideas for how you can get involved.

Contributing a pull request to this repo requires to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. A CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately. Simply follow the instructions provided by the bot. You will only need to do this once.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pyqir-0.7.0-cp37-abi3-win_amd64.whl (7.7 MB view details)

Uploaded CPython 3.7+ Windows x86-64

pyqir-0.7.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

pyqir-0.7.0-cp37-abi3-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

pyqir-0.7.0-cp37-abi3-macosx_10_7_x86_64.whl (7.9 MB view details)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

File details

Details for the file pyqir-0.7.0-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: pyqir-0.7.0-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyqir-0.7.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fda3e0dcb75f15ecfc1bbdb094089877823446ba3034597eba43578826121fbc
MD5 46c6336863e2d0620d87f3797d7b816c
BLAKE2b-256 f65d5e8d628300f125ff74b796d28b26c0b3fc158d852196d99bdc29e160c6c5

See more details on using hashes here.

File details

Details for the file pyqir-0.7.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyqir-0.7.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41ac0488ff0d02ffc446bb158d6f1fb5a4501ab9e5f9fc664348ebbae1cbff14
MD5 19c9edf4bfac4d344a06734e8c8667a3
BLAKE2b-256 3775114c27f6774af6f13b7a54cc0e7096f586641a01f8766fe188b55676bba5

See more details on using hashes here.

File details

Details for the file pyqir-0.7.0-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyqir-0.7.0-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d47576b75f8a3e6a8b6b78a5cceacdb621e27f7c5a151e5f1239eca3f0a77db6
MD5 47224c8c63cc41530c6421d91c3f478e
BLAKE2b-256 2c92f5296beb001e0c6814e48cfc2dc69734f4134cb1d303430dcc0dbfa30a8d

See more details on using hashes here.

File details

Details for the file pyqir-0.7.0-cp37-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pyqir-0.7.0-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 6234db4f7fcf5e83f9c50f940a82aa28257b37a27c4ac18a15563b181d5ff28f
MD5 5fee5991616f4f0e6a19f34dbbb5e385
BLAKE2b-256 37d51e6565e5208546d04c87625ae93d46579006c4387367c8aa76f13cf9d141

See more details on using hashes here.

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