Quantum Algorithms Library.
Python package for building, simulating, and benchmarking hybrid quantum-classical algorithms.
Installation
qbraid-algorithms requires Python 3.11 or greater, and can be installed with pip as follows:
pip install qbraid-algorithms
Install from source
You can also install from source by cloning this repository and running a pip install command
in the root directory of the repository:
git clone https://github.com/qBraid/qbraid-algorithms.git
cd qbraid-algorithms
pip3 install .
Check version
You can view the version of qbraid-algorithms you have installed within a Python shell as follows:
import qbraid_algorithms
qbraid_algorithms.__version__
Supported Algorithms
from qbraid_algorithms import bernstein_vazirani
secret_key = '01001'
algo = bernstein_vazirani.generate_program(secret_key)
print(algo)
OPENQASM 3.0;
include "stdgates.inc";
def bernvaz(qubit[5] q, qubit[1] ancilla) {
int[32] s = 18;
int[16] n = 5;
for int i in [0:n - 1] {
h q[i];
}
x ancilla[0];
h ancilla[0];
for int i in [0:n - 1] {
if (s >> i & 1) {
cx q[i], ancilla[0];
}
}
for int i in [0:n - 1] {
h q[i];
}
}
qubit[5] q;
qubit[1] ancilla;
bit[5] b;
bernvaz(q, ancilla);
b = measure q;
from qbraid_algorithms import qft
algo = qft.generate_program(4)
print(algo)
OPENQASM 3.0;
include "stdgates.inc";
def qft(qubit[4] q) {
int n = 4;
for int[16] i in [0:n - 1] {
h q[i];
for int[16] j in [i + 1:n - 1] {
int[16] k = j - i;
cp(2 * pi / (1 << k + 1)) q[j], q[i];
}
}
for int[16] i in [0:(n >> 1) - 1] {
swap q[i], q[n - i - 1];
}
}
qubit[4] q;
bit[4] b;
qft(q);
b = measure q;
from qbraid_algorithms import iqft
algo = iqft.generate_program(4)
print(algo)
OPENQASM 3.0;
include "stdgates.inc";
def iqft(qubit[4] q) {
int n = 4;
for int[16] i in [0:n - 1] {
int[16] target = n - i - 1;
for int[16] j in [0:n - target - 2] {
int[16] control = n - j - 1;
int[16] k = control - target;
cp(-2 * pi / (1 << k + 1)) q[control], q[target];
}
h q[target];
}
for int[16] i in [0:(n >> 1) - 1] {
swap q[i], q[n - i - 1];
}
}
qubit[4] q;
bit[4] b;
iqft(q);
b = measure q;
from qbraid_algorithms import qpe
"""
Path to a qasm file defining the unitary gate U.
Eg. -
OPENQASM 3.0;
include "stdgates.inc";
gate custom_t q {
p(pi/4) q;
}
"""
unitary_filepath = "gate.qasm"
"""
Path to a qasm file defining the eigenstate preparation gate.
Eg. -
OPENQASM 3.0;
include "stdgates.inc";
gate prep q {
x q;
}
"""
eigen_state_filepath = "eigen_state.qasm"
algo = qpe.generate_program(num_qubits = 4, unitary_filepath=unitary_filepath,
psi_filepath=eigen_state_filepath)
print(algo)
OPENQASM 3.0;
include "stdgates.inc";
def iqft(qubit[4] q) {
int n = 4;
for int[16] i in [0:n - 1] {
int[16] target = n - i - 1;
for int[16] j in [0:n - target - 2] {
int[16] control = n - j - 1;
int[16] k = control - target;
cp(-2 * pi / (1 << k + 1)) q[control], q[target];
}
h q[target];
}
for int[16] i in [0:(n >> 1) - 1] {
swap q[i], q[n - i - 1];
}
}
gate custom_t q {
p(pi / 4) q;
}
gate CU a, b {
ctrl @ custom_t a, b;
}
def qpe(qubit[4] q, qubit[1] psi) {
int n = 4;
for int i in [0:n - 1] {
h q[i];
}
for int j in [0:n - 1] {
int[16] k = 1 << j;
for int m in [0:k - 1] {
CU q[j], psi[0];
}
}
iqft(q);
}
qubit[4] q;
qubit[1] psi;
bit[4] b;
gate prep_eigenstate q {
x q;
}
qpe(q, psi);
b = measure q;
See API Reference for complete functionality of each algorithm module.