CLI Usage

qBraid Algorithms includes a command-line interface (CLI) for generating quantum algorithm subroutines. These subroutines can either be saved in .qasm files or displayed directly in the terminal, providing users with a convenient way to access and utilize quantum algorithms.

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
Command
qbraid-algorithms generate qft --qubits 4 --show
Output
QFT subroutine for 4 qubits generated successfully.
Output: /path/to/qft.qasm
Generated QASM:
--------------------------------------------------
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];
  }
}
--------------------------------------------------
  • Generate IQFT subroutine for 3 qubits with custom name and show the circuit
Command
qbraid-algorithms generate iqft -q 3 -o my_iqft.qasm --gate-name my_iqft --show
Output
QASM:
--------------------------------------------------
OPENQASM 3.0;
include "stdgates.inc";

def my_iqft(qubit[3] q) {
    int n = 3;

    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];
    }
}
--------------------------------------------------
  • Generate only the oracle for Bernstein-Vazirani
Command
qbraid-algorithms generate bernvaz -s "1001" --oracle-only --show
Output
Generating Bernstein-Vazirani oracle for secret '1001'...
Bernstein-Vazirani oracle generated successfully.
Output: /path/to/oracle.qasm
Secret string: 1001
Qubits needed: 4 + 1 ancilla
Generated QASM:
--------------------------------------------------
OPENQASM 3.0;
include "stdgates.inc";

def oracle(qubit[4] q, qubit[1] ancilla) {
    int[32] s = 9;
    int[16] n = 4;
    for int i in [0:n - 1] {
        if ((s >> i) & 1) {
            cx q[i], ancilla[0];
        }
    }
}

--------------------------------------------------
  • Generate a QPE subroutine for phase estimation:
Command
qbraid-algorithms generate qpe --unitary-file gate.qasm --qubits 3 --show
Output
QPE subroutine for 3 qubits generated successfully.
Unitary file: /path/to/gate.qasm
Output: /path/to/qpe.qasm
Generated QASM:
--------------------------------------------------
OPENQASM 3.0;
include "stdgates.inc";
include "iqft.qasm";

gate custom_t q {
  p(pi / 4) q;
}

gate CU a, b {
  ctrl @ custom_t a, b;
}


def qpe(qubit[3] q, qubit[1] psi) {
    int n = 3;
    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);

}
--------------------------------------------------

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

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