Advanced Examples

100+ Advanced Programs

Practical, real-world quantum programs that demonstrate every capability of the Sansqrit DSL. Each program is designed to teach one concept through working code. Click to expand.

Gate Programs (20)

A001All Single-Qubit Gates Demo
simulate {
    let q = quantum_register(18)
    I(q[0]); X(q[1]); Y(q[2]); Z(q[3]); H(q[4])
    S(q[5]); Sdg(q[6]); T(q[7]); Tdg(q[8]); SX(q[9]); SXdg(q[10])
    Rx(q[11],PI/4); Ry(q[12],PI/2); Rz(q[13],PI/3)
    Phase(q[14],PI/6); U1(q[15],PI/8)
    U2(q[16],0.0,PI); U3(q[17],PI/4,PI/2,PI/8)
    print(measure_all(q, shots=1000))
}
A002All Two-Qubit Gates Demo
simulate {
    let q = quantum_register(10)
    H(q[0])
    CNOT(q[0],q[1]); CZ(q[2],q[3]); CY(q[4],q[5])
    CH(q[0],q[6]); CSX(q[0],q[7])
    SWAP(q[1],q[8]); iSWAP(q[2],q[9])
    print(measure_all(q, shots=1000))
}
A003Controlled Rotation Gates
simulate {
    let q = quantum_register(6)
    H(q[0])
    CRx(q[0],q[1],PI/4); CRy(q[0],q[2],PI/3); CRz(q[0],q[3],PI/2)
    CP(q[0],q[4],PI/6); CU(q[0],q[5],PI/4,PI/2,PI/8)
    print(measure_all(q, shots=1000))
}
A004Ising Coupling Gates (RXX/RYY/RZZ/RZX)
simulate {
    let q = quantum_register(8)
    H_all(q)
    RXX(q[0],q[1],PI/4); RYY(q[2],q[3],PI/4)
    RZZ(q[4],q[5],PI/4); RZX(q[6],q[7],PI/4)
    print(measure_all(q, shots=1000))
}
A005SWAP Variants (SWAP/iSWAP/√SWAP/fSWAP/DCX)
simulate {
    let q = quantum_register(10)
    X(q[0]); X(q[2]); X(q[4]); X(q[6]); X(q[8])
    SWAP(q[0],q[1]); iSWAP(q[2],q[3]); SqrtSWAP(q[4],q[5])
    fSWAP(q[6],q[7]); DCX(q[8],q[9])
    print(measure_all(q, shots=1000))
}
A006Three-Qubit Gates (Toffoli/Fredkin/CCZ)
simulate {
    let q = quantum_register(9)
    X(q[0]); X(q[1]); Toffoli(q[0],q[1],q[2])
    X(q[3]); X(q[4]); Fredkin(q[3],q[4],q[5])
    X(q[6]); X(q[7]); X(q[8]); CCZ(q[6],q[7],q[8])
    print(measure_all(q, shots=100))
}
A007Multi-Controlled X (MCX/C3X/C4X)
simulate {
    let q = quantum_register(5)
    for i in range(4) { X(q[i]) }  -- all controls = |1⟩
    C4X(q[0],q[1],q[2],q[3],q[4])
    print(measure_all(q, shots=100))  -- {"11111": 100}
}
A008ECR and MS (Hardware Native Gates)
simulate {
    let q = quantum_register(4)
    ECR(q[0],q[1])  -- IBM native
    MS(q[2],q[3])   -- IonQ native
    print(measure_all(q, shots=1000))
}
A009Gate Unitarity Verification
simulate {
    let q = quantum_register(4)
    H(q[0]); Ry(q[1],1.23); CNOT(q[0],q[2]); RZZ(q[1],q[3],0.5)
    print(f"Total probability: {total_probability():.15f}")
    -- Must be 1.000000000000000
}
A010Bloch Sphere Rotations
simulate {
    let q = quantum_register(1)
    -- X rotation = Bloch x-axis
    Rx(q[0], PI/2)   -- rotate 90° about X
    print(f"After Rx(π/2): ⟨Z⟩={expectation_z(q[0]):.4f}")  -- 0.0
    Rx(q[0], PI/2)   -- rotate another 90°
    print(f"After Rx(π): ⟨Z⟩={expectation_z(q[0]):.4f}")    -- -1.0
}
A011-A020Gate Composition Patterns (10 programs)
-- A011: CNOT from CZ: H(target); CZ(c,t); H(target)
-- A012: SWAP from 3 CNOTs: CNOT(a,b); CNOT(b,a); CNOT(a,b)
-- A013: Toffoli from H+T+CNOT (6 CNOTs + T gates)
-- A014: Controlled-Ry from CNOT+Ry
-- A015: Phase kickback demo: H; CP; H
-- A016: Rz from S and T: S = T·T, Z = S·S
-- A017: U3 decomposition of arbitrary gate
-- A018: fSWAP for fermionic simulation
-- A019: RZZ from CNOT + Rz: CNOT; Rz(θ); CNOT
-- A020: Multi-controlled Z from MCX + H
simulate {
    let q = quantum_register(3)
    -- CNOT from CZ + H:
    H(q[1]); CZ(q[0],q[1]); H(q[1])
    -- Equivalent to: CNOT(q[0], q[1])
    print(measure_all(q, shots=100))
}

