File size: 5,687 Bytes
1ee2bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa87757
1ee2bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d22d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ee2bb3
 
 
36d22d6
1ee2bb3
36d22d6
1ee2bb3
36d22d6
 
 
1ee2bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Generate the demo circuits shipped with the app.

Run once locally:

    python scripts/generate_sample_circuits.py

Outputs .qpy files into ``backend/sample_circuits/``. The FastAPI layer
auto-discovers any .qpy in that folder at startup.

Circuits are intentionally small (2-4 qubits) so the algorithms run in
seconds even on CPU-only HuggingFace Spaces.
"""

from __future__ import annotations

import sys
from pathlib import Path

import numpy as np
from qiskit import QuantumCircuit, qpy
from qiskit.circuit import Parameter
from qiskit.circuit.library import EfficientSU2


REPO_ROOT = Path(__file__).resolve().parent.parent
OUT_DIR = REPO_ROOT / "backend" / "sample_circuits"


def bell_state() -> QuantumCircuit:
    """The canonical 2-qubit Bell pair. Used for a 'baseline fidelity' story."""
    qc = QuantumCircuit(2, name="bell_state")
    qc.h(0)
    qc.cx(0, 1)
    return qc


def vqc_2q_small() -> QuantumCircuit:
    """Tiny 2-qubit parameterised VQC for smoke-testing the pipeline."""
    qc = QuantumCircuit(2, name="vqc_2q_small")
    theta_0 = Parameter("theta_0")
    theta_1 = Parameter("theta_1")
    qc.ry(theta_0, 0)
    qc.ry(theta_1, 1)
    qc.cx(0, 1)
    return qc


def vqc_efficient_su2_4q() -> QuantumCircuit:
    """A 4-qubit EfficientSU2 ansatz (reps=2) — a common QML benchmark.

    EfficientSU2 alternates SU(2) single-qubit rotations with an entangler
    block; this is the ansatz used in many hardware-efficient VQC papers.
    """
    ansatz = EfficientSU2(num_qubits=4, reps=2, entanglement="linear")
    qc = QuantumCircuit(4, name="efficient_su2_4q")
    qc.compose(ansatz.decompose(), inplace=True)
    return qc


def qaoa_maxcut_4() -> QuantumCircuit:
    """QAOA ansatz (p=1) for Max-Cut on a 4-node ring graph.

    Edges: (0,1), (1,2), (2,3), (3,0). Standard benchmark for QAOA.
    """
    edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
    gamma = Parameter("gamma")
    beta = Parameter("beta")
    qc = QuantumCircuit(4, name="qaoa_maxcut_4")

    # Initial uniform superposition
    qc.h(range(4))

    # Problem unitary: ZZ on each edge with angle 2*gamma
    for a, b in edges:
        qc.cx(a, b)
        qc.rz(2 * gamma, b)
        qc.cx(a, b)

    # Mixer unitary: X rotation on each qubit with angle 2*beta
    for q in range(4):
        qc.rx(2 * beta, q)
    return qc


def ghz_3q() -> QuantumCircuit:
    """3-qubit GHZ state; illustrates entanglement depth vs fidelity."""
    qc = QuantumCircuit(3, name="ghz_3q")
    qc.h(0)
    qc.cx(0, 1)
    qc.cx(1, 2)
    return qc


def w_state_3q() -> QuantumCircuit:
    """3-qubit W state.

    W-state entanglement is robust to qubit loss; a didactic contrast to
    GHZ. Built via the standard RY + controlled-RY + CX construction.
    """
    qc = QuantumCircuit(3, name="w_state_3q")
    theta0 = 2.0 * np.arccos(1.0 / np.sqrt(3.0))
    qc.ry(theta0, 0)
    qc.x(0)
    qc.cry(2.0 * np.arccos(1.0 / np.sqrt(2.0)), 0, 1)
    qc.x(0)
    qc.cx(1, 2)
    qc.cx(0, 1)
    qc.x(0)
    return qc


def qft_3q() -> QuantumCircuit:
    """3-qubit Quantum Fourier Transform.

    Core building block of Shor, phase estimation, and many quantum signal
    algorithms. Non-parameterized, so CompressVQC + Fidelity show clear
    before/after results on it.
    """
    qc = QuantumCircuit(3, name="qft_3q")
    qc.h(0)
    qc.cp(np.pi / 2, 0, 1)
    qc.cp(np.pi / 4, 0, 2)
    qc.h(1)
    qc.cp(np.pi / 2, 1, 2)
    qc.h(2)
    qc.swap(0, 2)
    return qc


def ry_chain_6q() -> QuantumCircuit:
    """6-qubit RY chain — a bigger VQC for QuCAD to sparsify.

    Two RY layers (12 params total) and a linear CX chain between them.
    Gives QuCAD enough parameters that its sparsity trace moves visibly
    over the 3 default ADMM iterations.
    """
    qc = QuantumCircuit(6, name="ry_chain_6q")
    params_a = [Parameter(f"a_{i}") for i in range(6)]
    params_b = [Parameter(f"b_{i}") for i in range(6)]
    for i, p in enumerate(params_a):
        qc.ry(p, i)
    for i in range(5):
        qc.cx(i, i + 1)
    for i, p in enumerate(params_b):
        qc.ry(p, i)
    return qc


def hardware_efficient_4q() -> QuantumCircuit:
    """4-qubit hardware-efficient ansatz.

    Three layers of RY+RZ on each qubit, interleaved with linear CX
    entanglers. A classic warm-start ansatz for VQE / VQC on
    superconducting hardware.
    """
    qc = QuantumCircuit(4, name="hardware_efficient_4q")
    layers = 3
    for L in range(layers):
        for q in range(4):
            qc.ry(Parameter(f"ry_{L}_{q}"), q)
            qc.rz(Parameter(f"rz_{L}_{q}"), q)
        if L < layers - 1:
            for i in range(3):
                qc.cx(i, i + 1)
    return qc


# Ordered from smallest/simplest (top of list) to largest — this is the
# order the circuit picker displays in the UI.
BUILDERS = [
    bell_state,
    ghz_3q,
    w_state_3q,
    vqc_2q_small,
    qft_3q,
    qaoa_maxcut_4,
    vqc_efficient_su2_4q,
    hardware_efficient_4q,
    ry_chain_6q,
]


def main(argv: list[str]) -> int:
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    # Clear stale files so this script is idempotent.
    for stale in OUT_DIR.glob("*.qpy"):
        stale.unlink()

    print(f"Writing sample circuits to {OUT_DIR.relative_to(REPO_ROOT)}/")
    for build in BUILDERS:
        qc = build()
        out_path = OUT_DIR / f"{qc.name}.qpy"
        with open(out_path, "wb") as fh:
            qpy.dump(qc, fh)
        print(
            f"  [OK] {qc.name:<20s} "
            f"qubits={qc.num_qubits} depth={qc.depth()} "
            f"params={qc.num_parameters} size={qc.size()}"
        )
    print("Done.")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))