QuAIRKit is a Python research framework for quantum computing, quantum information, and quantum machine learning algorithm development. It focuses on flexible design, real-time simulation and rapid verification of quantum and classical algorithms.
QuAIRKit provides the following functionalities,
- Quantum neural network algorithm simulation
- Quantum circuit simulation & visualization
- Quantum channel simulation
- Quantum algorithm/information tools
The minimum Python environment for QuAIRKit is 3.8
. We recommend installing QuAIRKit with Python 3.10
.
conda create -n quair python=3.10
conda activate quair
conda install jupyter notebook
We recommend the following way of installing QuAIRKit with pip,
pip install quairkit
or download all the files and finish the installation locally,
git clone https://github.com/QuAIR/QuAIRKit
cd QuAIRKit
pip install -e . --config-settings editable_mode=strict
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
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
QuAIRKit provides a wide range of features for quantum computing, quantum information processing and quantum machine learning. Below are some of the key features:
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 batch computation for quantum circuit simulation. Here a zero state is passed through four different quantum circuits, and compared with the target state.
target_state = zero_state(1)
unitary_data = pauli_group(1)
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(cir(), target_state))
tensor([1.0000, 0.4794, 0.8415, 0.0707])
Above output is equivalent to
QuAIRKit also supports batch computations for quantum circuit simulations and most of the quantum information processing tools in qudit quantum computing, as shown below
# claim three systems, with 1 qubit and 1 qutrit
cir = Circuit(2, system_dim=[2, 3])
# apply 6^2 Heisenberg-Weyl operators on all systems
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]]
---------------------------------------------------
QuAIRKit supports probabilistic quantum circuit simulation, which allows you to simulate quantum circuits with probabilistic operations, such as measurement, partial post-selection, LOCC. This is useful in quantum communication protocols or quantum algorithm design. This functional is also compatible with batch computation 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
Circuit in QuAIRKIt can be plotted with Quantikz, a LaTeX package for quantum circuit visualization, which is useful for academic presentation. You can use the plot
function to visualize the circuit. Make sure you have up-to-date LaTeX installed on your system, so that the quantikz
package is available.
cir: Circuit = ...
cir.plot(print_code=True) # plot the circuit with LaTeX code
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 Hadamard gate on all qubits
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.
If you want to perform noise simulation or mixed-state-related tools, there is no need to specify the backend, or import other libraries. Just call the function, and QuAIRKit will transit the backend for you.
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)
state_vector
density_matrix
density_matrix
We appreciate the kind support from the Sourcery AI that greatly enhances the coding & review quality of the QuAIRKit project.