2 """Legacy exact lowering validation. 4 Builds representative legacy `qgd_Circuit` inputs and verifies that exact 5 in-bounds cases lower into the canonical planner surface while unsupported gate 6 families and malformed attached noise schedules fail before planner entry. 9 python benchmarks/density_matrix/planner_surface/legacy_exact_lowering_validation.py 12 from __future__
import annotations
17 from pathlib
import Path
19 REPO_ROOT = Path(__file__).resolve().parents[3]
20 if str(REPO_ROOT)
not in sys.path:
21 sys.path.insert(0, str(REPO_ROOT))
23 from benchmarks.density_matrix.planner_surface.common
import build_software_metadata
24 from squander
import Circuit
25 from squander.partitioning.noisy_planner
import (
26 build_canonical_planner_surface_from_qgd_circuit,
27 build_planner_audit_record,
30 SUITE_NAME =
"phase3_planner_surface_legacy_exact_lowering" 31 ARTIFACT_FILENAME =
"legacy_exact_lowering_bundle.json" 32 DEFAULT_OUTPUT_DIR = (
33 REPO_ROOT /
"benchmarks" /
"density_matrix" /
"artifacts" /
"planner_surface" 35 ARTIFACT_CORE_FIELDS = (
48 circuit.add_CNOT(1, 0)
53 workload_id=
"legacy_manual_u3_cnot",
56 record.update({
"case_name": surface.workload_id,
"status":
"pass"})
63 circuit.add_CNOT(1, 0)
67 workload_id=
"legacy_manual_with_noise",
70 "channel":
"local_depolarizing",
72 "after_gate_index": 1,
78 surface, metadata={
"case_kind":
"positive",
"has_attached_noise":
True}
80 record.update({
"case_name": surface.workload_id,
"status":
"pass"})
90 workload_id=
"legacy_manual_with_h",
92 except Exception
as err:
94 "case_name":
"legacy_manual_with_h",
95 "status":
"unsupported",
96 "metadata": {
"case_kind":
"negative"},
97 "error": err.to_dict()
if hasattr(err,
"to_dict")
else {
"reason": str(err)},
99 raise RuntimeError(
"Unsupported legacy gate case unexpectedly passed")
105 circuit.add_CNOT(1, 0)
109 workload_id=
"legacy_manual_bad_noise_index",
112 "channel":
"phase_damping",
114 "after_gate_index": 99,
119 except Exception
as err:
121 "case_name":
"legacy_manual_bad_noise_index",
122 "status":
"unsupported",
123 "metadata": {
"case_kind":
"negative"},
124 "error": err.to_dict()
if hasattr(err,
"to_dict")
else {
"reason": str(err)},
126 raise RuntimeError(
"Invalid legacy noise schedule case unexpectedly passed")
139 positive_cases = [case
for case
in cases
if case[
"status"] ==
"pass"]
140 negative_cases = [case
for case
in cases
if case[
"status"] ==
"unsupported"]
142 "suite_name": SUITE_NAME,
144 if len(positive_cases) == 2
and len(negative_cases) == 2
148 "total_cases": len(cases),
149 "positive_cases": len(positive_cases),
150 "negative_cases": len(negative_cases),
154 missing = [field
for field
in ARTIFACT_CORE_FIELDS
if field
not in bundle]
157 "Legacy exact lowering bundle missing required fields: {}".format(
165 output_dir.mkdir(parents=
True, exist_ok=
True)
166 output_path = output_dir / ARTIFACT_FILENAME
167 output_path.write_text(json.dumps(bundle, indent=2, sort_keys=
True) +
"\n")
171 def main(argv: list[str] |
None =
None) -> int:
172 parser = argparse.ArgumentParser(description=__doc__)
176 default=DEFAULT_OUTPUT_DIR,
177 help=
"Directory to write the legacy exact lowering bundle into.",
182 help=
"Suppress per-case console output.",
184 args = parser.parse_args(argv)
192 print(
"{case_name}: {status}".format(**case))
193 print(
"Wrote {}".format(output_path))
195 return 0
if bundle[
"status"] ==
"pass" else 1
198 if __name__ ==
"__main__":
199 raise SystemExit(
main())
def negative_case_unsupported_gate()
def write_artifact_bundle
def build_artifact_bundle
def negative_case_invalid_noise_index()
def build_planner_audit_record
def build_software_metadata()
def build_canonical_planner_surface_from_qgd_circuit
def positive_case_with_noise()
def positive_case_without_noise()