Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_correctness_evidence.py
Go to the documentation of this file.
1 from collections.abc import Callable
2 from pathlib import Path
3 import sys
4 
5 import pytest
6 
7 REPO_ROOT = Path(__file__).resolve().parents[3]
8 if str(REPO_ROOT) not in sys.path:
9  sys.path.insert(0, str(REPO_ROOT))
10 
11 from benchmarks.density_matrix.correctness_evidence.common import (
12  CORRECTNESS_EVIDENCE_CASE_SCHEMA_VERSION,
13  CORRECTNESS_EVIDENCE_NEGATIVE_RECORD_SCHEMA_VERSION,
14  CORRECTNESS_EVIDENCE_RUNTIME_CLASS_BASELINE,
15  CORRECTNESS_EVIDENCE_SUMMARY_SCHEMA_VERSION,
16  CORRECTNESS_PACKAGE_SCHEMA_VERSION,
17 )
18 from benchmarks.density_matrix.correctness_evidence.correctness_matrix_validation import (
19  build_artifact_bundle as build_correctness_matrix_bundle,
20  build_cases as build_correctness_matrix_cases,
21 )
22 from benchmarks.density_matrix.correctness_evidence.sequential_correctness_validation import (
23  build_artifact_bundle as build_sequential_correctness_bundle,
24  build_cases as build_sequential_correctness_cases,
25 )
26 from benchmarks.density_matrix.correctness_evidence.external_correctness_validation import (
27  build_artifact_bundle as build_external_correctness_bundle,
28  build_cases as build_external_correctness_cases,
29 )
30 from benchmarks.density_matrix.correctness_evidence.output_integrity_validation import (
31  build_artifact_bundle as build_output_integrity_bundle,
32  build_cases as build_output_integrity_cases,
33 )
34 from benchmarks.density_matrix.correctness_evidence.runtime_classification_validation import (
35  build_artifact_bundle as build_runtime_classification_bundle,
36  build_cases as build_runtime_classification_cases,
37 )
38 from benchmarks.density_matrix.correctness_evidence.unsupported_boundary_validation import (
39  build_artifact_bundle as build_unsupported_boundary_bundle,
40  build_cases as build_unsupported_boundary_cases,
41 )
42 from benchmarks.density_matrix.correctness_evidence.correctness_bundle_validation import (
43  build_artifact_bundle as build_correctness_package_bundle,
44 )
45 from benchmarks.density_matrix.correctness_evidence.summary_consistency_validation import (
46  build_artifact_bundle as build_summary_consistency_bundle,
47 )
49  assert_correctness_full_package_bundle,
50  assert_correctness_unsupported_boundary_bundle_core,
51 )
52 
53 _CORRECTNESS_POSITIVE_CASE_SLICES: tuple[
54  tuple[Callable[[], list[dict]], Callable[[list[dict]], dict]],
55  ...,
56 ] = (
57  (build_correctness_matrix_cases, build_correctness_matrix_bundle),
58  (build_sequential_correctness_cases, build_sequential_correctness_bundle),
59  (build_external_correctness_cases, build_external_correctness_bundle),
60  (build_output_integrity_cases, build_output_integrity_bundle),
61  (build_runtime_classification_cases, build_runtime_classification_bundle),
62 )
63 
64 _CORRECTNESS_POSITIVE_CASE_SLICE_IDS = (
65  "correctness_matrix",
66  "sequential_correctness",
67  "external_correctness",
68  "output_integrity",
69  "runtime_classification",
70 )
71 
72 
73 @pytest.mark.parametrize(
74  "build_cases_fn,build_bundle_fn",
75  _CORRECTNESS_POSITIVE_CASE_SLICES,
76  ids=list(_CORRECTNESS_POSITIVE_CASE_SLICE_IDS),
77 )
79  build_cases_fn: Callable[[], list[dict]],
80  build_bundle_fn: Callable[[list[dict]], dict],
81 ):
82  cases = build_cases_fn()
83  bundle = build_bundle_fn(cases)
84  assert bundle["status"] == "pass"
85  assert bundle["record_schema_version"] == CORRECTNESS_EVIDENCE_CASE_SCHEMA_VERSION
86 
87 
89  cases = build_correctness_matrix_cases()
90  assert len(cases) == 25
91  assert {case["candidate_id"] for case in cases} == {cases[0]["candidate_id"]}
92  assert {case["case_kind"] for case in cases} == {
93  "continuity",
94  "microcase",
95  "structured_family",
96  }
97  assert sum(case["external_reference_required"] for case in cases) == 4
98 
99 
101  bundle = build_correctness_matrix_bundle(build_correctness_matrix_cases())
102  assert bundle["summary"]["continuity_cases"] == 4
103  assert bundle["summary"]["microcases"] == 3
104  assert bundle["summary"]["structured_cases"] == 18
105 
106 
107 @pytest.fixture(scope="module")
109  return build_sequential_correctness_cases()
110 
111 
113  sequential_correctness_cases,
114 ):
115  assert len(sequential_correctness_cases) == 25
116  assert all(case["internal_reference_pass"] for case in sequential_correctness_cases)
117  assert all(case["supported_runtime_case"] for case in sequential_correctness_cases)
118  assert all(
119  case["record_schema_version"] == CORRECTNESS_EVIDENCE_CASE_SCHEMA_VERSION
120  for case in sequential_correctness_cases
121  )
122 
123 
125  bundle = build_sequential_correctness_bundle(build_sequential_correctness_cases())
126  assert bundle["summary"]["total_cases"] == 25
127  assert bundle["summary"]["internal_reference_passes"] == 25
128 
129 
131  cases = build_external_correctness_cases()
132  assert len(cases) == 4
133  assert sum(case["case_kind"] == "microcase" for case in cases) == 3
134  assert sum(case["case_kind"] == "continuity" for case in cases) == 1
135  assert all(case["external_reference_pass"] for case in cases)
136 
137 
139  bundle = build_external_correctness_bundle(build_external_correctness_cases())
140  assert bundle["summary"]["total_cases"] == 4
141  assert bundle["summary"]["external_reference_passes"] == 4
142 
143 
144 @pytest.fixture(scope="module")
146  return build_output_integrity_cases()
147 
148 
150  output_integrity_cases,
151 ):
152  assert all(case["output_integrity_pass"] for case in output_integrity_cases)
153  continuity_cases = [case for case in output_integrity_cases if case["continuity_energy_required"]]
154  assert len(continuity_cases) == 4
155  assert all(case["continuity_energy_pass"] for case in continuity_cases)
156 
157 
159  bundle = build_output_integrity_bundle(output_integrity_cases)
160  assert bundle["summary"]["total_cases"] == 25
161  assert bundle["summary"]["continuity_cases"] == 4
162  assert bundle["summary"]["continuity_energy_passes"] == 4
163 
164 
166  cases = build_runtime_classification_cases()
167  total = sum(
168  1
169  for case in cases
170  if case["runtime_path_classification"]
171  in {
172  "actually_fused",
173  "supported_but_unfused",
174  "deferred_or_unsupported_candidate",
175  CORRECTNESS_EVIDENCE_RUNTIME_CLASS_BASELINE,
176  }
177  )
178  assert total == len(cases)
179  assert all(case["supported_runtime_case"] for case in cases)
180 
181 
183  bundle = build_runtime_classification_bundle(build_runtime_classification_cases())
184  assert bundle["summary"]["total_cases"] == 25
185  assert sum(
186  bundle["summary"][key]
187  for key in (
188  "actually_fused",
189  "supported_but_unfused",
190  "deferred_or_unsupported_candidate",
191  CORRECTNESS_EVIDENCE_RUNTIME_CLASS_BASELINE,
192  )
193  ) == 25
194 
195 
197  cases = build_unsupported_boundary_cases()
198  assert len(cases) >= 3
199  assert {case["boundary_stage"] for case in cases} == {
200  "planner_entry",
201  "descriptor_generation",
202  "runtime_stage",
203  }
204  assert all(case["status"] == "unsupported" for case in cases)
205  assert all(
206  case["negative_record_schema_version"] == CORRECTNESS_EVIDENCE_NEGATIVE_RECORD_SCHEMA_VERSION
207  for case in cases
208  )
209 
210 
212  bundle = build_unsupported_boundary_bundle(build_unsupported_boundary_cases())
214  bundle,
215  negative_record_schema_version=CORRECTNESS_EVIDENCE_NEGATIVE_RECORD_SCHEMA_VERSION,
216  )
217 
218 
220  bundle = build_correctness_package_bundle()
222  bundle,
223  schema_version=CORRECTNESS_PACKAGE_SCHEMA_VERSION,
224  unsupported_case_count=len(bundle["negative_cases"]),
225  )
226 
227 
230  assert bundle["status"] == "pass"
231  assert bundle["schema_version"] == CORRECTNESS_EVIDENCE_SUMMARY_SCHEMA_VERSION
232  assert bundle["summary"]["summary_consistency_pass"] is True
233  assert bundle["summary"]["main_correctness_claim_completed"] is True
234  assert bundle["summary"]["counted_supported_cases"] == 25
def test_correctness_evidence_sequential_correctness_internal_gate_passes_full_matrix(sequential_correctness_cases)
def test_correctness_evidence_output_integrity_and_continuity_are_present(output_integrity_cases)
def test_correctness_evidence_output_integrity_bundle_summary(output_integrity_cases)