Skip to main content

QuAIRKit

QuAIRKit is a Python SDK for algorithm development in quantum computing, quantum information, and quantum machine learning. It focuses on flexible design, real-time simulation and rapid verification of quantum and classical algorithms.

QuAIRKit provides the following functionalities,

  • Quantum algorithm simulation & optimization
  • Quantum circuit simulation & visualization
  • Quantum channel simulation
  • Quantum algorithm/information tools

We provide skills for coding agents to use QuAIRKit.

Installation

The minimum supported Python version for QuAIRKit is 3.10. A typical fresh environment is:

conda create -n quair python=3.10
conda activate quair
conda install jupyter notebook  # for notebook tutorials

If you use Python >= 3.10, PyTorch >= 2.11, and one of the following platforms: Windows x86_64, Linux x86_64, or macOS Apple Silicon (arm64), the command below installs the recommended PyPI wheel.

pip install quairkit

Or install a wheel downloaded from GitHub Releases (Assets):

pip install ./quairkit-0.5.1-*.whl

These wheels are built against PyTorch 2.11.x and are expected to work with both CPU and CUDA PyTorch builds, as long as your installed PyTorch is also 2.11.x.

If you need a source or developer install instead, see Installation from source at the end of this README.

Setup

After installation, you can import QuAIRKit in your Python code as follows:

import quairkit as qkit
import torch # library for tensor manipulation

from quairkit import Circuit # standard quantum circuit interface

from quairkit.database import * # common matrices, sets, Hamiltonian, states, etc.
from quairkit.qinfo import * # common functions in quantum information processing
from quairkit.loss import * # common loss operators in neural network training

In most workflows, Circuit builds the executable workflow, quairkit.database provides standard states, matrices, and channels, quairkit.qinfo analyzes outputs, and quairkit.loss packages reusable training objectives.

QuAIRKit provides global setup functions to set the default data type, device and random seed.

qkit.set_dtype('complex128') # default data type is 'complex64'
qkit.set_device('cuda') # make sure CUDA is setup with torch
qkit.set_seed(73) # set seeds for all random number generators

Features

QuAIRKit provides a wide range of features for quantum computing, quantum information processing and quantum machine learning. Below are some of the key features:

Batch computation

QuAIRKit supports batch computations for quantum circuit simulations, state measurement and quantum information processing. It is easy to use and can be customized for different quantum (machine learning) algorithms.

Below is an example of batched circuit simulation. Here one circuit applies a batched oracle and batched rotation parameters, then compares the four outputs with the same target state.

target_state = zero_state(1)
unitary_data = pauli_group(1) # I, Pauli-X/Y/Z

cir = Circuit(1)
cir.oracle(unitary_data, 0)
cir.ry(param=[0, 1, 2, 3])

output_state = cir() # zero-state input by default
print(state_fidelity(output_state, target_state))
tensor([1.0000, 0.4794, 0.8415, 0.0707])

Above output is equivalent to

\left|\bra{0} R_y(0)\,I \ket{0} \right|,\,\,
\left|\bra{0} R_y(1)\,X \ket{0} \right|,\,\,
\left|\bra{0} R_y(2)\,Y \ket{0} \right|,\,\,
\left|\bra{0} R_y(3)\,Z \ket{0} \right|

Qudit computation

QuAIRKit also supports qudit workflows, including mixed-dimension circuits and batched operators, as shown below

# create two systems: one qubit and one qutrit
cir = Circuit(2, system_dim=[2, 3])

# apply dimension-6 Heisenberg-Weyl operators on the composite system
cir.oracle(heisenberg_weyl(6), [0, 1])

# apply the H gate on the qubit, controlled by the qutrit
cir.oracle(h(), [1, 0], control_idx=0)

# trace out the qutrit system and get the qubit state
traced_state = cir().trace(1)

print('The 6th and 7th state for the batched qubit state is', traced_state[5:7])
The 6th and 7th state for the batched qubit state is 
---------------------------------------------------
 Backend: density_matrix
 System dimension: [2]
 System sequence: [0]
 Batch size: [2]

 # 0:
[[1.+0.j 0.+0.j]
 [0.+0.j 0.+0.j]]
 # 1:
[[0.5+0.j 0.5+0.j]
 [0.5+0.j 0.5+0.j]]
---------------------------------------------------

Probabilistic computation

QuAIRKit supports probabilistic quantum circuit simulation, which allows you to simulate quantum circuits with probabilistic operations such as measurement, partial post-selection, and LOCC. This is useful in quantum communication protocols and quantum algorithm design. This functionality is also compatible with batch and qudit computation.

