File size: 8,838 Bytes
61a5f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
218
"""Regenerate the precomputed-run disk cache from the LIVE Space.

Why live and not local (`precompute_preset_results.py`): the deployed
Space has torch and the exact runtime that will later SERVE these
entries, so responses recorded here carry every provenance field the
current UI renders (transformation signatures, gate diffs,
circuit_shape, distribution payloads) with the deployed code's exact
semantics. The 2026-07 audit found the shipped cache predated the
provenance rework — schema-less entries that would render a crippled
first run — AND that every key had silently gone unreachable because
a qiskit upgrade changed the QPY bytes the key hashes. This script
closes both holes:

  * responses come from the live API (fresh mode, NO seed — the disk
    cache is seed-free by design; `save_cached_response` scrubs the
    envelope and stamps `cache_schema`);
  * keys are computed locally with `run_cache.compute_cache_key`, and
    the script REFUSES to run unless the local qiskit version equals
    the live server's (/api/health) — the only way the keys can match
    what the deployed server computes at request time.

Torch-heavy presets (qubound, qshot, full — LSTM training / GNN
warmup, tens of seconds to minutes per run) are deliberately NOT
cached by default: the schema gate makes those combos run fresh,
which is honest — better a slow true answer than an instant stale one.

Resumable: combos whose cache file already exists are skipped, and
`--budget-seconds` stops the script cleanly between combos, so it can
be re-invoked under a hard process cap (the dev sandbox kills
processes at ~40 s) until it reports "nothing left to do".

Usage, from the repo root (match the venv's qiskit to the live pin!):

    python scripts/regenerate_precomputed_cache_live.py             # qucad+compvqc x all samples
    python scripts/regenerate_precomputed_cache_live.py --presets qucad --samples bell_state
    python scripts/regenerate_precomputed_cache_live.py --base-url http://localhost:8000
"""

from __future__ import annotations

import argparse
import json
import sys
import time
import urllib.request
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent
BACKEND_DIR = REPO_ROOT / "backend"
sys.path.insert(0, str(BACKEND_DIR))
sys.path.insert(0, str(REPO_ROOT / "scripts"))

import qiskit  # noqa: E402

from app.schemas import FlowEdge, FlowNode, RunResponse  # noqa: E402
from app.services.circuit_service import (  # noqa: E402
    SAMPLE_CIRCUITS_DIR,
    load_sample,
)
from app.services.run_cache import (  # noqa: E402
    CACHE_DIR,
    compute_cache_key,
    save_cached_response,
)
from precompute_preset_results import PRESETS  # noqa: E402

DEFAULT_BASE = "https://jqub21-quaim.hf.space"
# Cheap, torch-light presets only — see module docstring.
DEFAULT_PRESETS = "qucad,compvqc"


def _get(base: str, path: str, timeout: float = 30) -> dict:
    with urllib.request.urlopen(f"{base}/api{path}", timeout=timeout) as r:
        return json.loads(r.read().decode("utf-8"))


