Skip to content

Random Circuit Generation

API reference for random circuit generation and quantum benchmarking utilities.

random_qcircuit

Generates a random quantum circuit with the specified qubits, depth, and gate types.

Signature

python
random_qcircuit(qubits: list[int], depth: int, gate_type: list[str]) -> QCircuit

Parameters

ParameterTypeDescription
qubitslist[int]List of qubit indices to include in the circuit
depthintDepth (number of gate layers) of the circuit
gate_typelist[str]List of gate type strings to use, e.g. ["H", "CNOT"]

Returns

QCircuit — A randomly generated quantum circuit.

Examples

python
from pyqpanda3.core import random_qcircuit

# Generate a random circuit on qubits [0, 1, 2] with depth 5 using H and CNOT gates
circ = random_qcircuit([0, 1, 2], 5, ["H", "CNOT"])
print(f"Circuit depth: {circ.depth()}")
print(f"Gate count: {circ.count_ops()}")

QV — Quantum Volume Benchmark

Generates a Quantum Volume (QV) benchmark circuit for the given configuration.

Signature

python
QV(num_qubit: int, depth: int, seed: int) -> QCircuit

Parameters

ParameterTypeDescription
num_qubitintNumber of qubits for the QV test
depthintDepth (number of layers) of the circuit
seedintRandom seed for reproducibility

Returns

QCircuit — The quantum volume benchmark circuit.

Examples

python
from pyqpanda3.core import QV

# Compute quantum volume for a 4-qubit, depth-4 circuit
qv = QV(num_qubit=4, depth=4, seed=42)
print(f"Quantum Volume: {qv}")

Mathematical Background

Quantum Volume is a single-number metric that captures the overall capability of a quantum processor. It accounts for qubit count, gate fidelity, connectivity, and software performance. The QV protocol runs random circuits of width n and depth d=n, checking if the heavy-output probability exceeds 2/3.

QV=2min(n,d)

where n is the largest number of qubits for which the heavy-output test passes.

direct_twirl

Performs direct twirling (randomized compiling) on a quantum program, decomposing it into a sequence of random Pauli frames.

Signature

python
direct_twirl(
    input_circ: QProg,
    twirled_gate: str = "CNOT",
    seed: int = 0
) -> QProg

Parameters

ParameterTypeDescription
input_circQProgInput quantum program to twirl
twirled_gatestrGate type to use for twirling (default "CNOT"). Supported: "CNOT", "CZ"
seedintRandom seed (default 0)

Returns

QProg — The twirled quantum program.

Examples

python
from pyqpanda3.core import H, CNOT, direct_twirl, QProg

# Build a simple program
prog = QProg()
prog << H(0) << CNOT(0, 1)

# Apply direct twirling
twirled = direct_twirl(prog, "CNOT", 42)
print(f"Original gates: {prog.count_ops()}")
print(f"Twirled gates: {twirled.count_ops()}")

Mathematical Background

Twirling converts a general quantum channel into a Pauli channel by conjugating with random Pauli operators. For a channel E:

ET(ρ)=14nP{I,X,Y,Z}nPE(PρP)P

This simplifies error characterization and enables error mitigation techniques.

See Also

Released under the MIT License.