API Reference: qbraid.interface

Generate random circuits

Generate a random quantum circuit object of any registered program type from qbraid.programs.NATIVE_REGISTRY. For example:

from qbraid.interface.random import random_circuit

circuit = random_circuit("qiskit")  # Swap "cirq", "braket", "qasm3", etc.
print(circuit)

# Sample output:
# q_0: ─────────────■──────────────■─
#                   │              │
# q_1: ─────────────■──────────────X─
#      ┌─────────────────────────┐ │
# q_2: ┤ U(3.8035,5.0708,2.3633) ├─X─
#      └─────────────────────────┘

Generate random unitaries

Generate random unitary matrices of any specified dimension (dim):

from qbraid.interface.random import random_unitary_matrix

unitary = random_unitary_matrix(2)
print(unitary)

# Sample output:
# [[-0.46060623-0.33349006j -0.82238991+0.01735275j]
#  [ 0.48450787-0.66473935j  0.01019327+0.56856822j]]

Check circuit unitary equivalence

Check whether two quantum circuits have equivalent unitary representations, regardless of their types. All input circuits must be registered in qbraid.programs.NATIVE_REGISTRY. Below is an example demonstrating the comparison between two circuits that each create a Bell state using different quantum programming libraries (Cirq and Qiskit).

import cirq
import qiskit

from qbraid.interface import circuits_allclose

def cirq_bell():
    circuit = cirq.Circuit()
    q0, q1 = cirq.LineQubit.range(2)
    circuit.append(cirq.H(q0))
    circuit.append(cirq.CNOT(q0, q1))
    return circuit

def qiskit_bell():
    circuit = qiskit.QuantumCircuit(2)
    circuit.h(0)
    circuit.cx(0,1)
    return circuit

# Returns True if the unitary matrices of the two circuits are equivalent
print(circuits_allclose(cirq_bell(), qiskit_bell())) # Output: True

This function also supports several optional keyword arguments that allow customization of the equivalence checking process:

  • index_contig: If set to True, maps both circuits to use sequential qubit indexing prior to computing their unitaries. Defaults to False.
  • allow_rev_qubits: If set to True, allows the function to consider circuits as equivalent even if their qubits are in reversed order. Defaults to False.
  • strict_gphase: If set to False, ignores differences in global phase between the circuits. Defaults to True.
  • atol: Sets the absolute tolerance level for the numerical comparison of unitaries, using np.allclose. Defaults to 1e-7.

Here are some examples of how these arguments could be used:

# ignore global phase and use an increased error margin
circuits_allclose(circuit0, circuit1, strict_gphase=False, atol=1e-6)

# allow for non-sequential qubit indexing and reversed qubit order
circuits_allclose(circuit2, circuit3, index_contig=True, allow_rev_qubits=True)