Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
claim_selection.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 from copy import deepcopy
4 from functools import lru_cache
5 import statistics
6 from collections import defaultdict
7 
8 from benchmarks.density_matrix.planner_calibration.calibration_records import (
9  build_planner_calibration_calibration_records,
10 )
11 
12 PLANNER_CALIBRATION_CLAIM_SELECTION_SCHEMA_VERSION = "phase3_planner_calibration_claim_selection_v1"
13 PLANNER_CALIBRATION_CLAIM_SELECTION_RULE = (
14  "min_median_density_aware_score_then_min_p90_then_smaller_span_budget"
15 )
16 PLANNER_CALIBRATION_CLAIM_STATUS_SUPPORTED = "supported_claim"
17 PLANNER_CALIBRATION_CLAIM_STATUS_COMPARISON = "comparison_baseline"
18 
19 
20 def _p90(values: list[float]) -> float:
21  ordered = sorted(values)
22  index = int(0.9 * (len(ordered) - 1))
23  return float(ordered[index])
24 
25 
26 def _build_candidate_summary(candidate_id: str, records: list[dict]) -> dict:
27  density_scores = [record["density_aware_score"] for record in records]
28  runtime_values = [record["runtime_ms"] for record in records]
29  planning_values = [record["planning_time_ms"] for record in records]
30  peak_rss_values = [record["peak_rss_mb"] for record in records]
31  return {
32  "candidate_id": candidate_id,
33  "planner_family": records[0]["planner_family"],
34  "planner_variant": records[0]["planner_variant"],
35  "max_partition_qubits": records[0]["max_partition_qubits"],
36  "total_cases": len(records),
37  "counted_cases": sum(record["counted_calibration_case"] for record in records),
38  "median_density_aware_score": float(statistics.median(density_scores)),
39  "p90_density_aware_score": _p90(density_scores),
40  "max_density_aware_score": float(max(density_scores)),
41  "mean_runtime_ms": float(statistics.mean(runtime_values)),
42  "mean_planning_time_ms": float(statistics.mean(planning_values)),
43  "mean_peak_rss_mb": float(statistics.mean(peak_rss_values)),
44  }
45 
46 
47 def select_supported_candidate(candidate_summaries: list[dict]) -> dict:
48  return min(
49  candidate_summaries,
50  key=lambda summary: (
51  summary["median_density_aware_score"],
52  summary["p90_density_aware_score"],
53  summary["max_partition_qubits"],
54  summary["candidate_id"],
55  ),
56  )
57 
58 
59 @lru_cache(maxsize=1)
62  grouped_records: dict[str, list[dict]] = defaultdict(list)
63  for record in records:
64  grouped_records[record["candidate_id"]].append(record)
65 
66  candidate_summaries = [
67  _build_candidate_summary(candidate_id, grouped_records[candidate_id])
68  for candidate_id in sorted(grouped_records)
69  ]
70  selected_summary = select_supported_candidate(candidate_summaries)
71  selected_candidate_id = selected_summary["candidate_id"]
72 
73  annotated_records = []
74  for record in records:
75  annotated_record = dict(record)
76  annotated_record["claim_selection_schema_version"] = (
77  PLANNER_CALIBRATION_CLAIM_SELECTION_SCHEMA_VERSION
78  )
79  annotated_record["claim_selection_rule"] = PLANNER_CALIBRATION_CLAIM_SELECTION_RULE
80  annotated_record["selected_candidate_id"] = selected_candidate_id
81  annotated_record["claim_status"] = (
82  PLANNER_CALIBRATION_CLAIM_STATUS_SUPPORTED
83  if record["candidate_id"] == selected_candidate_id
84  else PLANNER_CALIBRATION_CLAIM_STATUS_COMPARISON
85  )
86  annotated_records.append(annotated_record)
87 
88  return {
89  "schema_version": PLANNER_CALIBRATION_CLAIM_SELECTION_SCHEMA_VERSION,
90  "claim_selection_rule": PLANNER_CALIBRATION_CLAIM_SELECTION_RULE,
91  "selected_candidate": selected_summary,
92  "candidate_summaries": candidate_summaries,
93  "cases": annotated_records,
94  }
95 
96 
99 
100 
def _build_planner_calibration_claim_selection_payload_cached()
def build_planner_calibration_calibration_records()
def select_supported_candidate
def build_planner_calibration_claim_selection_payload()
def build_claim_selection_payload()