Circuit Programs (20)

A021Bell State — All 4 Variants
simulate {
    for (b0,b1) in [(0,0),(0,1),(1,0),(1,1)] {
        let q = quantum_register(2)
        if b0 == 1 { X(q[0]) }
        if b1 == 1 { X(q[1]) }
        H(q[0]); CNOT(q[0],q[1])
        print(f"|Φ{b0}{b1}⟩ = {measure_all(q, shots=100)}")
    }
}
A022W State (3, 5, 8 qubits)
simulate {
    for n in [3, 5, 8] {
        let q = quantum_register(n)
        w_state(q)
        print(f"W({n}): NNZ={engine_nnz()}")
    }
}
A023GHZ Scaling Test (10, 50, 100 qubits)
for n in [10, 50, 100] {
    simulate(engine="chunked") {
        let q = quantum_register(n)
        ghz_state(q)
        print(f"GHZ({n}): NNZ={engine_nnz()}, always 2")
    }
}
A024Dicke States D(6,1), D(6,2), D(6,3)
simulate {
    for k in [1,2,3] {
        let q = quantum_register(6)
        dicke_state(q, k=k)
        print(f"D(6,{k}): NNZ={engine_nnz()}")  -- C(6,k) terms
    }
}
A025Cluster State for MBQC
simulate {
    let q = quantum_register(6)
    cluster_state(q)
    print(f"Cluster(6): NNZ={engine_nnz()}")
    print(measure_all(q, shots=1000))
}
A026Quantum Fourier Transform on |5⟩
simulate {
    let q = quantum_register(4)
    X(q[0]); X(q[2])  -- prepare |0101⟩ = |5⟩
    qft(q)
    print(probabilities(q))
    iqft(q)  -- inverse: should recover |5⟩
    print(measure_all(q, shots=100))  -- {"0101": 100}
}
A027QFT Adder: 3 + 5 = 8
simulate {
    let q = quantum_register(8)
    basis_encoding(q[0..4], 3)   -- A = 3
    basis_encoding(q[4..8], 5)   -- B = 5
    draper_qft_adder(q[0..4], q[4..8])
    print(measure_all(q, shots=100))  -- A register now = 8
}
A028Amplitude Encoding (4D vector)
simulate {
    let q = quantum_register(2)
    amplitude_encoding(q, data=[1.0, 2.0, 3.0, 4.0])
    print(probabilities(q))  -- proportional to [1,4,9,16]/30
}
A029Random Circuit Benchmarking
simulate {
    let q = quantum_register(8)
    random_circuit(q, depth=20, seed=42)
    print(f"NNZ after depth 20: {engine_nnz()}")
    print(f"Norm: {total_probability():.10f}")  -- 1.0
}
A030-A040State Preparation Patterns (11 programs)
-- A030: Basis encoding (integer to qubits)
-- A031: Angle encoding (ML features)
-- A032: W state robustness test (trace out one qubit)
-- A033: Cat state vs GHZ equivalence proof
-- A034: QFT inverse roundtrip test
-- A035: Cluster state measurement pattern
-- A036: Dicke state symmetry verification
-- A037: Entanglement swapping protocol
-- A038: 50-qubit QFT on chunked engine
-- A039: Hardware-efficient ansatz sweep
-- A040: UCCSD ansatz for H2
simulate {
    let q = quantum_register(4)
    let params = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8]
    hardware_efficient_ansatz(q, params, n_layers=1)
    print(f"Ansatz output NNZ: {engine_nnz()}")
}