def _post(base: str, path: str, body: dict | None, timeout: float) -> dict:
    data = json.dumps(body).encode("utf-8") if body is not None else b""
    req = urllib.request.Request(
        f"{base}/api{path}",
        data=data,
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    with urllib.request.urlopen(req, timeout=timeout) as r:
        return json.loads(r.read().decode("utf-8"))


def _frontend_body(preset: dict, circuit_id: str) -> dict:
    """The exact JSON the frontend's Run button sends (FlowCanvas
    makeBody): id/type/data per node, id/source/target per edge,
    fresh mode (no seed, no precision_target)."""
    return {
        "circuit_id": circuit_id,
        "nodes": [
            {"id": n["id"], "type": n["type"], "data": n["data"]}
            for n in preset["nodes"]
        ],
        "edges": [
            {"id": f"e{i + 1}", "source": e["source"], "target": e["target"]}
            for i, e in enumerate(preset["edges"])
        ],
        "use_live_ibm": False,
    }


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    ap.add_argument("--base-url", default=DEFAULT_BASE)
    ap.add_argument("--presets", default=DEFAULT_PRESETS)
    ap.add_argument("--samples", default=None, help="default: all bundled")
    ap.add_argument("--run-timeout", type=float, default=120.0)
    ap.add_argument(
        "--budget-seconds",
        type=float,
        default=None,
        help="stop cleanly (exit 3) before starting a combo that would "
        "exceed this wall-clock budget; re-invoke to resume",
    )
    ap.add_argument("--force", action="store_true", help="rewrite existing files")
    args = ap.parse_args()

    health = _get(args.base_url, "/health")
    live_qiskit = health.get("qiskit_version")
    if live_qiskit != qiskit.__version__:
        print(
            f"REFUSING: local qiskit {qiskit.__version__} != live {live_qiskit}.\n"
            "The cache key hashes QPY bytes, which are qiskit-version-"
            "dependent; keys computed here would never match what the "
            "deployed server computes. Install the matching version, e.g.\n"
            f"  python -m venv --system-site-packages /tmp/qk && "
            f"/tmp/qk/bin/pip install --no-deps qiskit=={live_qiskit}",
            file=sys.stderr,
        )
        return 2

    preset_keys = [k.strip() for k in args.presets.split(",") if k.strip()]
    for k in preset_keys:
        if k not in PRESETS:
            print(f"Unknown preset {k!r}. Known: {list(PRESETS)}", file=sys.stderr)
            return 2
    sample_keys = (
        [k.strip() for k in args.samples.split(",")]
        if args.samples
        else sorted(p.stem for p in SAMPLE_CIRCUITS_DIR.glob("*.qpy"))
    )

    t0 = time.time()
    done = skipped = failed = 0
    for pk in preset_keys:
        preset = PRESETS[pk]
        for sk in sample_keys:
            qc = load_sample(sk)
            nodes = [FlowNode(**n) for n in preset["nodes"]]
            edges = [FlowEdge(**e) for e in preset["edges"]]
            key = compute_cache_key(qc, nodes, edges, use_live_ibm=False)
            path = CACHE_DIR / f"{key}.json"
            tag = f"{pk} x {sk} (key={key})"
            if path.exists() and not args.force:
                print(f"skip  {tag} — file exists")
                skipped += 1
                continue
            if (
                args.budget_seconds is not None
                and time.time() - t0 > args.budget_seconds
            ):
                print(f"budget reached — resume by re-running ({tag} next)")
                return 3
            try:
                ci = _post(args.base_url, f"/circuits/samples/{sk}", None, 30)
                resp_raw = _post(
                    args.base_url,
                    "/workflow/run",
                    _frontend_body(preset, ci["circuit_id"]),
                    args.run_timeout,
                )
            except Exception as e:  # noqa: BLE001
                print(f"FAIL  {tag}: {e}", file=sys.stderr)
                failed += 1
                continue
            if resp_raw.get("from_cache"):
                # The live server already serves this key from ITS disk
                # cache (i.e. a previous regeneration was deployed).
                # Re-storing what the cache returned would be a no-op at
                # best; if the local file is missing something is
                # inconsistent — surface it instead of writing.
                print(f"skip  {tag} — live already serves from cache")
                skipped += 1
                continue
            if not resp_raw.get("ok"):
                msgs = [
                    s.get("message")
                    for s in resp_raw.get("steps", [])
                    if s.get("status") == "error"
                ]
                print(f"skip  {tag} — run not ok: {msgs}", file=sys.stderr)
                failed += 1
                continue
            ok_steps = [s for s in resp_raw["steps"] if s.get("status") == "ok"]
            if not ok_steps or "transformation" not in ok_steps[0]:
                print(
                    f"FAIL  {tag}: live response lacks transformation payload — "
                    "refusing to cache a schema-less response",
                    file=sys.stderr,
                )
                failed += 1
                continue
            response = RunResponse.model_validate(resp_raw)
            save_cached_response(key, response)
            done += 1
            print(f"wrote {tag} -> {path.name}")
    print(f"\n{done} written, {skipped} skipped, {failed} failed in {time.time() - t0:.1f}s")
    return 0 if failed == 0 else 1


if __name__ == "__main__":
    sys.exit(main())