Sync test_mitigation_runner.py from v8.1.33
Browse files- test_mitigation_runner.py +119 -0
test_mitigation_runner.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for dashboard_core.mitigation_runner.run_mitigation_sweep and
|
| 3 |
+
dashboard_core.mitigation_panel.build_panel_mitigation -- Phase 3 of the
|
| 4 |
+
dashboard refactor (ZNE + predictive healing as a real dashboard feature,
|
| 5 |
+
reusing dense_evolution.mitigation.zero_noise_extrapolation as-is).
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import matplotlib
|
| 9 |
+
matplotlib.use("Agg")
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pytest
|
| 13 |
+
|
| 14 |
+
import dashboard_core as dc
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
BELL_CIRCUIT = "Bell |Φ+⟩"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_run_mitigation_sweep_ideal_raises():
|
| 21 |
+
with pytest.raises(ValueError, match="ideal"):
|
| 22 |
+
dc.run_mitigation_sweep(
|
| 23 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "ideal", 0.05, 256, 42,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_run_mitigation_sweep_returns_expected_keys():
|
| 28 |
+
res = dc.run_mitigation_sweep(
|
| 29 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 30 |
+
)
|
| 31 |
+
expected_keys = {
|
| 32 |
+
"noise_factors", "prob_per_scale", "fidelity_per_scale",
|
| 33 |
+
"prob_zne", "fidelity_zne", "prob_ideal", "healing_enabled",
|
| 34 |
+
"delta_preemp", "n_qubits",
|
| 35 |
+
}
|
| 36 |
+
assert expected_keys == set(res.keys())
|
| 37 |
+
assert res["noise_factors"] == (1.0, 2.0, 3.0)
|
| 38 |
+
assert len(res["prob_per_scale"]) == 3
|
| 39 |
+
assert len(res["fidelity_per_scale"]) == 3
|
| 40 |
+
assert res["healing_enabled"] is False
|
| 41 |
+
assert res["delta_preemp"] is None
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_run_mitigation_sweep_prob_zne_normalized():
|
| 45 |
+
res = dc.run_mitigation_sweep(
|
| 46 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.08, 256, 7,
|
| 47 |
+
)
|
| 48 |
+
assert float(np.sum(res["prob_zne"])) == pytest.approx(1.0, abs=1e-6)
|
| 49 |
+
assert np.all(res["prob_zne"] >= 0.0)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_run_mitigation_sweep_plain_vs_healing_differ():
|
| 53 |
+
common = dict(source_mode="Libreria Built-in", circuit_name=BELL_CIRCUIT,
|
| 54 |
+
qasm_text="", noise_model="depolarizing", base_noise_p=0.08,
|
| 55 |
+
shots=256, seed=3)
|
| 56 |
+
plain = dc.run_mitigation_sweep(**common, healing_enabled=False)
|
| 57 |
+
healed = dc.run_mitigation_sweep(**common, healing_enabled=True)
|
| 58 |
+
assert healed["healing_enabled"] is True
|
| 59 |
+
assert healed["delta_preemp"] is not None
|
| 60 |
+
# not asserting a specific direction -- just that the healing branch is
|
| 61 |
+
# actually wired in and changes the result, not silently ignored
|
| 62 |
+
assert plain["fidelity_zne"] != pytest.approx(healed["fidelity_zne"], abs=1e-12)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_run_mitigation_sweep_healing_requires_three_factors():
|
| 66 |
+
with pytest.raises(ValueError, match="3"):
|
| 67 |
+
dc.run_mitigation_sweep(
|
| 68 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 69 |
+
noise_factors=(1.0, 2.0), healing_enabled=True,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_run_mitigation_sweep_fidelity_zne_directionally_sane():
|
| 74 |
+
# Error Mitigation (Real-Stress): a 15-qubit circuit with several
|
| 75 |
+
# noisy CX/RZ layers -- fidelity degrades noticeably as noise scales up,
|
| 76 |
+
# a case where ZNE has real signal to extrapolate against (unlike a
|
| 77 |
+
# single-CX Bell state, where fidelity barely moves across 1x-3x).
|
| 78 |
+
res = dc.run_mitigation_sweep(
|
| 79 |
+
"Libreria Built-in", "Error Mitigation (Real-Stress)", "",
|
| 80 |
+
"depolarizing", 0.06, 256, 11,
|
| 81 |
+
)
|
| 82 |
+
fid = res["fidelity_per_scale"]
|
| 83 |
+
# not asserting strict per-step monotonicity: NoiseModel.apply_to_sv
|
| 84 |
+
# draws its channel randomness from OS entropy for a JAX statevector
|
| 85 |
+
# (see registry.py's rng/jax_key docs), so a single-shot fidelity
|
| 86 |
+
# sample can plateau or tie between adjacent scales -- the precondition
|
| 87 |
+
# that actually matters is that noise measurably degraded fidelity
|
| 88 |
+
# overall (base vs. worst), not a smooth curve in between.
|
| 89 |
+
assert fid[0] > fid[-1], "expected fidelity to be lower at the highest noise scale than at the base scale in this fixture"
|
| 90 |
+
base_error = abs(1.0 - fid[0])
|
| 91 |
+
zne_error = abs(1.0 - res["fidelity_zne"])
|
| 92 |
+
assert zne_error < base_error, (
|
| 93 |
+
f"ZNE should move the estimate closer to ideal (F=1) than the raw "
|
| 94 |
+
f"base-noise sample: base_error={base_error:.4f}, zne_error={zne_error:.4f}"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def test_build_panel_mitigation():
|
| 99 |
+
mitigation_res = dc.run_mitigation_sweep(
|
| 100 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 101 |
+
)
|
| 102 |
+
base_res = dc.run_simulation(
|
| 103 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 104 |
+
)
|
| 105 |
+
fig = dc.build_panel_mitigation(mitigation_res, base_res)
|
| 106 |
+
assert fig is not None
|
| 107 |
+
assert type(fig).__name__ == "Figure"
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_build_panel_mitigation_with_healing():
|
| 111 |
+
mitigation_res = dc.run_mitigation_sweep(
|
| 112 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 113 |
+
healing_enabled=True,
|
| 114 |
+
)
|
| 115 |
+
base_res = dc.run_simulation(
|
| 116 |
+
"Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 256, 42,
|
| 117 |
+
)
|
| 118 |
+
fig = dc.build_panel_mitigation(mitigation_res, base_res)
|
| 119 |
+
assert fig is not None
|