Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
correctness_matrix_validation.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 """Correctness matrix validation for density-matrix correctness evidence.
3 
4 Builds the mandatory case inventory for the selected supported calibration
5 candidate and records stable case identity plus validation-slice membership.
6 
7 Run with:
8  python benchmarks/density_matrix/correctness_evidence/correctness_matrix_validation.py
9 """
10 
11 from __future__ import annotations
12 
13 from copy import deepcopy
14 from functools import lru_cache
15 from pathlib import Path
16 
17 from benchmarks.density_matrix.correctness_evidence.case_selection import (
18  CORRECTNESS_EVIDENCE_CASE_KIND_CONTINUITY,
19  CORRECTNESS_EVIDENCE_CASE_KIND_MICROCASE,
20  CORRECTNESS_EVIDENCE_CASE_KIND_STRUCTURED,
21  build_correctness_evidence_case_contexts,
22 )
23 from benchmarks.density_matrix.correctness_evidence.common import (
24  correctness_evidence_output_dir,
25 )
26 from benchmarks.density_matrix.correctness_evidence.validation_support import (
27  assemble_positive_case_bundle,
28 )
29 from benchmarks.density_matrix.validation_scaffold import (
30  require_bundle_fields,
31  run_case_slice_cli,
32 )
33 
34 SUITE_NAME = "correctness_evidence_correctness_matrix"
35 ARTIFACT_FILENAME = "correctness_matrix_bundle.json"
36 DEFAULT_OUTPUT_DIR = correctness_evidence_output_dir("correctness_matrix")
37 ARTIFACT_CORE_FIELDS = (
38  "suite_name",
39  "status",
40  "record_schema_version",
41  "software",
42  "selected_candidate",
43  "summary",
44  "cases",
45 )
46 
47 
48 @lru_cache(maxsize=1)
49 def _build_correctness_matrix_cases_cached() -> tuple[dict, ...]:
50  return tuple(dict(case_context.metadata) for case_context in build_correctness_evidence_case_contexts())
51 
52 
53 def build_cases() -> list[dict]:
54  return deepcopy(list(_build_correctness_matrix_cases_cached()))
55 
56 
57 def build_artifact_bundle(cases: list[dict]) -> dict:
58  continuity_cases = sum(case["case_kind"] == CORRECTNESS_EVIDENCE_CASE_KIND_CONTINUITY for case in cases)
59  microcases = sum(case["case_kind"] == CORRECTNESS_EVIDENCE_CASE_KIND_MICROCASE for case in cases)
60  structured_cases = sum(case["case_kind"] == CORRECTNESS_EVIDENCE_CASE_KIND_STRUCTURED for case in cases)
61  external_slice_cases = sum(case["external_reference_required"] for case in cases)
62  status = (
63  "pass"
64  if continuity_cases == 4
65  and microcases == 3
66  and structured_cases == 18
67  and external_slice_cases == 4
68  else "fail"
69  )
70  summary = {
71  "total_cases": len(cases),
72  "continuity_cases": continuity_cases,
73  "microcases": microcases,
74  "structured_cases": structured_cases,
75  "external_slice_cases": external_slice_cases,
76  "internal_only_cases": sum(case["validation_slice"] == "internal_only" for case in cases),
77  "internal_plus_external_cases": sum(
78  case["validation_slice"] == "internal_plus_external" for case in cases
79  ),
80  }
81  bundle = assemble_positive_case_bundle(SUITE_NAME, status, summary, cases)
82  require_bundle_fields(bundle, ARTIFACT_CORE_FIELDS, "Correctness matrix bundle")
83  return bundle
84 
85 
86 def main(argv: list[str] | None = None) -> int:
87  return run_case_slice_cli(
88  argv,
89  build_cases=build_cases,
90  build_artifact_bundle=build_artifact_bundle,
91  artifact_filename=ARTIFACT_FILENAME,
92  default_output_dir=DEFAULT_OUTPUT_DIR,
93  description=__doc__ or "",
94  output_dir_help="Directory to write the correctness matrix bundle into.",
95  quiet_report=lambda b: print(
96  "selected_candidate={candidate_id}, total_cases={total_cases}".format(
97  candidate_id=b["selected_candidate"]["candidate_id"],
98  total_cases=b["summary"]["total_cases"],
99  )
100  ),
101  )
102 
103 
104 if __name__ == "__main__":
105  raise SystemExit(main())