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

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.1.tar.gz (265.8 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.1-cp312-cp312-win_amd64.whl (691.0 kB view details)

Uploaded CPython 3.12Windows x86-64

quairkit-0.5.1-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.1-cp312-cp312-macosx_11_0_arm64.whl (860.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

quairkit-0.5.1-cp311-cp311-win_amd64.whl (690.0 kB view details)

Uploaded CPython 3.11Windows x86-64

quairkit-0.5.1-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.1-cp311-cp311-macosx_11_0_arm64.whl (859.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

quairkit-0.5.1-cp310-cp310-win_amd64.whl (689.5 kB view details)

Uploaded CPython 3.10Windows x86-64

quairkit-0.5.1-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.1-cp310-cp310-macosx_11_0_arm64.whl (858.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for quairkit-0.5.1.tar.gz
Algorithm Hash digest
SHA256 6046a148af8b16e11d5bf394321f90b5d4ce7c4d15a2c2e9eb82199ed1591a5c
MD5 46c6afbb32c6978e198d4cb3312a7acb
BLAKE2b-256 0314b628fcb14716f60820233a90d32469b1dd6c3d6f58ff2bde843ff6babb96

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for quairkit-0.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8531be311a3a56f7d88c04b59f2e5d72a196dc2ce7f075f6a4d2bacf8743d363
MD5 6c8857703c6a478a34d3536a02750f4b
BLAKE2b-256 8f771137bd2284bdacdd740f38cf58ac7abbad0ae164e421c94e833065d4ba59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6ef6de68042480dae07bd23a49a645e139c7a6ce556a8cd8cb89cd021207aa38
MD5 e722b5f9c6f44b41b65f70dfb9c0b5f1
BLAKE2b-256 071c254674adc53b3808ecd355fcc8c497a990962ca93640fc519c9ecc130935

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ada57ec027c3b977871be101cb1bf757983c3b2b6f5c5ff73cfc42bb20470ecf
MD5 61f212787d2c3632fe2311d3d963524b
BLAKE2b-256 67f0cd38718a67f71b457466cfa927e0989b4d0e00d90c43d8248423a44dd44b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for quairkit-0.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 edfde02b920fe2a771ccc80b46c9fdf1d51e2a4711cee59f93c0f28b13657e2c
MD5 6c7c8d66ea6aa0f050b50d83c9cbd2a2
BLAKE2b-256 0bec6adf8d85b1cc9a16c8b6659d9c206512a3dfa702d56275e441347ca2affb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b3c07be4fc8ac523fcb2e56e9cc58b51d83c9a586bcd1b9ec9d16e4f5fcc7b57
MD5 388916209a7f7cbf3c54760d23f1e12f
BLAKE2b-256 7ecb2e33f8f9fdfef0d4ebb4ae708a7ef7c11a448fd14beb9b8f3769edb41ea7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 412a749cf00951f00e045b8e8b6d70b47c120315589eb11fe405b679eb942c10
MD5 cc7b24b67a63e8cc6f741738b7689ce0
BLAKE2b-256 53e64d2fcff6eb9585f864e62a5e9712d4d522689d8de7ffbcb0441c4274020d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for quairkit-0.5.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 76f9cca069aecada226f8ff4e6805b6fcc03ffca55f1fd4a2c753f2f2caf6375
MD5 920159fc9309b32224097b9402b64661
BLAKE2b-256 41ec0ee3bb71cb3df0e2b2129d346599393972633a336a187b8fed767a269bb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 81ee42cbaad09a54a01255a8835171e652a1e37f543d858682ffc3bf7d9fdec7
MD5 eaff3d9acf9c3f1f09191f920ce37724
BLAKE2b-256 db46384ffe9bd9c256b978269764db3c48f25faf9a440967851c0b4e4baf2084

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for quairkit-0.5.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f5a3a353aeda8201cc0b1017c87c3fd13d397565aa2a3fd5af73e6cc4e6eb92
MD5 aa4bd26276047f7585f5e9da55e5d65a
BLAKE2b-256 df04c71a1735feff02189e61586a5d552b9f7239f2c550b06826cb05ba10edbb

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