exportPython: make the reproducibility claim true
Browse files- input_circuit: sample runs emit a REAL loader (QPY downloaded from
/api/circuits/samples/<key>/download, cached beside the script)
instead of a hardcoded QuantumCircuit(4) placeholder under a header
claiming bit-exactness. Uploaded-circuit runs emit a sys.exit guard
BEFORE the placeholder so the script can never silently print wrong
numbers.
- fake/ibm/compvqc backends: qiskit.providers.fake_provider imports
(dead since Qiskit 1.0, and FakeFezV2 never existed there) replaced
with qiskit_ibm_runtime.fake_provider — the exact import the studio
backend (workflow_service / qlib) uses.
- qucad: removed the dead qc_bound block. Verified against
qlib/qucad.py: run_qucad_training_noisy trains the PARAMETERIZED
circuit (the backend passes ctx["circuit"] unbound; pre-binding
would leave zero trainable parameters), so the emitter now mirrors
_handle_qucad exactly and binds theta*mask after training.
- plugin/unknown steps: emitted as "# Step N: <kind> — plugin step,
not exportable" comments instead of a silent hole.
- check_export_python lane: new assertions for the loader URL, the
uploaded-circuit guard ordering, qiskit_ibm_runtime imports, the
plugin-step comment, and the qc_bound removal.
|
@@ -7,7 +7,15 @@
|
|
| 7 |
// (an early-stopped run's script must re-send the stopping rule
|
| 8 |
// or it would run all shots and reproduce nothing);
|
| 9 |
// 3. absence cases: no seed helper without a root seed / without a
|
| 10 |
-
// sampled step; no precision_target kwarg when the run had none
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
//
|
| 12 |
// exportPython imports lib/anon.ts, which touches import.meta.env at
|
| 13 |
// module scope, so this cannot run under plain node strip-types.
|
|
@@ -65,6 +73,16 @@ const prov = (over: Partial<ExportProvenance> = {}): ExportProvenance => ({
|
|
| 65 |
// No target on this run -> the kwarg must NOT appear.
|
| 66 |
assert.doesNotMatch(s, /precision_target=/);
|
| 67 |
assert.doesNotMatch(s, /stop_target/);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
// -- 2. early-stopped run: precision_target threaded -------------------------
|
|
@@ -129,4 +147,58 @@ const prov = (over: Partial<ExportProvenance> = {}): ExportProvenance => ({
|
|
| 129 |
assert.doesNotMatch(s, /_derive_seed|ROOT_SEED|precision_target|stop_target|run_id/);
|
| 130 |
}
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
console.log("check_export_python: all assertions passed");
|
|
|
|
| 7 |
// (an early-stopped run's script must re-send the stopping rule
|
| 8 |
// or it would run all shots and reproduce nothing);
|
| 9 |
// 3. absence cases: no seed helper without a root seed / without a
|
| 10 |
+
// sampled step; no precision_target kwarg when the run had none;
|
| 11 |
+
// 4. circuit provenance (Wave 1 audit): sample-key runs emit a real
|
| 12 |
+
// QPY loader against /api/circuits/samples/<key>/download —
|
| 13 |
+
// never a silent placeholder; uploaded-circuit runs emit a
|
| 14 |
+
// sys.exit guard BEFORE the placeholder so the script can never
|
| 15 |
+
// print wrong numbers silently;
|
| 16 |
+
// 5. Qiskit >= 1.0 imports (qiskit_ibm_runtime.fake_provider, the
|
| 17 |
+
// same family the studio backend imports) and no dead qc_bound
|
| 18 |
+
// in the QuCAD step; plugin steps are announced, not skipped.
|
| 19 |
//
|
| 20 |
// exportPython imports lib/anon.ts, which touches import.meta.env at
|
| 21 |
// module scope, so this cannot run under plain node strip-types.
|
|
|
|
| 73 |
// No target on this run -> the kwarg must NOT appear.
|
| 74 |
assert.doesNotMatch(s, /precision_target=/);
|
| 75 |
assert.doesNotMatch(s, /stop_target/);
|
| 76 |
+
// Circuit provenance: a REAL loader for the sample QPY, not a
|
| 77 |
+
// placeholder — and no refuse-to-run guard on a fetchable circuit.
|
| 78 |
+
assert.match(s, /urlretrieve\(/);
|
| 79 |
+
assert.match(s, /\/api\/circuits\/samples\/bell_state\/download/);
|
| 80 |
+
assert.match(s, /qc = qpy\.load\(f\)\[0\]/);
|
| 81 |
+
assert.doesNotMatch(s, /sys\.exit/);
|
| 82 |
+
// Qiskit >= 1.0: the fake-provider family the studio backend uses.
|
| 83 |
+
assert.match(s, /from qiskit_ibm_runtime\.fake_provider import FakeFez/);
|
| 84 |
+
assert.doesNotMatch(s, /from qiskit\.providers\.fake_provider import/);
|
| 85 |
+
assert.doesNotMatch(s, /FakeFezV2/);
|
| 86 |
}
|
| 87 |
|
| 88 |
// -- 2. early-stopped run: precision_target threaded -------------------------
|
|
|
|
| 147 |
assert.doesNotMatch(s, /_derive_seed|ROOT_SEED|precision_target|stop_target|run_id/);
|
| 148 |
}
|
| 149 |
|
| 150 |
+
// -- 6. uploaded circuit: refuse-to-run guard, never a silent placeholder ----
|
| 151 |
+
{
|
| 152 |
+
const { nodes, edges } = graph("sampled");
|
| 153 |
+
const s = generatePythonScript(nodes, edges, null, prov());
|
| 154 |
+
assert.match(s, /sys\.exit\(/);
|
| 155 |
+
assert.match(s, /replace the placeholder circuit/);
|
| 156 |
+
assert.doesNotMatch(s, /urlretrieve/);
|
| 157 |
+
// The guard must come BEFORE the placeholder assignment.
|
| 158 |
+
assert.ok(
|
| 159 |
+
s.indexOf("sys.exit(") < s.indexOf("qc = QuantumCircuit(4"),
|
| 160 |
+
"sys.exit guard precedes the placeholder circuit",
|
| 161 |
+
);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// -- 7. plugin / unknown step: announced in the script, not skipped ----------
|
| 165 |
+
{
|
| 166 |
+
const { nodes, edges } = graph("sampled");
|
| 167 |
+
const withPlugin = [
|
| 168 |
+
...nodes,
|
| 169 |
+
{ id: "n9", data: { kind: "plugin:my_pass", params: {} } },
|
| 170 |
+
] as any[];
|
| 171 |
+
const s = generatePythonScript(
|
| 172 |
+
withPlugin,
|
| 173 |
+
[...edges, { id: "e9", source: "n4", target: "n9" }] as any[],
|
| 174 |
+
"bell_state",
|
| 175 |
+
prov(),
|
| 176 |
+
);
|
| 177 |
+
assert.match(
|
| 178 |
+
s,
|
| 179 |
+
/# Step \d+: plugin:my_pass — plugin step, not exportable/,
|
| 180 |
+
);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// -- 8. QuCAD: trains the parameterized circuit, no dead qc_bound ------------
|
| 184 |
+
{
|
| 185 |
+
const nodes = [
|
| 186 |
+
{ id: "n1", data: { kind: "input_circuit", params: {} } },
|
| 187 |
+
{
|
| 188 |
+
id: "n2",
|
| 189 |
+
data: { kind: "fake_backend", params: { backend_name: "FakeFez", shots: 1024 } },
|
| 190 |
+
},
|
| 191 |
+
{ id: "n3", data: { kind: "qucad", params: {} } },
|
| 192 |
+
] as any[];
|
| 193 |
+
const edges = [
|
| 194 |
+
{ id: "e1", source: "n1", target: "n2" },
|
| 195 |
+
{ id: "e2", source: "n2", target: "n3" },
|
| 196 |
+
] as any[];
|
| 197 |
+
const s = generatePythonScript(nodes, edges, "vqc_2q_small", prov());
|
| 198 |
+
assert.match(s, /run_qucad_training_noisy\(/);
|
| 199 |
+
assert.doesNotMatch(s, /qc_bound/);
|
| 200 |
+
// The trained parameters ARE bound after training (backend parity).
|
| 201 |
+
assert.match(s, /qc = qc\.assign_parameters\(theta \* \(mask != 0\)\)/);
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
console.log("check_export_python: all assertions passed");
|
|
@@ -112,31 +112,57 @@ function asNum(v: unknown, dflt: number): number {
|
|
| 112 |
}
|
| 113 |
|
| 114 |
const emitInputCircuit: Emitter = (_b, _step, { sampleKey }) => {
|
| 115 |
-
const lines: string[] = [];
|
| 116 |
if (sampleKey) {
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
};
|
| 133 |
|
| 134 |
const emitFakeBackend: Emitter = (b) => {
|
| 135 |
const name = asStr(b.params.backend_name, "FakeFez");
|
| 136 |
const shots = asNum(b.params.shots, 1024);
|
| 137 |
return [
|
| 138 |
-
`
|
| 139 |
-
`backend
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
`noise_model = NoiseModel.from_backend(backend)`,
|
| 141 |
`shots = ${shots} # user-set measurement count`,
|
| 142 |
];
|
|
@@ -153,8 +179,8 @@ const emitIbmBackend: Emitter = (b) => {
|
|
| 153 |
`# noise_model = NoiseModel.from_backend(backend)`,
|
| 154 |
`#`,
|
| 155 |
`# For now, falling back to fake backend:`,
|
| 156 |
-
`from
|
| 157 |
-
`backend =
|
| 158 |
`noise_model = NoiseModel.from_backend(backend)`,
|
| 159 |
`shots = ${shots}`,
|
| 160 |
];
|
|
@@ -167,12 +193,9 @@ const emitQucad: Emitter = (b) => {
|
|
| 167 |
return [
|
| 168 |
`from qlib.qucad import run_qucad_training_noisy`,
|
| 169 |
``,
|
| 170 |
-
`#
|
| 171 |
-
`
|
| 172 |
-
`
|
| 173 |
-
`else:`,
|
| 174 |
-
` qc_bound = qc`,
|
| 175 |
-
``,
|
| 176 |
`theta, mask, history = run_qucad_training_noisy(`,
|
| 177 |
` qc, noise_model, backend,`,
|
| 178 |
` iterations=${iters}, lam=${lam}, rho=${rho},`,
|
|
@@ -220,8 +243,8 @@ const emitCompvqc: Emitter = () => [
|
|
| 220 |
`if qc.num_parameters > 0:`,
|
| 221 |
` comp_backend = backend if 'backend' in dir() else None`,
|
| 222 |
` if comp_backend is None:`,
|
| 223 |
-
` from
|
| 224 |
-
` comp_backend =
|
| 225 |
` lut = get_LUT(qc, comp_backend)`,
|
| 226 |
` if lut:`,
|
| 227 |
` qp = quadraticProgram_luttoqp(qc, lut)`,
|
|
@@ -434,8 +457,17 @@ export function generatePythonScript(
|
|
| 434 |
blocks.forEach((b, i) => {
|
| 435 |
const stepNum = i + 1;
|
| 436 |
const emitter = EMITTERS[b.kind];
|
| 437 |
-
if (!emitter) return; // Plugin / unknown — skip silently.
|
| 438 |
lines.push(sep);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
lines.push(`# Step ${stepNum}: ${STEP_TITLES[b.kind] ?? b.kind}`);
|
| 440 |
lines.push(...emitter(b, stepNum, ctx));
|
| 441 |
lines.push(``);
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
const emitInputCircuit: Emitter = (_b, _step, { sampleKey }) => {
|
|
|
|
| 115 |
if (sampleKey) {
|
| 116 |
+
// Bit-exact circuit provenance: download the SAME QPY the studio
|
| 117 |
+
// run executed (backend endpoint
|
| 118 |
+
// /api/circuits/samples/<key>/download), cached beside the script.
|
| 119 |
+
// A hardcoded placeholder here would silently break every
|
| 120 |
+
// "reproduces the archived numbers" claim in the header.
|
| 121 |
+
const origin =
|
| 122 |
+
typeof window !== "undefined" ? window.location.origin : "";
|
| 123 |
+
return [
|
| 124 |
+
`# Sample circuit: ${sampleKey} — fetched bit-exact (QPY) from the`,
|
| 125 |
+
`# deployment this run executed on; cached next to the script.`,
|
| 126 |
+
`from pathlib import Path`,
|
| 127 |
+
`from urllib.request import urlretrieve`,
|
| 128 |
+
`from qiskit import qpy`,
|
| 129 |
+
``,
|
| 130 |
+
`_qpy_path = Path("${sampleKey}.qpy")`,
|
| 131 |
+
`if not _qpy_path.exists():`,
|
| 132 |
+
` urlretrieve(`,
|
| 133 |
+
` "${origin}/api/circuits/samples/${sampleKey}/download",`,
|
| 134 |
+
` _qpy_path,`,
|
| 135 |
+
` )`,
|
| 136 |
+
`with _qpy_path.open("rb") as f:`,
|
| 137 |
+
` qc = qpy.load(f)[0]`,
|
| 138 |
+
`print(f"Loaded {qc.name}: {qc.num_qubits}q, depth {qc.depth()}")`,
|
| 139 |
+
];
|
| 140 |
}
|
| 141 |
+
// Uploaded circuit: this script cannot re-materialize it. Refuse to
|
| 142 |
+
// run rather than silently produce numbers from a placeholder — a
|
| 143 |
+
// wrong-but-plausible print would corrupt the provenance chain.
|
| 144 |
+
return [
|
| 145 |
+
`import sys`,
|
| 146 |
+
``,
|
| 147 |
+
`sys.exit(`,
|
| 148 |
+
` "replace the placeholder circuit: this run used an UPLOADED circuit, "`,
|
| 149 |
+
` "which this script cannot fetch. Load it below (qiskit.qpy / "`,
|
| 150 |
+
` "QuantumCircuit.from_qasm_file), then delete this guard."`,
|
| 151 |
+
`)`,
|
| 152 |
+
`qc = QuantumCircuit(4, name="my_circuit") # placeholder — REPLACE (see guard above)`,
|
| 153 |
+
];
|
| 154 |
};
|
| 155 |
|
| 156 |
const emitFakeBackend: Emitter = (b) => {
|
| 157 |
const name = asStr(b.params.backend_name, "FakeFez");
|
| 158 |
const shots = asNum(b.params.shots, 1024);
|
| 159 |
return [
|
| 160 |
+
`# Requires: pip install qiskit-ibm-runtime — the same fake-backend`,
|
| 161 |
+
`# family the studio backend imports (qiskit.providers.fake_provider`,
|
| 162 |
+
`# was removed in Qiskit 1.0).`,
|
| 163 |
+
`from qiskit_ibm_runtime.fake_provider import ${name}`,
|
| 164 |
+
``,
|
| 165 |
+
`backend = ${name}()`,
|
| 166 |
`noise_model = NoiseModel.from_backend(backend)`,
|
| 167 |
`shots = ${shots} # user-set measurement count`,
|
| 168 |
];
|
|
|
|
| 179 |
`# noise_model = NoiseModel.from_backend(backend)`,
|
| 180 |
`#`,
|
| 181 |
`# For now, falling back to fake backend:`,
|
| 182 |
+
`from qiskit_ibm_runtime.fake_provider import FakeFez`,
|
| 183 |
+
`backend = FakeFez()`,
|
| 184 |
`noise_model = NoiseModel.from_backend(backend)`,
|
| 185 |
`shots = ${shots}`,
|
| 186 |
];
|
|
|
|
| 193 |
return [
|
| 194 |
`from qlib.qucad import run_qucad_training_noisy`,
|
| 195 |
``,
|
| 196 |
+
`# QuCAD trains the PARAMETERIZED circuit directly (mirrors the`,
|
| 197 |
+
`# studio backend's _handle_qucad — binding first would leave zero`,
|
| 198 |
+
`# trainable parameters); theta/mask are bound after training below.`,
|
|
|
|
|
|
|
|
|
|
| 199 |
`theta, mask, history = run_qucad_training_noisy(`,
|
| 200 |
` qc, noise_model, backend,`,
|
| 201 |
` iterations=${iters}, lam=${lam}, rho=${rho},`,
|
|
|
|
| 243 |
`if qc.num_parameters > 0:`,
|
| 244 |
` comp_backend = backend if 'backend' in dir() else None`,
|
| 245 |
` if comp_backend is None:`,
|
| 246 |
+
` from qiskit_ibm_runtime.fake_provider import FakeFez`,
|
| 247 |
+
` comp_backend = FakeFez()`,
|
| 248 |
` lut = get_LUT(qc, comp_backend)`,
|
| 249 |
` if lut:`,
|
| 250 |
` qp = quadraticProgram_luttoqp(qc, lut)`,
|
|
|
|
| 457 |
blocks.forEach((b, i) => {
|
| 458 |
const stepNum = i + 1;
|
| 459 |
const emitter = EMITTERS[b.kind];
|
|
|
|
| 460 |
lines.push(sep);
|
| 461 |
+
if (!emitter) {
|
| 462 |
+
// Plugin / unknown kind: say so IN the script instead of leaving
|
| 463 |
+
// a silent hole — the reader must be able to see the exported
|
| 464 |
+
// pipeline is not the whole studio pipeline.
|
| 465 |
+
lines.push(
|
| 466 |
+
`# Step ${stepNum}: ${b.kind} — plugin step, not exportable (runs only inside ${APP_NAME}'s backend)`,
|
| 467 |
+
);
|
| 468 |
+
lines.push(``);
|
| 469 |
+
return;
|
| 470 |
+
}
|
| 471 |
lines.push(`# Step ${stepNum}: ${STEP_TITLES[b.kind] ?? b.kind}`);
|
| 472 |
lines.push(...emitter(b, stepNum, ctx));
|
| 473 |
lines.push(``);
|