Below is the implementation of a qubit teleportation protocol in QuAIRKit.

M1_locc = torch.stack([eye(), x()]) # apply X gate for measure outcome 1
M2_locc = torch.stack([eye(), z()]) # apply Z gate for measure outcome 1

# setup protocol
cir = Circuit(3)
cir.cnot([0, 1])
cir.h(0)
cir.locc(M1_locc, [1, 2]) # measure on qubit 1, apply local operations on qubit 2
cir.locc(M2_locc, [0, 2]) # measure on qubit 0, apply local operations on qubit 2

# test with 100 random single-qubit (mixed) states
psi = random_state(1, size=100)
input_state = nkron(psi, bell_state(2))
output_state = cir(input_state).trace([0, 1]) # discard first two qubits

fid = state_fidelity(output_state.expec_state(), psi).mean().item()
print('The average fidelity of the teleportation protocol is', fid)
The average fidelity of the teleportation protocol is 0.9999999999998951

Here cir(input_state) keeps the LOCC branch structure, and expec_state() averages that branch axis before the fidelity is compared with one reference state per input sample.

Other functionalities

Plot circuit with LaTeX

Circuit in QuAIRKit can be visualized with Quantikz, a LaTeX package for quantum circuit presentation. Use to_latex() if you only need the source code, or plot() if you want QuAIRKit to render the figure. Make sure you have an up-to-date LaTeX installation so that the quantikz package is available.

cir: Circuit = ...
cir.plot(print_code=True)  # plot the circuit with LaTeX code

See the tutorial for more details.

Third-party Cloud Integration

QuAIRKit supports third-party cloud integration through StateOperator backends. This is a backend-integrator interface rather than the default user path; ordinary users typically interact with such backends through set_backend together with measurement and expectation-value APIs.

class YourState(qkit.StateOperator):
    def _execute(self, qasm: str, shots: int) -> Dict[str, int]:
        r"""IMPLEMENT HERE to execute the circuit on the quantum cloud."""

qkit.set_backend(YourState)

See the tutorial for more details.

Fast construction

QuAIRKit provides a fast and flexible way to construct quantum circuits, by self-managing the parameters. All parameters would be created randomly if not specified. QuAIRKit also supports built-in layer ansatz, such as complex_entangled_layer.

cir = Circuit(2)

cir.rx() # apply Rx gates on all qubits with random parameters
cir.complex_entangled_layer(depth=2) # apply complex entangled layers of depth 2
cir.universal_two_qubits() # apply universal two-qubit gate with random parameters

Circuit is a child class of torch.nn.Module, so you can access its parameters and other attributes directly, or use it as a layer in a hybrid neural network.

Implicit transition

If you want to perform noise simulation or mixed-state-related analysis, there is no need to switch backends manually or import other libraries. Just call the function, and QuAIRKit will move the simulator from the pure-state path to the mixed-state path when the workflow becomes non-unitary.

cir = Circuit(3)

cir.complex_entangled_layer(depth=3)
print(cir().backend)

# partial transpose on the first two qubits
print(cir().transpose([0, 1]).backend)

cir.depolarizing(prob=0.1)
print(cir().backend)
default-pure
default-mixed
default-mixed

Tutorials

Installation from source

Installation from source will compile the C++ extension and compiler/toolchain with C++17 support. Source builds support PyTorch >= 2.4 (recommended: PyTorch 2.11 or newer).

Compiler toolchain references (official):

Install from source (assumes your environment is already activated):

git clone https://github.com/QuAIR/QuAIRKit
cd QuAIRKit

pip install -e . --no-build-isolation

If VSCode/Pylance cannot resolve quairkit after the editable install above, you can choose one of the following:

  • Reinstall in strict editable mode for better IDE compatibility:
pip install -e . --config-settings editable_mode=strict --no-build-isolation
  • Keep the default editable install and add your local QuAIRKit repository root to the global python.analysis.extraPaths setting in VSCode/Pylance. Since the quairkit/ package directory lives directly under the repository root, add the absolute path to your cloned QuAIRKit directory, for example:
{
  "python.analysis.extraPaths": [
    "/path/to/QuAIRKit"
  ]
}

If you frequently modify the source code, we recommend the extraPaths approach above. The strict editable mode may require rerunning the install command after C++ changes.

Metadata

