Skip to main content

QuAIRKit is a Python research toolbox for developing quantum computing, quantum information, and quantum machine learning algorithms.

Project description

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

Quick start (recommended)

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.0-*.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.

Acknowledgements

We appreciate the kind support from the Sourcery AI that greatly enhances the coding & review quality of the QuAIRKit project.

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

quairkit-0.5.0.tar.gz (265.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

quairkit-0.5.0-cp312-cp312-win_amd64.whl (689.2 kB view details)

Uploaded CPython 3.12Windows x86-64

quairkit-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (24.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

quairkit-0.5.0-cp312-cp312-macosx_11_0_arm64.whl (858.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

quairkit-0.5.0-cp311-cp311-win_amd64.whl (688.2 kB view details)

Uploaded CPython 3.11Windows x86-64

quairkit-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (24.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

quairkit-0.5.0-cp311-cp311-macosx_11_0_arm64.whl (857.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

quairkit-0.5.0-cp310-cp310-win_amd64.whl (687.8 kB view details)

Uploaded CPython 3.10Windows x86-64

quairkit-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (24.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

quairkit-0.5.0-cp310-cp310-macosx_11_0_arm64.whl (856.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file quairkit-0.5.0.tar.gz.

File metadata

  • Download URL: quairkit-0.5.0.tar.gz
  • Upload date:
  • Size: 265.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for quairkit-0.5.0.tar.gz
Algorithm Hash digest
SHA256 282d8dc6e7e26ece008cc825849543fe0d905cf28ef10a480319a52ec42a9b85
MD5 3dba5141e462249ee53af0620bff9ce0
BLAKE2b-256 a5ce48b3620075f184c4477c4cef81edc7d4b701b0afbb904d02360abaa90fc9

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: quairkit-0.5.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 689.2 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for quairkit-0.5.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 08fbff396c03484b915a401cb3228123d12e8ff52d8ffa34e3239675def7fa3e
MD5 cc7e2b3bb6ae46ad5b5597385a8de7e2
BLAKE2b-256 9d55ab4fdc38dfbb760ce20700845b938bebcff439757dfb808229593ebc7ca5

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 37f253ffc5173efa90af7edd2d805112762f7b5de3dd75f6f01fd803206d1710
MD5 b159fa9495b4dc4098b5b0ec28b82fc2
BLAKE2b-256 2e70ed76f475c7f28ed7b82b2862b9f53ec4dc3139e378ec97d43a9e4a6aea95

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87ca2c0d0f8e380d9cbfe646b2055fde178a6cf54325beb8c8df935fc68aa50a
MD5 b93d295566d1d71758a0deef232d45c1
BLAKE2b-256 e9f7482a25ced64da4419e371cc8184bbe9ddf66a92bbb9c2a413d9247a461f9

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: quairkit-0.5.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 688.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for quairkit-0.5.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 97edcf7315ab1016fe3cd5cab83a22861980365329299304a6ce51381c319122
MD5 2e5015a2b59ce551e38eb0a138f35069
BLAKE2b-256 c6d903028b48b996fe376cadbf91a39d8233985512ea7d03f7eda15a98d470c6

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7ebfa55e30617ade0a0bdada9dd05f3e733297cfa97d5a83e045f61d40fe6991
MD5 8177ecc08b81dd24e07fc80de1d46c61
BLAKE2b-256 163968280b182e78fb7496e7845e009a128e5cdc0d602f5cfd3ce2935ba8bd4b

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc147ec62a96260e7dec38ff4dccec60f35904d3b77bd1c6fad9309cc98205d8
MD5 d267d9ed5642c6471d3cf8ae924abff9
BLAKE2b-256 4266d498c11f6796dddbf149597440c34cf1a4432bb610a883732dd1015c5314

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: quairkit-0.5.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 687.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for quairkit-0.5.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 356222afeb60d9fd4077d00baf50e876984e459adef6de80994b992f066261b7
MD5 5d65e3313a1cce004369a97c2f2a2e1b
BLAKE2b-256 bdc9af1450838ab66a7e6af30d6b287f9b9e5110115e92f98fa6e72a029d5171

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d58f6d0d59f52d4427ff88dc15cc4a1dbc953e959a76c9ed23d9589470f6be1a
MD5 4bceca4eff82004a3875f7c3ce707c2d
BLAKE2b-256 ba299becefee1a517b3620071795c9f89b7ec2b2f3df6e336e740549f3690780

See more details on using hashes here.

File details

Details for the file quairkit-0.5.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quairkit-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4282d216490d163d3e68f2ab5f7d29fd9729e3f48f8cea1123f5735bbd0a38dd
MD5 67322f3777a71a5f098fc8d958b3a160
BLAKE2b-256 06f91f1431d9167519559fd23146c4c35aca2c946f00b7b3cd2361a298359a88

See more details on using hashes here.

Supported by

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