Algorithm Programs (20)

A041Grover: Find 7 in 16 items
simulate { let r=grover_search(n_qubits=4,target=7,shots=1000); print(r.solution) }
A042Grover: Multi-target search
simulate { let r=grover_search_multi(n_qubits=5,targets=[3,7,15],shots=1000); print(r.histogram) }
A043Shor: Factor 15
simulate { let r=shor_factor(15); print(f"15={r.factors[0]}×{r.factors[1]}") }
A044Shor: Factor 21, 35, 77
simulate {
    for n in [21,35,77] { let r=shor_factor(n); print(f"{n}={r.factors}") }
}
A045VQE: H₂ Potential Energy Surface
simulate {
    for r in range_step(0.4, 2.5, 0.1) {
        let result = vqe_h2(bond_length=r, max_iter=200)
        print(f"R={r:.1f}Å E={result.energy:.6f} Ha")
    }
}
A046QAOA: Triangle Graph MaxCut
simulate { let r=qaoa_maxcut(edges=[(0,1),(1,2),(2,0)],n_nodes=3,layers=2,shots=1000)
print(f"Cut: {r.best_cost}") }
A047QPE: Estimate Phase of Rz(π/4)
simulate { let r=quantum_phase_estimation(n_counting_bits=8,unitary_angle=PI/4)
print(f"Estimated: {r.phase:.6f}, True: {PI/4:.6f}") }
A048HHL: Solve 2×2 System
simulate { let r=hhl_solve([[3,1],[1,3]], [1,0]); print(f"x={r.solution}") }
A049Bernstein-Vazirani: Recover 10110
simulate { print(bernstein_vazirani(n_bits=5, oracle_secret=[1,0,1,1,0])) }
A050Deutsch-Jozsa: Constant vs Balanced
simulate {
    print(deutsch_jozsa(4, "constant_0"))  -- "constant"
    print(deutsch_jozsa(4, "balanced"))    -- "balanced"
}
A051-A060Algorithm Applications (10 programs)
-- A051: Teleportation with Ry(π/3) state
-- A052: Superdense coding all 4 messages
-- A053: BB84 QKD with 256-bit key
-- A054: Quantum walk on 8-position line
-- A055: Quantum counting (3 solutions in 32 items)
-- A056: Amplitude estimation for option pricing
-- A057: Variational classifier on XOR
-- A058: SWAP test: compare Bell with GHZ
-- A059: Simon's algorithm with hidden period
-- A060: Combined Grover + QPE pipeline
simulate {
    let (alice, bob, err) = bb84_qkd(key_length=256)
    print(f"Key bits: {len(alice)}, Error: {err:.4f}")
}

Error Correction Programs (10)

