Skip to main content

Python package for utilizing, implementing, and building quantum algorithms in OpenQASM 3.

Project description

qbraid-algorithms

CI PyPI version PyPI version License Discord

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

[!WARNING] This project is "pre-alpha", and is not yet stable or fully realized. Use with caution, as the API and functionality are subject to significant changes.

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__

Key Features: Load algorithms as PyQASM modules and QASM files

qBraid Algorithms provides a collection of quantum algorithms that can be loaded as PyQASM modules, or you can generate .qasm files to use them as subroutines in your own circuits.

Loading Algorithms as PyQASM Modules

To load an algorithm as a PyQASM module, use the generate_program function from the qbraid_algorithms package, passing algorithm-specific parameters. For example, to load the Quantum Fourier Transform (QFT) algorithm:

from qbraid_algorithms import qft

qft_module = qft.generate_program(3) # Load QFT for 3 qubits

Now, you can perform operations with the PyQASM module, such as unrolling, and converting back to a QASM string:

qft_module.unroll()
qasm_str = pyqasm.dumps(qft_module)

Loading Algorithms as .qasm Files

In order to utilize algorithms as subroutines in your own circuits, use the save_to_qasm function for your desired algorithm. By passing algorithm-specific parameters, and optionally a desired output path, you can generate a .qasm file containing a subroutine for the paramterized circuit. For example, to generate a QFT subroutine for 4 qubits:

from qbraid_algorithms import qft, iqft
path = "path/to/output" # Specify your desired output path
qft.save_to_qasm(4) # Generate 4-qubit QFT in the current directory
iqft.save_to_qasm(4, path=path) # Generate 4-qubit IQFT in specified path

To utilize the generated subroutine in your own circuit, include the generated .qasm file, and call the subroutine on an qubit register of the size specified when generating the subroutine. For example, after running

qft.save_to_qasm(4)

you can append include "qft.qasm"; to your OpenQASM file, and call the subroutine. For example:

OPENQASM 3.0;
include "qft.qasm";

qubit[4] q;
bit[4] c;

qft(q);
measure q -> c;

CLI Usage

qBraid Algorithms includes a command-line interface (CLI) for generating quantum algorithm subroutines.

Installation

To use the CLI, install with CLI dependencies:

pip install "qbraid-algorithms[cli]"

Or install from source:

pip install -e ".[cli]"

Generate Subroutines

Generate quantum algorithm subroutines that can be included in other circuits:

# Generate QFT subroutine for 4 qubits
qbraid-algorithms generate qft --qubits 4

# Generate IQFT subroutine for 3 qubits with custom name and show the circuit
qbraid-algorithms generate iqft -q 3 -o my_iqft.qasm --gate-name my_iqft --show

# Generate Bernstein-Vazirani circuit for secret "101" and display it
qbraid-algorithms generate bernvaz --secret "101" --show

# Generate only the oracle for Bernstein-Vazirani
qbraid-algorithms generate bernvaz -s "1001" --oracle-only --show

# Generate QPE subroutine for 4 qubits with a custom unitary gate
qbraid-algorithms generate qpe --unitary-file my_gate.qasm --qubits 4

# Generate QPE with custom output and show the circuit
qbraid-algorithms generate qpe -u gate.qasm -q 3 -o my_qpe.qasm --show

Help

Get help for any command:

qbraid-algorithms --help
qbraid-algorithms generate --help
qbraid-algorithms generate qft --help
qbraid-algorithms generate iqft --help
qbraid-algorithms generate bernvaz --help
qbraid-algorithms generate qpe --help
qbraid-algorithms generate bernvaz --help

Examples

Complete Workflow

  1. Generate a QFT subroutine:

    qbraid-algorithms generate qft --qubits 3
    
  2. Generate a Bernstein-Vazirani oracle and view it:

    qbraid-algorithms generate bernvaz --secret "101" --oracle-only --show
    
  3. Generate an IQFT circuit with custom output:

    qbraid-algorithms generate iqft --qubits 4 --output my_iqft_4.qasm --show
    
  4. Generate a QPE subroutine for phase estimation:

    qbraid-algorithms generate qpe --unitary-file t_gate.qasm --qubits 3 --show
    

Community

We are actively looking for new contributors!

License

Apache-2.0 License

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

qbraid_algorithms-0.1.2.tar.gz (246.8 kB view details)

Uploaded Source

Built Distribution

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

qbraid_algorithms-0.1.2-py3-none-any.whl (90.3 kB view details)

Uploaded Python 3

File details

Details for the file qbraid_algorithms-0.1.2.tar.gz.

File metadata

  • Download URL: qbraid_algorithms-0.1.2.tar.gz
  • Upload date:
  • Size: 246.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for qbraid_algorithms-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c5637c628a01d2e3bf2edf13e7f39e84c1cebb0252cdfee1278645d25d7b2059
MD5 a3e979fc72e9eeebd8a859e4df184875
BLAKE2b-256 a85d053732e893748ce791dbd8b211e0ea41b465ae18b084227bb9563f4a0919

See more details on using hashes here.

File details

Details for the file qbraid_algorithms-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for qbraid_algorithms-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cde486f963dbb38c9c88b24d8398467a5182095e9e026826f787fcd3a458b0a2
MD5 6151b97baec3cfcdae6e84d72560af8a
BLAKE2b-256 33f77daec37abec28c8a7020a76491e4d77398ae1f8e613c6be33a4989749d7a

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