Complete Reference
Circuit Reference — 17 Circuits
Pre-built quantum circuit constructors for state preparation, arithmetic, data encoding, error correction, and variational ansätze. Source: circuits.rs
State Preparation (6)
Bell State
The simplest entangled state: (|00⟩ + |11⟩)/√2. Created by H(q[0]) + CNOT(q[0], q[1]). Fundamental building block for teleportation, superdense coding, and entanglement-based protocols. Uses 2 qubits, 2 gates.
bell_state(q[0], q[1]) -- (|00⟩ + |11⟩)/√2
GHZ State
Greenberger-Horne-Zeilinger state: (|00...0⟩ + |11...1⟩)/√2. Maximum entanglement across all qubits. Violates Bell inequalities maximally. Used in quantum secret sharing, error correction benchmarking, and many-body physics. In Sansqrit's sparse engine, a 100-qubit GHZ state has exactly 2 non-zero entries (~100 bytes).
ghz_state(q) -- all qubits entangled
-- 100-qubit GHZ: NNZ = 2, memory = ~100 bytes
W State
W state: (|100...0⟩ + |010...0⟩ + ... + |000...1⟩)/√n. Equal superposition of all states with exactly one qubit in |1⟩. Unlike GHZ, W states are more robust — tracing out one qubit still leaves the remaining qubits entangled. Used in quantum networks and multipartite entanglement protocols.
w_state(q) -- n terms, each with exactly one |1⟩
Cluster State
Graph state on a linear chain: H on all qubits then CZ between adjacent pairs. Cluster states are the resource for measurement-based quantum computing (MBQC / one-way quantum computing). Any quantum computation can be performed by adaptive single-qubit measurements on a cluster state.
cluster_state(q) -- H⊗n then CZ chain
Dicke State D(n, k)
Equal superposition of all n-qubit states with exactly k ones. D(n,1) = W state. D(n, n/2) = the most entangled Dicke state. Number of terms = C(n,k). Used in quantum networking, quantum voting protocols, and studying multipartite entanglement.
dicke_state(q, k=2) -- D(n,2): C(n,2) terms
Cat State
Schrödinger cat state — same as GHZ: (|0⟩⊗n + |1⟩⊗n)/√2. Named after Schrödinger's thought experiment about macroscopic quantum superposition.
cat_state(q) -- alias for GHZ
Quantum Transforms (2)
Quantum Fourier Transform (QFT)
The quantum analog of the discrete Fourier transform. Maps computational basis states to Fourier basis states. Core subroutine in: Shor's algorithm (period finding), quantum phase estimation, quantum arithmetic (Draper adder), and quantum signal processing. Uses O(n²) gates: n Hadamards and n(n-1)/2 controlled-phase rotations.
qft(q) -- QFT on all qubits
qft_on(q[2..6]) -- QFT on subset
Inverse QFT
iqft(q) -- inverse QFT: reverse order + conjugate phases
Quantum Arithmetic (2)
Draper QFT Adder
Adds register B into register A using the QFT: apply QFT(A), then controlled-phase rotations from B to A (in Fourier space), then inverse QFT(A). No ancilla qubits needed. Gate count: O(n²).
draper_qft_adder(a_qubits, b_qubits) -- A = A + B
Quantum Multiplier
Multiplies two n-bit quantum registers using shift-and-add with the Draper adder as subroutine.
quantum_multiplier(a, b, result) -- result = a × b
Data Encoding (3)
Amplitude Encoding
Encodes a classical vector x = [x₁, x₂, ..., x_N] into the amplitudes of a quantum state: |ψ⟩ = Σ xᵢ|i⟩ / ||x||. Requires log₂(N) qubits to encode N data points — exponential compression.
amplitude_encoding(q, data=[1.0, 2.0, 3.0, 4.0]) -- 4 values in 2 qubits
Angle Encoding
Encodes each data point as a rotation angle: Ry(xᵢ·π) on qubit i. Uses n qubits for n data points. Simpler than amplitude encoding but no exponential compression.
angle_encoding(q, data=[0.5, 0.3, 0.8]) -- Ry rotations
Basis Encoding
Encodes an integer as a computational basis state: value 5 = |0101⟩. Uses X gates on the appropriate qubits.
basis_encoding(q, value=42) -- |101010⟩
Quantum Error Correction (4)
3-Qubit Bit-Flip Code
Encodes 1 logical qubit into 3 physical qubits to protect against single X (bit-flip) errors. Encoding: CNOT(data, a1) + CNOT(data, a2). If one physical qubit flips, majority voting recovers the original state.
bit_flip_encode(data=q[0], ancilla1=q[1], ancilla2=q[2])
3-Qubit Phase-Flip Code
Protects against single Z (phase-flip) errors. Same structure as bit-flip code but in the Hadamard basis: CNOT + H on all three qubits.
phase_flip_encode(data=q[0], a1=q[1], a2=q[2])
Shor's 9-Qubit Code
The first quantum error-correcting code, discovered by Peter Shor in 1995. Encodes 1 logical qubit into 9 physical qubits. Protects against any single-qubit error (X, Y, Z, or any combination). Combines bit-flip and phase-flip codes: first encode against phase flips (3 blocks), then each block is encoded against bit flips (3 qubits each). This is the [[9,1,3]] code.
shor_9qubit_encode(qubits=[q[0]..q[8]]) -- 1 logical in 9 physical
Steane's 7-Qubit Code
The [[7,1,3]] CSS (Calderbank-Shor-Steane) code. Encodes 1 logical qubit into 7 physical qubits with distance 3 (corrects any single-qubit error). More efficient than Shor's 9-qubit code. Based on the classical [7,4,3] Hamming code. Transversal gates: H, CNOT, S are all transversal (can be applied bitwise without spreading errors).
steane_7qubit_encode(qubits=[q[0]..q[6]]) -- 1 logical in 7 physical
Variational Ansatz Circuits (2)
Hardware-Efficient Ansatz
The standard ansatz for near-term VQE on real hardware. Alternates layers of Ry/Rz rotations (2n parameters per layer) with linear CNOT entanglement chains. Designed to have low circuit depth while maintaining enough expressibility to approximate ground states. Parameters per layer: 2n (n = number of qubits).
hardware_efficient_ansatz(q, params, n_layers=3)
-- Layer structure: [Ry, Rz] on each qubit → CNOT chain → repeat
UCCSD Ansatz
Unitary Coupled Cluster Singles and Doubles — the gold standard ansatz for quantum chemistry. Starts from the Hartree-Fock state (first n_electrons qubits in |1⟩), then applies singles excitations (i→a) and doubles excitations (i,j→a,b) parameterized by θ. UCCSD captures the bulk of electron correlation energy and achieves chemical accuracy (< 1 kcal/mol error) for small molecules.
uccsd_ansatz(q, params, n_electrons=2)
-- Starts from HF state, applies singles + doubles excitations
Utility Circuits (2)
Random Circuit Generator
Generates random quantum circuits for benchmarking. Each layer applies random single-qubit gates (H, X, T, S, Rz, Ry) followed by a CNOT layer with linear connectivity. Parameterized by depth and seed for reproducibility. Used for: quantum volume estimation, compiler benchmarking, and scrambling studies.
random_circuit(q, depth=10, seed=42) -- reproducible random circuit
Entanglement Swapping
Transfers entanglement between non-interacting particles. Given two Bell pairs (A,B) and (C,D), performs a Bell measurement on B and C to entangle A and D — even though A and D never directly interacted. Fundamental for quantum repeaters and long-distance quantum communication.
entanglement_swapping(qubits=[A, B, C, D])
-- Input: Bell pairs (A,B) and (C,D)
-- Output: A and D are entangled