Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_partitioned_channel_native_phase31_second_slice.py
Go to the documentation of this file.
1 """Phase 3.1 channel-native second slice: helper substrate, public runtime, correctness gate, boundaries.
2 
3 **Helper / E01–E02 substrate:** imports private helpers from
4 ``noisy_runtime_channel_native`` for bundle shape, invariants, support-aware
5 application, and (``P31-S04-E02``) CNOT / 2-local Kraus composition vs. the
6 sequential reference.
7 
8 **Public runtime (``P31-S05-E01``):** ``execute_partitioned_density_channel_native``
9 admits bounded mixed motifs per partition via local-support validation in
10 ``_validate_whole_partition_motif`` (no whole-workload single-qubit gate).
11 
12 **Public positives (``P31-S05``):** integration tests assert runtime path,
13 fused-region audit fields, and density vs. sequential reference.
14 
15 **Public boundary (``P31-S05-E02``):** negative matrix for span, noise presence,
16 and non-channel-native gates on surfaces built with
17 ``strict_phase3_support=False``.
18 
19 **Public correctness gates:** counted 2q closure includes ``P31-S06-E01``
20 (``phase31_microcase_2q_cnot_local_noise_pair``) and ``P31-S10-E01`` (remaining
21 strict counted microcases below) vs. sequential oracle with Frobenius/max-abs,
22 ``trace_deviation``, ``rho_is_valid``; Kraus bundle invariant checks on fused 2q
23 blocks. Non-counted ``phase31_local_support_q4_spectator_embedding_smoke`` remains
24 a larger-workload smoke.
25 
26 **Hybrid continuity** (``phase2_xxz_hea_q4_continuity``, ``phase2_xxz_hea_q6_continuity``)
27 lives in ``test_partitioned_channel_native_phase31_hybrid_slice.py``.
28 
29 **Still deferred:** Qiskit Aer on the frozen external slice (``P31-ADR-011``);
30 versioned ``correctness_evidence`` / ``channel_invariants`` / route-summary
31 packaging (``P31-S10-E02``, ``P31-ADR-013``); counted structured performance cases
32 (``P31-ADR-010``); Task 4 performance matrix.
33 
34 The first-slice file ``test_partitioned_channel_native_phase31_slice.py`` remains
35 the unchanged 1q public regression anchor.
36 """
37 
38 from __future__ import annotations
39 
40 from pathlib import Path
41 import sys
42 
43 import numpy as np
44 import pytest
45 
46 REPO_ROOT = Path(__file__).resolve().parents[2]
47 if str(REPO_ROOT) not in sys.path:
48  sys.path.insert(0, str(REPO_ROOT))
49 
50 from squander.density_matrix import DensityMatrix
51 from squander.partitioning.noisy_planner import (
52  build_canonical_planner_surface_from_operation_specs,
53  build_partition_descriptor_set,
54 )
55 from squander.partitioning.noisy_runtime import (
56  PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF,
57  PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
58  execute_partitioned_density_channel_native,
59  execute_sequential_density_reference,
60 )
61 from squander.partitioning.noisy_runtime_channel_native import (
62  _apply_kraus_bundle,
63  _check_kraus_bundle_invariants,
64  _compose_kraus_bundles,
65  _identity_kraus_bundle_for_support_qubit_count,
66  _kraus_bundle_phase_damping,
67  _member_to_kraus_bundle,
68 )
69 from squander.partitioning.noisy_runtime_core import (
70  _build_partition_parameter_vector,
71  _segment_parameter_vector,
72  execute_sequential_density_reference,
73 )
74 from squander.partitioning.noisy_runtime_fusion import (
75  _embed_cnot_gate,
76  _embed_single_qubit_gate,
77  _kernel_indices_for_fused_cnot,
78 )
79 from squander.partitioning.noisy_types import (
80  NoisyPartitionDescriptor,
81  NoisyPartitionDescriptorSet,
82 )
83 from squander.partitioning.noisy_validation_errors import NoisyRuntimeValidationError
85  PHASE3_RUNTIME_DENSITY_TOL,
86  build_density_comparison_metrics,
87  build_initial_parameters,
88 )
90  _cnot,
91  _noise_value,
92  _u3,
93  build_microcase_surface,
94  build_phase31_microcase_descriptor_set,
95 )
96 
97 
100  "phase31_microcase_1q_u3_local_noise_chain"
101  )
102 
103 
105  descriptor_set: NoisyPartitionDescriptorSet,
106  wanted_locals: tuple[int, ...] = (0, 1),
107 ) -> NoisyPartitionDescriptor:
108  for partition in descriptor_set.partitions:
109  if partition.local_to_global_qbits == wanted_locals:
110  return partition
111  raise AssertionError(
112  "no partition with local_to_global_qbits == {}".format(wanted_locals)
113  )
114 
115 
116 def _local_qbit_to_kernel_index(local_support: tuple[int, ...]) -> dict[int, int]:
117  return {lq: idx for idx, lq in enumerate(local_support)}
118 
119 
121  steps: list[np.ndarray],
122  *,
123  descriptor_set: NoisyPartitionDescriptorSet,
124  support_qubit_count: int,
125  runtime_path: str,
126 ) -> np.ndarray:
128  support_qubit_count,
129  descriptor_set=descriptor_set,
130  runtime_path=runtime_path,
131  )
132  for step in steps:
134  acc,
135  step,
136  descriptor_set=descriptor_set,
137  runtime_path=runtime_path,
138  )
139  return acc
140 
141 
143  descriptor_set = _descriptor_set_for_invariant_tests()
144  bundle = np.array([np.eye(4, dtype=np.complex128)])
146  bundle,
147  descriptor_set=descriptor_set,
148  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
149  )
150 
151 
153  descriptor_set = _descriptor_set_for_invariant_tests()
154  u1 = np.eye(2, dtype=np.complex128)
155  u2 = np.array([[0, 1], [1, 0]], dtype=np.complex128)
156  bundle = np.array([np.kron(u1, u2)])
158  bundle,
159  descriptor_set=descriptor_set,
160  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
161  )
162 
163 
165  descriptor_set = _descriptor_set_for_invariant_tests()
166  bundle = np.array([2.0 * np.eye(4, dtype=np.complex128)])
167  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
169  bundle,
170  descriptor_set=descriptor_set,
171  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
172  )
173  assert excinfo.value.first_unsupported_condition == "channel_native_invariant_failure"
174 
175 
177  descriptor_set = _descriptor_set_for_invariant_tests()
178  bell = np.zeros((4, 4), dtype=np.complex128)
179  bell[0, 0] = bell[3, 3] = bell[0, 3] = bell[3, 0] = 0.5
180  rho = DensityMatrix.from_numpy(bell)
181  bundle = _kraus_bundle_phase_damping(0.25)
182  rho_np = np.asarray(rho.to_numpy(), dtype=np.complex128)
183  ref_out = np.zeros((4, 4), dtype=np.complex128)
184  for k in range(bundle.shape[0]):
185  k_full = _embed_single_qubit_gate(
186  bundle[k], total_kernel_qbits=2, kernel_target_qbit=0
187  )
188  ref_out += k_full @ rho_np @ k_full.conj().T
189  out = _apply_kraus_bundle(
190  bundle,
191  rho,
192  qbit_num=2,
193  local_support=(0,),
194  global_target_qbits=(0,),
195  descriptor_set=descriptor_set,
196  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
197  )
198  got = np.asarray(out.to_numpy(), dtype=np.complex128)
199  assert float(np.linalg.norm(got - ref_out, ord="fro")) <= PHASE3_RUNTIME_DENSITY_TOL
200 
201 
203  descriptor_set = _descriptor_set_for_invariant_tests()
204  rng = np.random.default_rng(0)
205  psi = rng.normal(size=4) + 1j * rng.normal(size=4)
206  psi = psi / np.linalg.norm(psi)
207  rho = DensityMatrix.from_numpy(np.outer(psi, psi.conj()).astype(np.complex128))
208  bundle = np.array([np.eye(4, dtype=np.complex128)])
209  out = _apply_kraus_bundle(
210  bundle,
211  rho,
212  qbit_num=2,
213  local_support=(0, 1),
214  global_target_qbits=(0, 1),
215  descriptor_set=descriptor_set,
216  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
217  )
218  assert float(
219  np.linalg.norm(
220  np.asarray(out.to_numpy()) - np.asarray(rho.to_numpy()), ord="fro"
221  )
222  ) <= PHASE3_RUNTIME_DENSITY_TOL
223 
224 
226  """Kraus matrix for U0 on global 0 and U1 on global 1 matches product embed.
227 
228  ``_embed_two_qubit_operator_on_globals`` uses subsystem index ``b_g0 + 2*b_g1``
229  (g0 LSB). NumPy ``np.kron(A, B)`` places the first factor on the more
230  significant index bit, so the 4×4 operator is ``np.kron(U1, U0)``.
231  """
232  descriptor_set = _descriptor_set_for_invariant_tests()
233  u0 = np.array([[0, 1], [1, 0]], dtype=np.complex128)
234  u1 = np.array([[1, 0], [0, -1]], dtype=np.complex128)
235  bundle = np.array([np.kron(u1, u0)])
236  rng = np.random.default_rng(1)
237  psi = rng.normal(size=4) + 1j * rng.normal(size=4)
238  psi = psi / np.linalg.norm(psi)
239  rho = DensityMatrix.from_numpy(np.outer(psi, psi.conj()).astype(np.complex128))
240  rho_np = np.asarray(rho.to_numpy(), dtype=np.complex128)
241  k_ref = _embed_single_qubit_gate(
242  u0, total_kernel_qbits=2, kernel_target_qbit=0
243  ) @ _embed_single_qubit_gate(u1, total_kernel_qbits=2, kernel_target_qbit=1)
244  ref_out = k_ref @ rho_np @ k_ref.conj().T
245  out = _apply_kraus_bundle(
246  bundle,
247  rho,
248  qbit_num=2,
249  local_support=(0, 1),
250  global_target_qbits=(0, 1),
251  descriptor_set=descriptor_set,
252  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
253  )
254  got = np.asarray(out.to_numpy(), dtype=np.complex128)
255  assert float(np.linalg.norm(got - ref_out, ord="fro")) <= PHASE3_RUNTIME_DENSITY_TOL
256 
257 
259  descriptor_set = _descriptor_set_for_invariant_tests()
260  bundle = np.array([np.eye(2, dtype=np.complex128)])
261  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
263  bundle,
264  DensityMatrix(3),
265  qbit_num=3,
266  local_support=(0, 1, 2),
267  global_target_qbits=(0, 1, 2),
268  descriptor_set=descriptor_set,
269  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
270  )
271  assert excinfo.value.first_unsupported_condition == "channel_native_representation"
272 
273 
275  descriptor_set = _descriptor_set_for_invariant_tests()
276  bundle = np.array([np.eye(4, dtype=np.complex128)])
277  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
279  bundle,
280  DensityMatrix(2),
281  qbit_num=2,
282  local_support=(0,),
283  global_target_qbits=(0,),
284  descriptor_set=descriptor_set,
285  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
286  )
287  assert excinfo.value.first_unsupported_condition == "channel_native_representation"
288 
289 
292  "phase31_microcase_2q_cnot_local_noise_pair"
293  )
294  partition = _find_partition_with_span(ds)
295  cnot_member = next(
296  m
297  for m in partition.members
298  if ds.canonical_operation_for(m).name == "CNOT"
299  )
300  params = build_initial_parameters(ds.parameter_count)
302  ds,
303  partition,
304  params,
305  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
306  )
308  ds,
309  partition.members,
310  local_vec,
311  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
312  )
313  local_support = tuple(range(len(partition.local_to_global_qbits)))
315  ds,
316  cnot_member,
317  seg,
318  local_support=local_support,
319  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
320  )
321  lmap = _local_qbit_to_kernel_index(local_support)
322  k_ctl, k_tgt = _kernel_indices_for_fused_cnot(
323  local_qbit_to_kernel_index=lmap,
324  local_target_qbit=cnot_member.local_target_qbit,
325  local_control_qbit=cnot_member.local_control_qbit,
326  )
327  expected = np.array(
328  [
330  total_kernel_qbits=2,
331  kernel_control_qbit=k_ctl,
332  kernel_target_qbit=k_tgt,
333  )
334  ],
335  dtype=np.complex128,
336  )
337  assert got.shape == expected.shape
338  assert (
339  float(np.linalg.norm(got[0] - expected[0], ord="fro"))
340  <= PHASE3_RUNTIME_DENSITY_TOL
341  )
342 
343 
346  "phase31_microcase_2q_multi_noise_entangler_chain"
347  )
348  partition = _find_partition_with_span(ds)
349  params = build_initial_parameters(ds.parameter_count)
351  ds,
352  partition,
353  params,
354  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
355  )
357  ds,
358  partition.members,
359  local_vec,
360  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
361  )
362  local_support = tuple(range(len(partition.local_to_global_qbits)))
363  lmap = _local_qbit_to_kernel_index(local_support)
364  cnot_members = [
365  m
366  for m in partition.members
367  if ds.canonical_operation_for(m).name == "CNOT"
368  ]
369  assert len(cnot_members) == 2
370  for m in cnot_members:
372  ds,
373  m,
374  seg,
375  local_support=local_support,
376  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
377  )
378  k_ctl, k_tgt = _kernel_indices_for_fused_cnot(
379  local_qbit_to_kernel_index=lmap,
380  local_target_qbit=m.local_target_qbit,
381  local_control_qbit=m.local_control_qbit,
382  )
383  expected = np.array(
384  [
386  total_kernel_qbits=2,
387  kernel_control_qbit=k_ctl,
388  kernel_target_qbit=k_tgt,
389  )
390  ],
391  dtype=np.complex128,
392  )
393  assert (
394  float(np.linalg.norm(got[0] - expected[0], ord="fro"))
395  <= PHASE3_RUNTIME_DENSITY_TOL
396  )
397 
398 
401  "phase31_microcase_2q_cnot_local_noise_pair"
402  )
403  partition = _find_partition_with_span(ds)
404  params = build_initial_parameters(ds.parameter_count)
406  ds,
407  partition,
408  params,
409  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
410  )
412  ds,
413  partition.members,
414  local_vec,
415  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
416  )
417  local_support = tuple(range(len(partition.local_to_global_qbits)))
418  global_targets = partition.local_to_global_qbits
419  steps = [
421  ds,
422  m,
423  seg,
424  local_support=local_support,
425  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
426  )
427  for m in partition.members
428  ]
430  len(local_support),
431  descriptor_set=ds,
432  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
433  )
434  for step in steps:
436  acc,
437  step,
438  descriptor_set=ds,
439  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
440  )
442  acc,
443  descriptor_set=ds,
444  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
445  )
446  rho0 = DensityMatrix(2)
447  rho_out = _apply_kraus_bundle(
448  acc,
449  rho0,
450  qbit_num=2,
451  local_support=local_support,
452  global_target_qbits=global_targets,
453  descriptor_set=ds,
454  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
455  )
456  reference = execute_sequential_density_reference(ds, params)
457  metrics = build_density_comparison_metrics(rho_out, reference)
458  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
459  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
460 
461 
464  "phase31_microcase_2q_cnot_local_noise_pair"
465  )
466  partition = _find_partition_with_span(ds)
467  params = build_initial_parameters(ds.parameter_count)
469  ds,
470  partition,
471  params,
472  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
473  )
475  ds,
476  partition.members,
477  local_vec,
478  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
479  )
480  local_support = tuple(range(len(partition.local_to_global_qbits)))
481  global_targets = partition.local_to_global_qbits
482  steps = [
484  ds,
485  m,
486  seg,
487  local_support=local_support,
488  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
489  )
490  for m in partition.members
491  ]
492  acc_ordered = _compose_kraus_step_sequence(
493  steps,
494  descriptor_set=ds,
495  support_qubit_count=len(local_support),
496  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
497  )
498  acc_reversed = _compose_kraus_step_sequence(
499  list(reversed(steps)),
500  descriptor_set=ds,
501  support_qubit_count=len(local_support),
502  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
503  )
504  rho0 = DensityMatrix(2)
505  out_ordered = _apply_kraus_bundle(
506  acc_ordered,
507  rho0,
508  qbit_num=2,
509  local_support=local_support,
510  global_target_qbits=global_targets,
511  descriptor_set=ds,
512  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
513  )
514  out_reversed = _apply_kraus_bundle(
515  acc_reversed,
516  rho0,
517  qbit_num=2,
518  local_support=local_support,
519  global_target_qbits=global_targets,
520  descriptor_set=ds,
521  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
522  )
523  reference = execute_sequential_density_reference(ds, params)
524  assert (
525  build_density_comparison_metrics(out_ordered, reference)["frobenius_norm_diff"]
526  <= PHASE3_RUNTIME_DENSITY_TOL
527  )
528  fro_rev = float(
529  np.linalg.norm(
530  np.asarray(out_ordered.to_numpy(), dtype=np.complex128)
531  - np.asarray(out_reversed.to_numpy(), dtype=np.complex128),
532  ord="fro",
533  )
534  )
535  assert fro_rev > 10.0 * PHASE3_RUNTIME_DENSITY_TOL
536 
537 
538 # --- Public runtime positives (P31-S05-E01) + fused-region audit ---
539 
540 
543  "phase31_local_support_q4_spectator_embedding_smoke"
544  )
545  assert [p.local_to_global_qbits for p in ds.partitions] == [(0, 1), (2, 3)]
546 
547 
549  assert fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
550  assert fr.partition_member_indices == (0, 1, 2, 3, 4, 5)
551  assert fr.operation_names == (
552  "U3",
553  "U3",
554  "CNOT",
555  "amplitude_damping",
556  "phase_damping",
557  "U3",
558  )
559 
560 
563  "phase31_microcase_2q_cnot_local_noise_pair"
564  )
565  parameters = build_initial_parameters(ds.parameter_count)
566  reference = execute_sequential_density_reference(ds, parameters)
567  result = execute_partitioned_density_channel_native(ds, parameters)
568 
569  assert result.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
570  assert result.fused_region_count >= 1
571  motif_regions = [
572  fr
573  for fr in result.fused_regions
574  if fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
575  ]
576  assert len(motif_regions) >= 1
578  assert motif_regions[0].global_target_qbits == (0, 1)
579  assert motif_regions[0].local_target_qbits == (0, 1)
580 
581  metrics = build_density_comparison_metrics(result.density_matrix, reference)
582  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
583  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
584  assert result.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
585  assert result.rho_is_valid is True
586 
587 
589  """Non-counted larger-workload smoke; full 4q state vs sequential oracle."""
591  "phase31_local_support_q4_spectator_embedding_smoke"
592  )
593  parameters = build_initial_parameters(ds.parameter_count)
594  reference = execute_sequential_density_reference(ds, parameters)
595  result = execute_partitioned_density_channel_native(ds, parameters)
596 
597  assert result.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
598  assert result.fused_region_count == 2
599  motif_regions = [
600  fr
601  for fr in result.fused_regions
602  if fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
603  ]
604  assert len(motif_regions) == 2
605  by_global = {fr.global_target_qbits: fr for fr in motif_regions}
606  assert set(by_global) == {(0, 1), (2, 3)}
609  assert by_global[(0, 1)].local_target_qbits == (0, 1)
610  assert by_global[(2, 3)].local_target_qbits == (0, 1)
611 
612  metrics = build_density_comparison_metrics(result.density_matrix, reference)
613  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
614  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
615  assert result.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
616  assert result.rho_is_valid is True
617 
618 
619 # --- Public correctness gate (P31-S06-E01) ---
620 
621 
624  "phase31_microcase_2q_cnot_local_noise_pair"
625  )
626  partition = _find_partition_with_span(ds)
627  params = build_initial_parameters(ds.parameter_count)
629  ds,
630  partition,
631  params,
632  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
633  )
635  ds,
636  partition.members,
637  local_vec,
638  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
639  )
640  local_support = tuple(range(len(partition.local_to_global_qbits)))
641  steps = [
643  ds,
644  m,
645  seg,
646  local_support=local_support,
647  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
648  )
649  for m in partition.members
650  ]
652  steps,
653  descriptor_set=ds,
654  support_qubit_count=len(local_support),
655  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
656  )
658  acc,
659  descriptor_set=ds,
660  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
661  )
662 
663 
664 # --- P31-S10-E01: remaining counted strict microcases (fourth vertical slice) ---
665 
666 
668  assert fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
669  assert fr.global_target_qbits == (0, 1)
670  assert fr.local_target_qbits == (0, 1)
671  assert fr.operation_names == (
672  "U3",
673  "U3",
674  "CNOT",
675  "local_depolarizing",
676  "U3",
677  "CNOT",
678  "amplitude_damping",
679  "U3",
680  "phase_damping",
681  )
682 
683 
685  assert fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
686  assert fr.global_target_qbits == (0, 1)
687  assert fr.local_target_qbits == (0, 1)
688  assert fr.operation_names == (
689  "U3",
690  "U3",
691  "CNOT",
692  "local_depolarizing",
693  "CNOT",
694  "amplitude_damping",
695  "U3",
696  "U3",
697  "phase_damping",
698  )
699 
700 
703  "phase31_microcase_2q_multi_noise_entangler_chain"
704  )
705  assert ds.workload_id == "phase31_microcase_2q_multi_noise_entangler_chain"
706  parameters = build_initial_parameters(ds.parameter_count)
707  reference = execute_sequential_density_reference(ds, parameters)
708  result = execute_partitioned_density_channel_native(ds, parameters)
709 
710  assert result.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
711  assert result.requested_runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
712  assert result.fused_region_count >= 1
713  motifs = [
714  fr
715  for fr in result.fused_regions
716  if fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
717  ]
718  assert len(motifs) >= 1
720 
721  metrics = build_density_comparison_metrics(result.density_matrix, reference)
722  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
723  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
724  assert result.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
725  assert result.rho_is_valid is True
726 
727 
730  "phase31_microcase_2q_dense_same_support_motif"
731  )
732  assert ds.workload_id == "phase31_microcase_2q_dense_same_support_motif"
733  parameters = build_initial_parameters(ds.parameter_count)
734  reference = execute_sequential_density_reference(ds, parameters)
735  result = execute_partitioned_density_channel_native(ds, parameters)
736 
737  assert result.runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
738  assert result.requested_runtime_path == PHASE31_RUNTIME_PATH_CHANNEL_NATIVE
739  assert result.fused_region_count >= 1
740  motifs = [
741  fr
742  for fr in result.fused_regions
743  if fr.candidate_kind == PHASE31_FUSION_KIND_CHANNEL_NATIVE_MOTIF
744  ]
745  assert len(motifs) >= 1
747 
748  metrics = build_density_comparison_metrics(result.density_matrix, reference)
749  assert metrics["frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
750  assert metrics["max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
751  assert result.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
752  assert result.rho_is_valid is True
753 
754 
757  "phase31_microcase_2q_multi_noise_entangler_chain"
758  )
759  partition = _find_partition_with_span(ds)
760  params = build_initial_parameters(ds.parameter_count)
762  ds,
763  partition,
764  params,
765  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
766  )
768  ds,
769  partition.members,
770  local_vec,
771  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
772  )
773  local_support = tuple(range(len(partition.local_to_global_qbits)))
774  steps = [
776  ds,
777  m,
778  seg,
779  local_support=local_support,
780  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
781  )
782  for m in partition.members
783  ]
785  steps,
786  descriptor_set=ds,
787  support_qubit_count=len(local_support),
788  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
789  )
791  acc,
792  descriptor_set=ds,
793  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
794  )
795 
796 
799  "phase31_microcase_2q_dense_same_support_motif"
800  )
801  partition = _find_partition_with_span(ds)
802  params = build_initial_parameters(ds.parameter_count)
804  ds,
805  partition,
806  params,
807  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
808  )
810  ds,
811  partition.members,
812  local_vec,
813  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
814  )
815  local_support = tuple(range(len(partition.local_to_global_qbits)))
816  steps = [
818  ds,
819  m,
820  seg,
821  local_support=local_support,
822  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
823  )
824  for m in partition.members
825  ]
827  steps,
828  descriptor_set=ds,
829  support_qubit_count=len(local_support),
830  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
831  )
833  acc,
834  descriptor_set=ds,
835  runtime_path=PHASE31_RUNTIME_PATH_CHANNEL_NATIVE,
836  )
837 
838 
839 # --- Public boundary (P31-S05-E02) ---
840 
841 
843  surface = build_microcase_surface("microcase_3q_mixed_local_noise_sequence")
844  ds = build_partition_descriptor_set(surface, max_partition_qubits=3)
845  parameters = build_initial_parameters(ds.parameter_count)
846  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
848  assert excinfo.value.first_unsupported_condition == "channel_native_qubit_span"
849 
850 
853  qbit_num=2,
854  source_type="microcase_builder",
855  workload_id="second_slice_2q_unitary_only_channel_native_negative",
856  operation_specs=[
857  _u3(0),
858  _u3(1),
859  _cnot(1, 0),
860  _u3(0),
861  ],
862  )
863  ds = build_partition_descriptor_set(surface)
864  parameters = build_initial_parameters(ds.parameter_count)
865  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
867  assert excinfo.value.first_unsupported_condition == "channel_native_noise_presence"
868 
869 
872  qbit_num=2,
873  source_type="microcase_builder",
874  workload_id="second_slice_rx_on_relaxed_surface_channel_native_negative",
875  operation_specs=[
876  {
877  "kind": "gate",
878  "name": "RX",
879  "target_qbit": 0,
880  "param_count": 1,
881  },
882  {
883  "kind": "noise",
884  "name": "phase_damping",
885  "target_qbit": 0,
886  "source_gate_index": 0,
887  "fixed_value": _noise_value("phase_damping"),
888  "param_count": 0,
889  },
890  ],
891  strict_phase3_support=False,
892  )
893  ds = build_partition_descriptor_set(surface)
894  parameters = build_initial_parameters(ds.parameter_count)
895  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
897  assert excinfo.value.first_unsupported_condition == "channel_native_support_surface"
898 
899 
902  qbit_num=1,
903  source_type="microcase_builder",
904  workload_id="second_slice_bit_flip_on_relaxed_surface_channel_native_negative",
905  operation_specs=[
906  _u3(0),
907  {
908  "kind": "noise",
909  "name": "bit_flip",
910  "target_qbit": 0,
911  "source_gate_index": 0,
912  "fixed_value": 0.1,
913  "param_count": 0,
914  },
915  ],
916  strict_phase3_support=False,
917  )
918  ds = build_partition_descriptor_set(surface)
919  parameters = build_initial_parameters(ds.parameter_count)
920  with pytest.raises(NoisyRuntimeValidationError) as excinfo:
922  assert excinfo.value.first_unsupported_condition == "channel_native_support_surface"
def execute_sequential_density_reference
Execute a sequential density reference.
def execute_partitioned_density_channel_native
def build_density_comparison_metrics