A0613-Qubit Bit-Flip Code — Encode |0⟩
simulate {
    let q = quantum_register(3)
    bit_flip_encode(q[0], q[1], q[2])
    print(measure_all(q, shots=100))  -- {"000": 100}
}
A062Bit-Flip Code — Detect & Correct Error
simulate {
    let q = quantum_register(5)
    bit_flip_encode(q[0],q[1],q[2])
    X(q[1])  -- simulate error on qubit 1
    -- Syndrome measurement
    CNOT(q[0],q[3]); CNOT(q[1],q[3])
    CNOT(q[1],q[4]); CNOT(q[2],q[4])
    print(measure_all(q, shots=100))  -- syndrome tells which qubit flipped
}
A063Phase-Flip Code
simulate {
    let q = quantum_register(3)
    phase_flip_encode(q[0],q[1],q[2])
    Z(q[1])  -- phase error
    print(measure_all(q, shots=100))
}
A064Shor 9-Qubit Code
simulate {
    let q = quantum_register(9)
    shor_9qubit_encode(q)
    print(f"9-qubit code NNZ: {engine_nnz()}")
}
A065Steane 7-Qubit Code
simulate {
    let q = quantum_register(7)
    steane_7qubit_encode(q)
    print(f"Steane code NNZ: {engine_nnz()}")
}
A066-A070Advanced QEC (5 programs)
-- A066: Shor code with X error injection + syndrome decode
-- A067: Shor code with Z error injection + correction
-- A068: Shor code with Y error (combined X+Z)
-- A069: Steane code transversal CNOT
-- A070: Error rate threshold estimation

Quantum Machine Learning (10)

A071QNN: XOR Classifier
simulate {
    let (params, acc) = variational_classifier(
        n_features=2, training_data=[([0,0],0),([0,1],1),([1,0],1),([1,1],0)],
        n_layers=4, epochs=100, lr=0.01)
    print(f"XOR accuracy: {acc:.0%}")
}
A072-A080QML Applications (9 programs)
-- A072: Angle encoding for iris dataset features
-- A073: Amplitude encoding for image classification
-- A074: Variational classifier with 3 features
-- A075: QNN loss landscape visualization
-- A076: Barren plateau detection (random init gradient)
-- A077: Feature map comparison (angle vs amplitude)
-- A078: Quantum kernel estimation
-- A079: Transfer learning: classical features + quantum classifier
-- A080: 100-qubit QNN on chunked engine
simulate(engine="chunked") {
    let q = quantum_register(100)
    let p = random_params(400)
    for l in range(4) {
        for i in range(100) { Ry(q[i], p[l*100+i]) }
        for i in range(100) { CNOT(q[i], q[(i+1)%100]) }
    }
    print(f"100-qubit QNN: NNZ={engine_nnz()}")
}

Real-World Applications (10)

A081Drug Discovery: Screen ACE2 Inhibitors
import medical
let hits = screen_drugs(target="ACE2", library=load_library("FDA_approved"), top_n=10)
for h in hits { print(f"{h.name}: Ki={h.ki:.1f} nM") }
A082-A090Applied Quantum Computing (9 programs)
-- A082: CRISPR guide RNA design for BRCA1
-- A083: Protein folding (QAOA on HP model)
-- A084: DNA→RNA→Protein central dogma pipeline
-- A085: Ising model phase transition
-- A086: Heisenberg chain time evolution
-- A087: Portfolio optimization (QAOA)
-- A088: Quantum random number generation (32-bit)
-- A089: Vaccine epitope prediction
-- A090: Multi-molecule VQE comparison (H2, LiH, H2O)

Distributed & Large-Scale (10)

A091100-Qubit GHZ on Chunked Engine
simulate(engine="chunked") {
    let q = quantum_register(100)
    H(q[0])
    for i in range(99) { CNOT(q[i], q[i+1]) }
    print(f"NNZ: {engine_nnz()}")  -- Always 2!
    print(measure_all(q, shots=200))
}
A092-A100Scale & Distribution (9 programs)
-- A092: 50-qubit QFT with chunk statistics
-- A093: 100-qubit drug binding simulation
-- A094: Cross-chunk CNOT performance test
-- A095: Sparse vs dense memory comparison
-- A096: Chunk statistics visualization
-- A097: 60-qubit random circuit (stress test)
-- A098: Multi-backend export (QASM+IBM+IonQ+Cirq+Braket)
-- A099: Distributed cluster simulation (3 nodes)
-- A100: 200-qubit GHZ (sparse, trivial NNZ=2)
simulate(engine="chunked") {
    let q = quantum_register(200)
    H(q[0])
    for i in range(199) { CNOT(q[i], q[i+1]) }
    print(f"200 qubits! NNZ={engine_nnz()}, Memory ≈ {engine_nnz()*80} bytes")
}
← 120 Basic Programs Gate Reference → GitHub