Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_partitioned_channel_native_phase31_hybrid_slice.py
Go to the documentation of this file.
1 """Phase 3.1 hybrid whole-workload path: runtime (P31-S07-E01) and counted continuity (P31-S08 + P31-S10).
2 
3 **P31-S07-E01 (runtime / routing):** ``phase31_channel_native_hybrid``,
4 ``execute_partitioned_density_channel_native_hybrid``, partition-level route
5 reasons, unsupported-by-both failure, and structured smoke.
6 
7 **Counted hybrid continuity gates (Task 3):** ``P31-S08-E01`` —
8 ``phase2_xxz_hea_q4_continuity``; ``P31-S10-E01`` — ``phase2_xxz_hea_q6_continuity``.
9 Each asserts full-density exactness vs sequential oracle and a frozen aggregated
10 route summary (partition count, runtime-class counts, route-reason counts).
11 
12 **Correctness package (``P31-S10-E02``):** Stage-A sibling builders and
13 ``phase31_validation_pipeline.py`` emit the bounded six-case package under
14 ``artifacts/correctness_evidence/phase31_stage_a/`` (default Phase 3 pipeline
15 unchanged).
16 
17 **Still deferred:** counted structured performance matrix (``P31-ADR-010``);
18 default-pipeline Stage-B switch; publication closure.
19 
20 Strict motif-proof tests stay in ``test_partitioned_channel_native_phase31_*slice.py``.
21 """
22 
23 from __future__ import annotations
24 
25 from collections import Counter
26 from pathlib import Path
27 import sys
28 from typing import Any
29 
30 import pytest
31 
32 REPO_ROOT = Path(__file__).resolve().parents[2]
33 if str(REPO_ROOT) not in sys.path:
34  sys.path.insert(0, str(REPO_ROOT))
35 
36 from squander.partitioning.noisy_planner import (
37  build_canonical_planner_surface_from_operation_specs,
38  build_partition_descriptor_set,
39  build_phase3_continuity_partition_descriptor_set,
40 )
41 from squander.partitioning.noisy_runtime import (
42  PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID,
43  NoisyRuntimeExecutionResult,
44  execute_partitioned_density_channel_native_hybrid,
45  execute_sequential_density_reference,
46 )
47 from squander.partitioning.noisy_validation_errors import NoisyRuntimeValidationError
48 from tests.partitioning.fixtures.continuity import build_phase2_continuity_vqe
50  PHASE3_RUNTIME_DENSITY_TOL,
51  build_density_comparison_metrics,
52  build_initial_parameters,
53 )
55  _noise_value,
56  build_phase31_structured_descriptor_set,
57 )
58 
59 # Frozen P31-ADR-012 hybrid route-reason vocabulary (subset used by classifier).
60 _FROZEN_HYBRID_ROUTE_REASONS = frozenset(
61  {
62  "eligible_channel_native_motif",
63  "pure_unitary_partition",
64  "channel_native_noise_presence",
65  "channel_native_qubit_span",
66  "channel_native_support_surface",
67  }
68 )
69 
70 # Counted hybrid continuity anchor — aggregated route summary for current planner
71 # partitioning (update deliberately if partitioning changes).
72 _COUNTED_HYBRID_CONTINUITY_Q4_WORKLOAD_ID = "phase2_xxz_hea_q4_continuity"
73 _EXPECTED_Q4_HYBRID_PARTITION_COUNT = 5
74 _EXPECTED_Q4_RUNTIME_CLASS_COUNTS = {
75  "phase31_channel_native": 2,
76  "phase3_unitary_island_fused": 3,
77 }
78 _EXPECTED_Q4_ROUTE_REASON_COUNTS = {
79  "eligible_channel_native_motif": 2,
80  "pure_unitary_partition": 3,
81 }
82 
83 _COUNTED_HYBRID_CONTINUITY_Q6_WORKLOAD_ID = "phase2_xxz_hea_q6_continuity"
84 _EXPECTED_Q6_HYBRID_PARTITION_COUNT = 7
85 _EXPECTED_Q6_RUNTIME_CLASS_COUNTS = {
86  "phase31_channel_native": 2,
87  "phase3_unitary_island_fused": 5,
88 }
89 _EXPECTED_Q6_ROUTE_REASON_COUNTS = {
90  "eligible_channel_native_motif": 2,
91  "pure_unitary_partition": 5,
92 }
93 
94 
96  result: NoisyRuntimeExecutionResult,
97 ) -> dict[str, Any]:
98  """Aggregate hybrid route metadata from ``result.partitions`` (test-local)."""
99  classes = [rec.partition_runtime_class for rec in result.partitions]
100  reasons = [rec.partition_route_reason for rec in result.partitions]
101  return {
102  "partition_count": len(result.partitions),
103  "runtime_class_counts": dict(Counter(c for c in classes if c is not None)),
104  "route_reason_counts": dict(Counter(r for r in reasons if r is not None)),
105  }
106 
107 
108 # --- P31-S08-E01: counted hybrid continuity correctness gate ---
109 
110 
112  vqe, _, _ = build_phase2_continuity_vqe(4)
114  assert descriptor_set.workload_id == _COUNTED_HYBRID_CONTINUITY_Q4_WORKLOAD_ID
115 
116  parameters = build_initial_parameters(descriptor_set.parameter_count)
117  hybrid = execute_partitioned_density_channel_native_hybrid(descriptor_set, parameters)
118  reference = execute_sequential_density_reference(descriptor_set, parameters)
119  metrics = build_density_comparison_metrics(hybrid.density_matrix, reference)
120 
121  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
122  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
123  assert hybrid.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
124  assert hybrid.rho_is_valid is True
125  assert hybrid.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID
126  assert hybrid.requested_runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID
127 
128  summary = _hybrid_partition_route_summary(hybrid)
129  assert summary["partition_count"] == _EXPECTED_Q4_HYBRID_PARTITION_COUNT
130  assert summary["runtime_class_counts"] == _EXPECTED_Q4_RUNTIME_CLASS_COUNTS
131  assert summary["route_reason_counts"] == _EXPECTED_Q4_ROUTE_REASON_COUNTS
132 
133  classes = [rec.partition_runtime_class for rec in hybrid.partitions]
134  assert any(c == "phase31_channel_native" for c in classes)
135  assert any(
136  c in ("phase3_unitary_island_fused", "phase3_supported_unfused") for c in classes
137  )
138 
139  reasons = [rec.partition_route_reason for rec in hybrid.partitions]
140  assert all(r in _FROZEN_HYBRID_ROUTE_REASONS for r in reasons)
141 
142 
143 # --- P31-S10-E01: second counted hybrid continuity anchor (q6) ---
144 
145 
147  vqe, _, _ = build_phase2_continuity_vqe(6)
149  assert descriptor_set.workload_id == _COUNTED_HYBRID_CONTINUITY_Q6_WORKLOAD_ID
150 
151  parameters = build_initial_parameters(descriptor_set.parameter_count)
153  descriptor_set, parameters
154  )
155  reference = execute_sequential_density_reference(descriptor_set, parameters)
156  metrics = build_density_comparison_metrics(hybrid.density_matrix, reference)
157 
158  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
159  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
160  assert hybrid.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
161  assert hybrid.rho_is_valid is True
162  assert hybrid.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID
163  assert hybrid.requested_runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID
164 
165  summary = _hybrid_partition_route_summary(hybrid)
166  assert summary["partition_count"] == _EXPECTED_Q6_HYBRID_PARTITION_COUNT
167  assert summary["runtime_class_counts"] == _EXPECTED_Q6_RUNTIME_CLASS_COUNTS
168  assert summary["route_reason_counts"] == _EXPECTED_Q6_ROUTE_REASON_COUNTS
169 
170  classes = [rec.partition_runtime_class for rec in hybrid.partitions]
171  assert any(c == "phase31_channel_native" for c in classes)
172  assert any(
173  c in ("phase3_unitary_island_fused", "phase3_supported_unfused") for c in classes
174  )
175 
176  reasons = [rec.partition_route_reason for rec in hybrid.partitions]
177  assert all(r in _FROZEN_HYBRID_ROUTE_REASONS for r in reasons)
178 
179 
180 # --- P31-S07-E01: unsupported-by-both must not be absorbed by hybrid routing ---
181 
182 
185  qbit_num=2,
186  source_type="microcase_builder",
187  workload_id="hybrid_negative_rx_relaxed_surface",
188  operation_specs=[
189  {
190  "kind": "gate",
191  "name": "RX",
192  "target_qbit": 0,
193  "param_count": 1,
194  },
195  {
196  "kind": "noise",
197  "name": "phase_damping",
198  "target_qbit": 0,
199  "source_gate_index": 0,
200  "fixed_value": _noise_value("phase_damping"),
201  "param_count": 0,
202  },
203  ],
204  strict_phase3_support=False,
205  )
206  ds = build_partition_descriptor_set(surface)
207  parameters = build_initial_parameters(ds.parameter_count)
208  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
210  assert excinfo.value.first_unsupported_condition == "gate_name"
211 
212 
213 # --- Non-counted support smoke for later performance slice (P31-S09); not Task 3 closure ---
214 
215 
218  "phase31_pair_repeat",
219  qbit_num=8,
220  noise_pattern="dense",
221  seed=20260318,
222  )
223  parameters = build_initial_parameters(descriptor_set.parameter_count)
224  hybrid = execute_partitioned_density_channel_native_hybrid(descriptor_set, parameters)
225  reference = execute_sequential_density_reference(descriptor_set, parameters)
226  metrics = build_density_comparison_metrics(hybrid.density_matrix, reference)
227 
228  assert hybrid.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE_HYBRID
229  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
230  reasons = [rec.partition_route_reason for rec in hybrid.partitions]
231  assert all(r in _FROZEN_HYBRID_ROUTE_REASONS for r in reasons)
def execute_partitioned_density_channel_native_hybrid
def execute_sequential_density_reference
Execute a sequential density reference.
def build_phase2_continuity_vqe
def build_phase3_continuity_partition_descriptor_set
def build_density_comparison_metrics