Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
correctness_evidence/records.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 from collections import Counter
4 from copy import deepcopy
5 from functools import lru_cache
6 from typing import Any
7 
8 from benchmarks.density_matrix.evidence_core import (
9  build_runtime_correctness_bridge_fields,
10  counted_supported_case,
11 )
12 from benchmarks.density_matrix.correctness_evidence.common import (
13  CORRECTNESS_EVIDENCE_CASE_SCHEMA_VERSION,
14  CORRECTNESS_EVIDENCE_NEGATIVE_RECORD_SCHEMA_VERSION,
15  CORRECTNESS_EVIDENCE_PHASE31_CASE_SCHEMA_VERSION,
16  CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_EXTERNAL,
17  CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_INTERNAL,
18  CORRECTNESS_EVIDENCE_RUNTIME_CLASS_BASELINE,
19  build_selected_candidate,
20  build_validation_slice,
21 )
22 from benchmarks.density_matrix.correctness_evidence.case_selection import (
23  CORRECTNESS_EVIDENCE_CASE_KIND_CONTINUITY,
24  CORRECTNESS_EVIDENCE_CASE_KIND_MICROCASE,
25  _build_phase31_correctness_evidence_case_contexts_cached,
26  build_correctness_evidence_case_contexts,
27  build_phase31_correctness_evidence_case_contexts,
28 )
29 from benchmarks.density_matrix.correctness_evidence.phase31_channel_invariants import (
30  build_strict_microcase_channel_invariants_slice,
31 )
32 from benchmarks.density_matrix.partitioned_runtime.common import (
33  build_density_comparison_metrics,
34  execute_fused_with_reference,
35 )
36 from benchmarks.density_matrix.planner_surface.unsupported_descriptor_validation import (
37  SUITE_NAME as UNSUPPORTED_DESCRIPTOR_ORIGIN_SUITE_NAME,
38  build_unsupported_descriptor_cases as build_descriptor_unsupported_cases,
39 )
40 from benchmarks.density_matrix.planner_surface.unsupported_planner_validation import (
41  SUITE_NAME as UNSUPPORTED_PLANNER_ORIGIN_SUITE_NAME,
42  build_cases as build_planner_unsupported_cases,
43 )
44 from benchmarks.density_matrix.planner_calibration.claim_selection import (
45  PLANNER_CALIBRATION_CLAIM_STATUS_SUPPORTED,
46 )
47 from benchmarks.density_matrix.partitioned_runtime.unsupported_runtime_validation import (
48  SUITE_NAME as UNSUPPORTED_RUNTIME_ORIGIN_SUITE_NAME,
49  build_cases as build_runtime_unsupported_cases,
50 )
51 from squander.partitioning.noisy_runtime import (
52  PHASE3_FUSION_CLASS_DEFERRED,
53  PHASE3_FUSION_CLASS_FUSED,
54  PHASE3_FUSION_CLASS_SUPPORTED_UNFUSED,
55  execute_partitioned_density_channel_native,
56  execute_partitioned_density_channel_native_hybrid,
57  execute_sequential_density_reference,
58 )
59 
60 
61 def _base_case_record(metadata: dict[str, Any], descriptor_set) -> dict[str, Any]:
62  selected_candidate = build_selected_candidate()
63  external_reference_required = bool(metadata["external_reference_required"])
64  return {
65  "record_schema_version": CORRECTNESS_EVIDENCE_CASE_SCHEMA_VERSION,
66  "candidate_schema_version": selected_candidate["candidate_schema_version"],
67  "claim_status": PLANNER_CALIBRATION_CLAIM_STATUS_SUPPORTED,
68  "case_name": metadata["case_name"],
69  "case_kind": metadata["case_kind"],
70  "candidate_id": metadata["candidate_id"],
71  "planner_family": metadata["planner_family"],
72  "planner_variant": metadata["planner_variant"],
73  "planner_settings": dict(metadata["planner_settings"]),
74  "max_partition_qubits": metadata["max_partition_qubits"],
75  "planner_calibration_selected_candidate_id": metadata["planner_calibration_selected_candidate_id"],
76  "planner_calibration_claim_selection_schema_version": metadata[
77  "planner_calibration_claim_selection_schema_version"
78  ],
79  "planner_calibration_claim_selection_rule": metadata["planner_calibration_claim_selection_rule"],
80  "requested_mode": descriptor_set.requested_mode,
81  "source_type": descriptor_set.source_type,
82  "workload_id": descriptor_set.workload_id,
83  "qbit_num": descriptor_set.qbit_num,
84  "parameter_count": descriptor_set.parameter_count,
85  "family_name": metadata["family_name"],
86  "noise_pattern": metadata["noise_pattern"],
87  "seed": metadata["seed"],
88  "topology": metadata["topology"],
89  "validation_slice": build_validation_slice(
90  external_reference_required=external_reference_required
91  ),
92  "external_reference_required": external_reference_required,
93  "reference_backend_internal": CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_INTERNAL,
94  "reference_backend_external": (
95  CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_EXTERNAL if external_reference_required else None
96  ),
97  "correctness_matrix_pass": True,
98  }
99 
100 
101 def _runtime_classification(runtime_result) -> str:
102  if runtime_result.actual_fused_execution:
103  return PHASE3_FUSION_CLASS_FUSED
104  if runtime_result.supported_unfused_region_count > 0:
105  return PHASE3_FUSION_CLASS_SUPPORTED_UNFUSED
106  if runtime_result.deferred_region_count > 0:
107  return PHASE3_FUSION_CLASS_DEFERRED
108  return CORRECTNESS_EVIDENCE_RUNTIME_CLASS_BASELINE
109 
110 
111 def _phase31_hybrid_partition_route_summary(runtime_result) -> dict[str, Any]:
112  classes = [rec.partition_runtime_class for rec in runtime_result.partitions]
113  reasons = [rec.partition_route_reason for rec in runtime_result.partitions]
114  return {
115  "partition_count": len(runtime_result.partitions),
116  "runtime_class_counts": dict(Counter(c for c in classes if c is not None)),
117  "route_reason_counts": dict(Counter(r for r in reasons if r is not None)),
118  }
119 
120 
121 def _phase31_base_case_record(metadata: dict[str, Any], descriptor_set) -> dict[str, Any]:
122  selected_candidate = build_selected_candidate()
123  external_reference_required = bool(metadata["external_reference_required"])
124  return {
125  "record_schema_version": CORRECTNESS_EVIDENCE_PHASE31_CASE_SCHEMA_VERSION,
126  "candidate_schema_version": selected_candidate["candidate_schema_version"],
127  "claim_status": PLANNER_CALIBRATION_CLAIM_STATUS_SUPPORTED,
128  "case_name": metadata["case_name"],
129  "case_kind": metadata["case_kind"],
130  "candidate_id": metadata["candidate_id"],
131  "planner_family": metadata["planner_family"],
132  "planner_variant": metadata["planner_variant"],
133  "planner_settings": dict(metadata["planner_settings"]),
134  "max_partition_qubits": metadata["max_partition_qubits"],
135  "planner_calibration_selected_candidate_id": metadata["planner_calibration_selected_candidate_id"],
136  "planner_calibration_claim_selection_schema_version": metadata[
137  "planner_calibration_claim_selection_schema_version"
138  ],
139  "planner_calibration_claim_selection_rule": metadata["planner_calibration_claim_selection_rule"],
140  "requested_mode": descriptor_set.requested_mode,
141  "source_type": descriptor_set.source_type,
142  "workload_id": descriptor_set.workload_id,
143  "qbit_num": descriptor_set.qbit_num,
144  "parameter_count": descriptor_set.parameter_count,
145  "family_name": metadata["family_name"],
146  "noise_pattern": metadata["noise_pattern"],
147  "seed": metadata["seed"],
148  "topology": metadata["topology"],
149  "validation_slice": build_validation_slice(
150  external_reference_required=external_reference_required
151  ),
152  "external_reference_required": external_reference_required,
153  "reference_backend_internal": CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_INTERNAL,
154  "reference_backend_external": (
155  CORRECTNESS_EVIDENCE_REFERENCE_BACKEND_EXTERNAL if external_reference_required else None
156  ),
157  "correctness_matrix_pass": True,
158  }
159 
160 
161 def build_correctness_evidence_positive_record(case_context) -> dict[str, Any]:
162  record = _base_case_record(case_context.metadata, case_context.descriptor_set)
163 
164  runtime_result, reference_density, density_metrics = execute_fused_with_reference(
165  case_context.descriptor_set, case_context.parameters
166  )
167  runtime_payload = runtime_result.to_dict(include_density_matrix=False)
168 
169  external_reference_required = bool(record["external_reference_required"])
170  record.update(
172  case_context,
173  runtime_result,
174  reference_density,
175  density_metrics,
176  external_reference_required=external_reference_required,
177  runtime_payload=runtime_payload,
178  )
179  )
180  record.update(
181  {
182  "runtime_path_classification": _runtime_classification(runtime_result),
183  "partitions": runtime_payload["partitions"],
184  "fused_regions": runtime_payload["fused_regions"],
185  "exact_output": runtime_payload["exact_output"],
186  }
187  )
188  return record
189 
190 
191 @lru_cache(maxsize=1)
192 def _build_correctness_evidence_positive_records_cached() -> tuple[dict[str, Any], ...]:
193  return tuple(
195  for case_context in build_correctness_evidence_case_contexts()
196  )
197 
198 
199 def build_correctness_evidence_positive_records() -> list[dict[str, Any]]:
201 
202 
203 def build_phase31_correctness_evidence_positive_record(case_context) -> dict[str, Any]:
204  """Positive record via strict (microcases) or hybrid (continuity) Phase 3.1 paths."""
205  record = _phase31_base_case_record(case_context.metadata, case_context.descriptor_set)
206  external_reference_required = bool(record["external_reference_required"])
207  case_kind = case_context.metadata["case_kind"]
208  if case_kind == CORRECTNESS_EVIDENCE_CASE_KIND_MICROCASE:
210  case_context.descriptor_set, case_context.parameters
211  )
212  elif case_kind == CORRECTNESS_EVIDENCE_CASE_KIND_CONTINUITY:
214  case_context.descriptor_set, case_context.parameters
215  )
216  else:
217  raise ValueError("Phase 3.1 bounded package only supports microcase and continuity rows")
218 
219  reference_density = execute_sequential_density_reference(
220  case_context.descriptor_set, case_context.parameters
221  )
222  density_metrics = build_density_comparison_metrics(
223  runtime_result.density_matrix, reference_density
224  )
225  runtime_payload = runtime_result.to_dict(include_density_matrix=False)
226  record.update(
228  case_context,
229  runtime_result,
230  reference_density,
231  density_metrics,
232  external_reference_required=external_reference_required,
233  runtime_payload=runtime_payload,
234  )
235  )
236  record.update(
237  {
238  "runtime_path_classification": _runtime_classification(runtime_result),
239  "partitions": runtime_payload["partitions"],
240  "fused_regions": runtime_payload["fused_regions"],
241  "exact_output": runtime_payload["exact_output"],
242  "runtime_class": runtime_payload["runtime_path"],
243  "claim_surface_id": case_context.metadata["claim_surface_id"],
244  "representation_primary": case_context.metadata["representation_primary"],
245  "fused_block_support_qbits": case_context.metadata.get("fused_block_support_qbits"),
246  "contains_noise": case_context.metadata["contains_noise"],
247  "counted_phase31_case": case_context.metadata["counted_phase31_case"],
248  }
249  )
250  if case_kind == CORRECTNESS_EVIDENCE_CASE_KIND_MICROCASE:
251  record["channel_invariants"] = build_strict_microcase_channel_invariants_slice(
252  case_context.descriptor_set, case_context.parameters
253  )
254  record["partition_route_summary"] = None
255  else:
256  record["channel_invariants"] = None
257  record["partition_route_summary"] = _phase31_hybrid_partition_route_summary(
258  runtime_result
259  )
260  return record
261 
262 
263 @lru_cache(maxsize=1)
265  return tuple(
268  )
269 
270 
273 
274 
276  case: dict[str, Any], *, boundary_stage: str, origin_suite_name: str
277 ) -> dict[str, Any]:
278  return {
279  "negative_record_schema_version": CORRECTNESS_EVIDENCE_NEGATIVE_RECORD_SCHEMA_VERSION,
280  "origin_suite_name": origin_suite_name,
281  "boundary_stage": boundary_stage,
282  "case_name": case.get("case_name"),
283  "status": case.get("status"),
284  "unsupported_category": case.get("unsupported_category"),
285  "first_unsupported_condition": case.get("first_unsupported_condition"),
286  "failure_stage": case.get("failure_stage"),
287  "source_type": case.get("source_type"),
288  "requested_mode": case.get("requested_mode"),
289  "workload_id": case.get("workload_id"),
290  "runtime_path": case.get("runtime_path"),
291  "supported_case_recorded": bool(
292  case.get("supported_partitioned_case_recorded")
293  or case.get("supported_descriptor_case_recorded")
294  or case.get("supported_runtime_case_recorded")
295  ),
296  "reason": case.get("reason"),
297  "planner_calibration_selected_candidate_id": build_selected_candidate()["candidate_id"],
298  }
299 
300 
301 @lru_cache(maxsize=1)
302 def _build_correctness_evidence_negative_records_cached() -> tuple[dict[str, Any], ...]:
303  records: list[dict[str, Any]] = []
304  records.extend(
306  case,
307  boundary_stage="planner_entry",
308  origin_suite_name=UNSUPPORTED_PLANNER_ORIGIN_SUITE_NAME,
309  )
310  for case in build_planner_unsupported_cases()
311  )
312  records.extend(
314  case,
315  boundary_stage="descriptor_generation",
316  origin_suite_name=UNSUPPORTED_DESCRIPTOR_ORIGIN_SUITE_NAME,
317  )
318  for case in build_descriptor_unsupported_cases()
319  )
320  records.extend(
322  case,
323  boundary_stage="runtime_stage",
324  origin_suite_name=UNSUPPORTED_RUNTIME_ORIGIN_SUITE_NAME,
325  )
326  for case in build_runtime_unsupported_cases()
327  )
328  return tuple(records)
329 
330 
331 def build_correctness_evidence_negative_records() -> list[dict[str, Any]]:
333 
334 
335 def build_positive_records() -> list[dict[str, Any]]:
337 
338 
339 def build_negative_records() -> list[dict[str, Any]]:
341 
342 
343 def correctness_evidence_counted_supported_case(record: dict[str, Any]) -> bool:
344  return counted_supported_case(record)
def _build_correctness_evidence_negative_records_cached()
def _runtime_classification(runtime_result)
def build_phase31_correctness_evidence_positive_records()
def execute_partitioned_density_channel_native_hybrid
def build_correctness_evidence_positive_record(case_context)
def execute_sequential_density_reference
Execute a sequential density reference.
def build_correctness_evidence_positive_records()
def _build_phase31_correctness_evidence_positive_records_cached()
def execute_partitioned_density_channel_native
def _phase31_hybrid_partition_route_summary(runtime_result)
def correctness_evidence_counted_supported_case
def build_density_comparison_metrics
def build_phase31_correctness_evidence_positive_record(case_context)
def _build_correctness_evidence_positive_records_cached()
def build_runtime_correctness_bridge_fields
def build_correctness_evidence_negative_records()
def counted_supported_case