Qclib is a quantum computing library implemented using qiskit.
The focus of qclib is on preparing quantum states, but it is not limited to that.
The easiest way of installing qclib is by using pip:
pip install qclibNow that qclib is installed, you can start building quantum circuits to prepare quantum states. Here is a basic example:
$ python
import numpy as np
from qiskit import transpile
from qiskit_aer import AerSimulator
from qclib.state_preparation import LowRankInitialize
# Generate 3-qubit random input state vector
n = 3
rnd = np.random.RandomState(42)
input_vector = rnd.rand(2 ** n) + rnd.rand(2 ** n) * 1j
input_vector = input_vector / np.linalg.norm(input_vector)
# Build a quantum circuit to initialize the input vector
circuit = LowRankInitialize(input_vector).definition
# Construct an ideal simulator
backend = AerSimulator()
# Tests whether the produced state vector is equal to the input vector.
t_circuit = transpile(circuit, backend)
t_circuit.save_statevector()
state_vector = backend.run(t_circuit).result().get_statevector()
print('Equal:', np.allclose(state_vector, input_vector))
# Equal: TrueThe following table shows the depth, number of qubits, and CNOTs of circuits produced by different state preparation methods for the same random 15-qubit state.
| method | lib | qubits | cnots | depth |
|---|---|---|---|---|
| low-rank | qclib | 15 | 30998 | 53643 |
| svd | qclib | 15 | 38814 | 71580 |
| ucg | qclib | 15 | 32752 | 65505 |
| isometry | qiskit | 15 | 32752 | 65505 |
| multiplexor | qiskit | 15 | 65504 | 131025 |
| bdsp | qclib | 1151 | 72320 | 1603 |
| dcsp | qclib | 32767 | 262016 | 899 |
You can reproduce the results here.
qclib can use SynQ, a C++ synthesis library, as an alternative backend for state preparation, unitaries and diagonal gates. SynQ is optional. It is built from source, so it needs a C++20 compiler, CMake, Eigen and LAPACKE (MKL on Windows):
pip install git+https://github.com/qsoftware/synq
Note that pip install synq without the URL installs an unrelated PyPI package. On Windows, point CMake to vcpkg as described in the SynQ README.
from qclib.state_preparation import SynqInitialize
from qclib.unitary import unitary
from qclib import synq
SynqInitialize.initialize(circuit, state) # state preparation
circuit = unitary(matrix, decomposition="synq") # unitary (CSD/QSD)
circuit = unitary(matrix, decomposition="synq_zxz") # unitary (Block-ZXZ)
circuit = synq.diagonal(diagonal_entries) # diagonal gateThe circuits use only rz, ry, p, cx, cz and h and reproduce the target exactly, including the global phase. Unitaries use the quantum Shannon decomposition with optimizations A.1 and A.2 (20, 100, 444 CNOTs for 3, 4, 5 qubits) or, with decomposition="synq_zxz", the Block-ZXZ decomposition of Krol and Al-Ars (19, 95, 423 CNOTs); synq_zxz also uses h gates. qclib.synq.is_available() reports whether SynQ is installed.
The first version of qclib was developed at Centro de Informática - UFPE. Qclib is an active project, and other people have contributed.
If you are doing research using qclib, please cite our project. We use a CITATION.cff file, so you can easily copy the citation information from the repository landing page.
qclib is free and open source, released under the Apache License, Version 2.0.