Release files for quairkit 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quairkit 0.5.1
File Size Uploaded
quairkit-0.5.1.tar.gz 265.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for quairkit 0.5.1
File
quairkit-0.5.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
quairkit-0.5.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details
quairkit-0.5.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
quairkit-0.5.1-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
quairkit-0.5.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details
quairkit-0.5.1-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
quairkit-0.5.1-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
quairkit-0.5.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
quairkit-0.5.1-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details

Total release size: 78.5 MB

Release files / quairkit-0.5.1.tar.gz

Download URL quairkit-0.5.1.tar.gz
Size 265.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6046a148af8b16e11d5bf394321f90b5d4ce7c4d15a2c2e9eb82199ed1591a5c
BLAKE2b-256 checksum
How to use checksums
0314b628fcb14716f60820233a90d32469b1dd6c3d6f58ff2bde843ff6babb96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp312-cp312-win_amd64.whl

Download URL quairkit-0.5.1-cp312-cp312-win_amd64.whl
Size 691.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8531be311a3a56f7d88c04b59f2e5d72a196dc2ce7f075f6a4d2bacf8743d363
BLAKE2b-256 checksum
How to use checksums
8f771137bd2284bdacdd740f38cf58ac7abbad0ae164e421c94e833065d4ba59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL quairkit-0.5.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 24.6 MB
Tags CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
6ef6de68042480dae07bd23a49a645e139c7a6ce556a8cd8cb89cd021207aa38
BLAKE2b-256 checksum
How to use checksums
071c254674adc53b3808ecd355fcc8c497a990962ca93640fc519c9ecc130935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL quairkit-0.5.1-cp312-cp312-macosx_11_0_arm64.whl
Size 860.8 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ada57ec027c3b977871be101cb1bf757983c3b2b6f5c5ff73cfc42bb20470ecf
BLAKE2b-256 checksum
How to use checksums
67f0cd38718a67f71b457466cfa927e0989b4d0e00d90c43d8248423a44dd44b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp311-cp311-win_amd64.whl

Download URL quairkit-0.5.1-cp311-cp311-win_amd64.whl
Size 690.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
edfde02b920fe2a771ccc80b46c9fdf1d51e2a4711cee59f93c0f28b13657e2c
BLAKE2b-256 checksum
How to use checksums
0bec6adf8d85b1cc9a16c8b6659d9c206512a3dfa702d56275e441347ca2affb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL quairkit-0.5.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 24.6 MB
Tags CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
b3c07be4fc8ac523fcb2e56e9cc58b51d83c9a586bcd1b9ec9d16e4f5fcc7b57
BLAKE2b-256 checksum
How to use checksums
7ecb2e33f8f9fdfef0d4ebb4ae708a7ef7c11a448fd14beb9b8f3769edb41ea7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp311-cp311-macosx_11_0_arm64.whl

Download URL quairkit-0.5.1-cp311-cp311-macosx_11_0_arm64.whl
Size 859.9 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
412a749cf00951f00e045b8e8b6d70b47c120315589eb11fe405b679eb942c10
BLAKE2b-256 checksum
How to use checksums
53e64d2fcff6eb9585f864e62a5e9712d4d522689d8de7ffbcb0441c4274020d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp310-cp310-win_amd64.whl

Download URL quairkit-0.5.1-cp310-cp310-win_amd64.whl
Size 689.5 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
76f9cca069aecada226f8ff4e6805b6fcc03ffca55f1fd4a2c753f2f2caf6375
BLAKE2b-256 checksum
How to use checksums
41ec0ee3bb71cb3df0e2b2129d346599393972633a336a187b8fed767a269bb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL quairkit-0.5.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 24.4 MB
Tags CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
81ee42cbaad09a54a01255a8835171e652a1e37f543d858682ffc3bf7d9fdec7
BLAKE2b-256 checksum
How to use checksums
db46384ffe9bd9c256b978269764db3c48f25faf9a440967851c0b4e4baf2084
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release files / quairkit-0.5.1-cp310-cp310-macosx_11_0_arm64.whl

Download URL quairkit-0.5.1-cp310-cp310-macosx_11_0_arm64.whl
Size 858.5 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4f5a3a353aeda8201cc0b1017c87c3fd13d397565aa2a3fd5af73e6cc4e6eb92
BLAKE2b-256 checksum
How to use checksums
df04c71a1735feff02189e61586a5d552b9f7239f2c550b06826cb05ba10edbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.18

Release history Release notifications | RSS feed

This release

0.5.1 This release

10 release files

0.5.0

10 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page