2 Python Unit Tests for Density Matrix Module 13 AmplitudeDampingChannel,
21 HADAMARD = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
25 assert rho.purity() < 1.0
30 rho_np = rho.to_numpy()
31 assert rho_np[0, 0].real > 0
36 rho_np = rho.to_numpy()
37 assert np.abs(rho_np[0, 1]) < 0.5
41 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
43 """Test basic density matrix operations.""" 46 """Test density matrix construction.""" 47 rho = DensityMatrix(qbit_num=2)
49 assert rho.qbit_num == 2
53 """Test ground state initialization.""" 54 rho = DensityMatrix(qbit_num=2)
57 rho_np = rho.to_numpy()
60 expected = np.zeros((4, 4), dtype=complex)
63 np.testing.assert_allclose(rho_np, expected, atol=1e-10)
66 """Test trace calculation.""" 67 rho = DensityMatrix(qbit_num=2)
70 assert np.isclose(tr.real, 1.0, atol=1e-10)
71 assert np.isclose(tr.imag, 0.0, atol=1e-10)
74 """Test that pure states have purity = 1.""" 75 rho = DensityMatrix(qbit_num=2)
78 assert np.isclose(purity, 1.0, atol=1e-10), \
79 f
"Pure state should have purity=1, got {purity}" 82 """Test that mixed states have purity < 1.""" 83 rho = DensityMatrix.maximally_mixed(qbit_num=2)
86 expected_purity = 0.25
88 assert np.isclose(purity, expected_purity, atol=1e-10), \
89 f
"Maximally mixed 2-qubit state should have purity=0.25, got {purity}" 92 """Test that pure states have zero entropy.""" 93 rho = DensityMatrix(qbit_num=2)
95 entropy = rho.entropy()
96 assert np.isclose(entropy, 0.0, atol=1e-10), \
97 f
"Pure state should have entropy=0, got {entropy}" 100 """Test entropy of maximally mixed state.""" 101 rho = DensityMatrix.maximally_mixed(qbit_num=2)
103 entropy = rho.entropy()
104 expected_entropy = 2.0
106 assert np.isclose(entropy, expected_entropy, atol=1e-10), \
107 f
"Maximally mixed 2-qubit state should have entropy=2, got {entropy}" 110 """Test density matrix validation.""" 112 rho_pure = DensityMatrix(qbit_num=2)
113 assert rho_pure.is_valid()
116 rho_mixed = DensityMatrix.maximally_mixed(qbit_num=2)
117 assert rho_mixed.is_valid()
120 """Test construction from state vector.""" 122 psi = np.array([1, 1], dtype=complex) / np.sqrt(2)
124 rho = DensityMatrix(psi)
127 expected = np.outer(psi, psi.conj())
128 rho_np = rho.to_numpy()
130 np.testing.assert_allclose(rho_np, expected, atol=1e-10)
133 assert np.isclose(rho.purity(), 1.0, atol=1e-10)
136 """Test unitary application.""" 137 rho = DensityMatrix(qbit_num=1)
140 rho.apply_unitary(HADAMARD)
142 expected = np.array([[0.5, 0.5], [0.5, 0.5]], dtype=complex)
143 np.testing.assert_allclose(rho.to_numpy(), expected, atol=1e-10)
146 """Test optimized single-qubit unitary.""" 147 rho = DensityMatrix(qbit_num=2)
150 rho.apply_single_qubit_unitary(HADAMARD, 0)
152 rho_np = rho.to_numpy()
157 assert np.isclose(rho_np[0, 0], 0.5)
158 assert np.isclose(rho_np[0, 1], 0.5)
159 assert np.isclose(rho_np[1, 0], 0.5)
160 assert np.isclose(rho_np[1, 1], 0.5)
164 rho1 = DensityMatrix(qbit_num=2)
168 rho1.apply_single_qubit_unitary(HADAMARD, 0)
171 rho2_np = rho2.to_numpy()
172 assert np.isclose(rho2_np[0, 0], 1.0)
175 """Test eigenvalue computation.""" 177 rho = DensityMatrix(qbit_num=2)
178 eigs = rho.eigenvalues()
180 assert len(eigs) == 4
181 assert np.isclose(eigs[0], 1.0, atol=1e-10)
182 assert np.all(np.abs(eigs[1:]) < 1e-10)
185 rho_mixed = DensityMatrix.maximally_mixed(qbit_num=2)
186 eigs_mixed = rho_mixed.eigenvalues()
188 expected = np.array([0.25, 0.25, 0.25, 0.25])
189 np.testing.assert_allclose(eigs_mixed, expected, atol=1e-10)
192 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
194 """Test unitary-only circuit operations.""" 197 """Test circuit construction.""" 198 circuit = NoisyCircuit(qbit_num=2)
200 assert circuit.qbit_num == 2
201 assert circuit.parameter_num == 0
204 """Test adding gates to circuit.""" 205 circuit = NoisyCircuit(2)
209 circuit.add_CNOT(1, 0)
214 """Test Hadamard gate application.""" 215 circuit = NoisyCircuit(2)
219 rho = DensityMatrix(qbit_num=2)
222 circuit.apply_to(np.array([]), rho)
226 purity = rho.purity()
227 assert np.isclose(purity, 1.0, atol=1e-10), \
228 "Hadamard should preserve purity" 231 """Test creation of Bell state |Φ+â© = (|00â© + |11â©)/â2.""" 232 circuit = NoisyCircuit(2)
234 circuit.add_CNOT(1, 0)
237 rho = DensityMatrix(qbit_num=2)
240 circuit.apply_to(np.array([]), rho)
243 purity = rho.purity()
244 assert np.isclose(purity, 1.0, atol=1e-10)
247 rho_np = rho.to_numpy()
250 assert np.isclose(rho_np[0, 0], 0.5, atol=1e-10)
251 assert np.isclose(rho_np[0, 3], 0.5, atol=1e-10)
252 assert np.isclose(rho_np[3, 0], 0.5, atol=1e-10)
253 assert np.isclose(rho_np[3, 3], 0.5, atol=1e-10)
256 """Test parametric gate (RZ).""" 257 circuit = NoisyCircuit(2)
260 rho = DensityMatrix(qbit_num=2)
263 params = np.array([np.pi / 4])
264 circuit.apply_to(params, rho)
267 purity = rho.purity()
268 assert np.isclose(purity, 1.0, atol=1e-10)
271 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
273 """Test noise operations in circuits.""" 275 @pytest.mark.parametrize(
276 "error_rate, params, expected_param_num",
278 pytest.param(0.1, np.array([]), 0, id=
"fixed"),
279 pytest.param(
None, np.array([0.2]), 1, id=
"parametric"),
283 """Test depolarizing noise in circuit.""" 284 circuit = NoisyCircuit(2)
286 circuit.add_CNOT(1, 0)
287 if error_rate
is None:
288 circuit.add_depolarizing(2)
290 circuit.add_depolarizing(2, error_rate=error_rate)
292 assert circuit.parameter_num == expected_param_num
294 rho = DensityMatrix(qbit_num=2)
295 circuit.apply_to(params, rho)
297 assert rho.purity() < 1.0
298 assert rho.is_valid()
301 """Test local single-qubit depolarizing noise in circuit.""" 302 circuit = NoisyCircuit(2)
304 circuit.add_local_depolarizing(0, error_rate=0.2)
306 rho = DensityMatrix(qbit_num=2)
307 circuit.apply_to(np.array([]), rho)
309 assert rho.purity() < 1.0
310 assert rho.is_valid()
313 """Test local single-qubit depolarizing rejects out-of-range targets.""" 314 circuit = NoisyCircuit(1)
315 circuit.add_local_depolarizing(1, error_rate=0.2)
317 rho = DensityMatrix(qbit_num=1)
318 with pytest.raises(RuntimeError, match=
"target_qbit out of range"):
319 circuit.apply_to(np.array([]), rho)
322 """Test amplitude damping in circuit.""" 323 circuit = NoisyCircuit(1)
325 circuit.add_amplitude_damping(0, gamma=0.5)
327 rho = DensityMatrix(qbit_num=1)
328 circuit.apply_to(np.array([]), rho)
330 rho_np = rho.to_numpy()
332 assert rho_np[0, 0].real > 0
333 assert rho_np[1, 1].real < 1
334 assert np.isclose(rho.trace().real, 1.0)
337 """Test phase damping in circuit.""" 338 circuit = NoisyCircuit(1)
340 circuit.add_phase_damping(0, lambda_param=0.5)
342 rho = DensityMatrix(qbit_num=1)
343 circuit.apply_to(np.array([]), rho)
345 rho_np = rho.to_numpy()
347 assert np.abs(rho_np[0, 1]) < 0.5
349 assert np.isclose(rho_np[0, 0].real, 0.5)
350 assert np.isclose(rho_np[1, 1].real, 0.5)
353 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
355 """Test combined circuit operations.""" 358 """Test mixed gates and noise in circuit.""" 359 circuit = NoisyCircuit(2)
361 circuit.add_amplitude_damping(0, gamma=0.1)
362 circuit.add_CNOT(1, 0)
363 circuit.add_phase_damping(1, lambda_param=0.05)
364 circuit.add_depolarizing(2, error_rate=0.02)
366 assert circuit.parameter_num == 0
368 rho = DensityMatrix(qbit_num=2)
369 circuit.apply_to(np.array([]), rho)
371 assert rho.is_valid()
372 assert rho.purity() < 1.0
375 """Test operation info from circuit.""" 376 circuit = NoisyCircuit(2)
378 circuit.add_CNOT(1, 0)
379 circuit.add_depolarizing(2, error_rate=0.1)
380 circuit.add_phase_damping(0)
382 info = circuit.get_operation_info()
384 assert len(info) == 4
385 assert info[0].name ==
"H" 386 assert info[0].is_unitary
is True 387 assert info[0].param_count == 0
389 assert info[1].name ==
"CNOT" 390 assert info[1].is_unitary
is True 392 assert info[2].name ==
"Depolarizing" 393 assert info[2].is_unitary
is False 394 assert info[2].param_count == 0
396 assert info[3].name ==
"PhaseDamping" 397 assert info[3].is_unitary
is False 398 assert info[3].param_count == 1
401 """Test HEA-relevant U3/CNOT lowering metadata.""" 402 circuit = NoisyCircuit(2)
404 circuit.add_CNOT(1, 0)
405 circuit.add_local_depolarizing(0, error_rate=0.05)
406 circuit.add_phase_damping(1, lambda_param=0.02)
408 info = circuit.get_operation_info()
410 assert len(info) == 4
411 assert info[0].name ==
"U3" 412 assert info[0].param_count == 3
413 assert info[0].param_start == 0
415 assert info[1].name ==
"CNOT" 416 assert info[1].param_count == 0
417 assert info[1].param_start == 3
419 assert info[2].name ==
"LocalDepolarizing" 420 assert info[2].param_count == 0
421 assert info[2].param_start == 3
423 assert info[3].name ==
"PhaseDamping" 424 assert info[3].param_count == 0
425 assert info[3].param_start == 3
428 """Test the exact-energy helper on a deterministic tiny fixture.""" 429 pytest.importorskip(
"qiskit")
430 pytest.importorskip(
"qiskit_aer")
432 from benchmarks.density_matrix.validate_squander_vs_qiskit
import density_energy
434 hamiltonian = np.array(
435 [[0.6, 0.2 + 0.1j], [0.2 - 0.1j, -0.4]],
438 density_matrix = np.array(
439 [[0.7, 0.1 - 0.05j], [0.1 + 0.05j, 0.3]],
443 energy_real, energy_imag =
density_energy(hamiltonian, density_matrix)
444 expected = np.trace(hamiltonian @ density_matrix)
446 assert np.isclose(energy_real, np.real(expected), atol=1e-12)
447 assert np.isclose(energy_imag, np.imag(expected), atol=1e-12)
450 """Test one mandatory micro-validation case end-to-end.""" 451 pytest.importorskip(
"qiskit")
452 pytest.importorskip(
"qiskit_aer")
454 from benchmarks.density_matrix.circuits
import MANDATORY_MICROCASES
455 from benchmarks.density_matrix.validate_squander_vs_qiskit
import (
461 for case
in MANDATORY_MICROCASES
462 if case[
"case_name"] ==
"micro_validation_2q_u3_cnot_local_depolarizing" 466 assert result[
"status"] ==
"pass" 467 assert result[
"energy_pass"]
468 assert result[
"density_valid_pass"]
469 assert result[
"trace_pass"]
470 assert result[
"observable_pass"]
471 assert result[
"qbit_num"] == 2
472 assert len(result[
"parameter_vector"]) == 6
475 """Test the mandatory mixed required-noise microcase.""" 476 pytest.importorskip(
"qiskit")
477 pytest.importorskip(
"qiskit_aer")
479 from benchmarks.density_matrix.circuits
import MANDATORY_MICROCASES
480 from benchmarks.density_matrix.validate_squander_vs_qiskit
import (
486 for case
in MANDATORY_MICROCASES
487 if case[
"case_name"] ==
"micro_validation_3q_u3_cnot_mixed_local_noise" 491 assert result[
"status"] ==
"pass" 492 assert result[
"energy_pass"]
493 assert result[
"density_valid_pass"]
494 assert result[
"trace_pass"]
495 assert result[
"observable_pass"]
496 assert result[
"required_gate_coverage_pass"]
497 assert result[
"required_noise_model_coverage_pass"]
498 assert result[
"noise_sequence_match_pass"]
499 assert result[
"mixed_sequence_order_pass"]
500 assert result[
"operation_audit_pass"]
501 assert result[
"noise_operation_sequence"] == [
502 "local_depolarizing",
506 assert result[
"noise_operation_targets"] == [0, 1, 2]
509 """Test the required local-noise micro-validation bundle schema.""" 510 pytest.importorskip(
"qiskit")
511 pytest.importorskip(
"qiskit_aer")
513 from benchmarks.density_matrix.noise_support.required_local_noise_micro_validation
import (
514 REQUIRED_LOCAL_NOISE_MODELS,
515 build_artifact_bundle,
516 run_required_local_noise_micro_validation,
519 results = run_required_local_noise_micro_validation(verbose=
False)
522 assert bundle[
"status"] ==
"pass" 523 assert bundle[
"backend"] ==
"density_matrix" 524 assert bundle[
"summary"][
"total_cases"] == 7
525 assert bundle[
"summary"][
"passed_cases"] == 7
526 assert bundle[
"summary"][
"pass_rate"] == 1.0
527 assert bundle[
"summary"][
"required_cases"] == 7
528 assert bundle[
"summary"][
"required_passed_cases"] == 7
529 assert bundle[
"summary"][
"required_pass_rate"] == 1.0
530 assert bundle[
"summary"][
"optional_cases"] == 0
531 assert bundle[
"summary"][
"optional_cases_count_toward_mandatory_baseline"] == 0
532 assert bundle[
"summary"][
"mandatory_baseline_completed"]
533 assert bundle[
"summary"][
"exact_threshold_passed_cases"] == 7
534 assert bundle[
"summary"][
"operation_audit_passed_cases"] == 7
535 assert bundle[
"summary"][
"mixed_sequence_case_count"] == 1
536 assert bundle[
"summary"][
"mixed_sequence_passed_cases"] == 1
537 assert set(bundle[
"requirements"][
"required_local_noise_models"]) == set(
538 REQUIRED_LOCAL_NOISE_MODELS
540 assert "required" in bundle[
"requirements"][
"support_tier_vocabulary"]
541 assert set(bundle[
"summary"][
"required_noise_models_covered"]) == set(
542 REQUIRED_LOCAL_NOISE_MODELS
544 assert all(case[
"required_local_noise_microcase_pass"]
for case
in bundle[
"cases"])
545 assert all(case[
"support_tier"] ==
"required" for case
in bundle[
"cases"])
546 assert all(case[
"case_purpose"] ==
"mandatory_baseline" for case
in bundle[
"cases"])
547 assert all(case[
"counts_toward_mandatory_baseline"]
for case
in bundle[
"cases"])
550 """Test the local-correctness bundle schema.""" 551 pytest.importorskip(
"qiskit")
552 pytest.importorskip(
"qiskit_aer")
554 from benchmarks.density_matrix.validation_evidence.local_correctness_validation
import (
555 MANDATORY_CASE_NAMES,
560 micro_validation_bundle,
561 required_local_noise_micro_validation_bundle,
565 assert micro_validation_bundle[
"status"] ==
"pass" 566 assert required_local_noise_micro_validation_bundle[
"status"] ==
"pass" 567 assert bundle[
"status"] ==
"pass" 568 assert bundle[
"backend"] ==
"density_matrix" 569 assert bundle[
"summary"][
"total_cases"] == len(MANDATORY_CASE_NAMES)
570 assert bundle[
"summary"][
"passed_cases"] == len(MANDATORY_CASE_NAMES)
571 assert bundle[
"summary"][
"required_cases"] == len(MANDATORY_CASE_NAMES)
572 assert bundle[
"summary"][
"required_passed_cases"] == len(MANDATORY_CASE_NAMES)
573 assert bundle[
"summary"][
"required_pass_rate"] == 1.0
574 assert bundle[
"summary"][
"mandatory_baseline_completed"]
is True 575 assert bundle[
"summary"][
"exact_threshold_passed_cases"] == len(
578 assert bundle[
"summary"][
"operation_audit_passed_cases"] == len(
581 assert bundle[
"summary"][
"stable_case_ids_present"]
is True 582 assert bundle[
"summary"][
"missing_mandatory_case_names"] == []
583 assert bundle[
"summary"][
"duplicate_case_names"] == []
584 assert bundle[
"summary"][
"unexpected_case_names"] == []
585 assert bundle[
"summary"][
"all_cases_required"]
is True 586 assert bundle[
"summary"][
"all_cases_count_toward_mandatory_baseline"]
is True 587 assert bundle[
"summary"][
"local_correctness_gate_completed"]
is True 588 assert set(bundle[
"requirements"][
"mandatory_case_names"]) == set(
591 assert bundle[
"required_artifacts"][
"micro_validation_reference"][
"status"] ==
"pass" 592 assert bundle[
"required_artifacts"][
"required_local_noise_micro_validation"][
"status"] ==
"pass" 593 assert all(case[
"support_tier"] ==
"required" for case
in bundle[
"cases"])
594 assert all(case[
"counts_toward_mandatory_baseline"]
for case
in bundle[
"cases"])
597 """Test that missing mandatory microcases fail the local-correctness gate.""" 598 pytest.importorskip(
"qiskit")
599 pytest.importorskip(
"qiskit_aer")
603 from benchmarks.density_matrix.validation_evidence.local_correctness_validation
import (
604 build_artifact_bundle,
609 micro_validation_bundle,
610 required_local_noise_micro_validation_bundle,
613 broken_required_local_noise_micro_validation_bundle = copy.deepcopy(
614 required_local_noise_micro_validation_bundle
616 omitted_case = broken_required_local_noise_micro_validation_bundle[
"cases"].pop()
619 micro_validation_bundle,
620 broken_required_local_noise_micro_validation_bundle,
623 assert bundle[
"status"] ==
"fail" 624 assert bundle[
"summary"][
"stable_case_ids_present"]
is False 625 assert bundle[
"summary"][
"local_correctness_gate_completed"]
is False 626 assert omitted_case[
"case_name"]
in bundle[
"summary"][
"missing_mandatory_case_names"]
629 """Test the optional-noise classification bundle schema.""" 630 pytest.importorskip(
"qiskit")
631 pytest.importorskip(
"qiskit_aer")
633 from benchmarks.density_matrix.noise_support.optional_noise_classification_validation
import (
634 OPTIONAL_WHOLE_REGISTER_CASES,
635 build_artifact_bundle,
640 required_local_noise_bundle,
641 required_local_noise_micro_bundle,
645 required_local_noise_bundle,
646 required_local_noise_micro_bundle,
650 assert bundle[
"status"] ==
"pass" 651 assert bundle[
"backend"] ==
"density_matrix" 652 assert bundle[
"summary"][
"required_cases"] == 10
653 assert bundle[
"summary"][
"required_passed_cases"] == 10
654 assert bundle[
"summary"][
"required_pass_rate"] == 1.0
655 assert bundle[
"summary"][
"optional_cases"] == len(OPTIONAL_WHOLE_REGISTER_CASES)
656 assert bundle[
"summary"][
"optional_passed_cases"] == len(OPTIONAL_WHOLE_REGISTER_CASES)
657 assert bundle[
"summary"][
"optional_pass_rate"] == 1.0
658 assert bundle[
"summary"][
"optional_cases_count_toward_mandatory_baseline"] == 0
659 assert bundle[
"summary"][
"mandatory_baseline_completed"]
660 assert set(bundle[
"summary"][
"support_tiers_present"]) == {
"optional",
"required"}
661 assert bundle[
"required_artifacts"][
"required_local_noise_bundle"][
"status"] ==
"pass" 663 bundle[
"required_artifacts"][
"required_local_noise_micro_bundle"][
"status"]
666 assert "optional" in bundle[
"requirements"][
"support_tier_vocabulary"]
667 assert all(case[
"support_tier"] ==
"optional" for case
in bundle[
"cases"])
668 assert all(
not case[
"counts_toward_mandatory_baseline"]
for case
in bundle[
"cases"])
669 assert all(case[
"whole_register_baseline_classification_pass"]
for case
in bundle[
"cases"])
670 assert all(case[
"optional_noise_case_pass"]
for case
in bundle[
"cases"])
671 assert all(case[
"noise_operation_sequence"] == [
"depolarizing"]
for case
in bundle[
"cases"])
674 """Test all supported gate types.""" 675 circuit = NoisyCircuit(2)
689 circuit.add_CNOT(1, 0)
693 rho = DensityMatrix(qbit_num=2)
694 circuit.apply_to(np.array([]), rho)
697 assert rho.is_valid()
700 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
702 """Test noise channel implementations.""" 705 """Test depolarizing noise reduces purity.""" 706 rho = DensityMatrix(qbit_num=2)
708 initial_purity = rho.purity()
709 assert np.isclose(initial_purity, 1.0)
712 noise = DepolarizingChannel(qbit_num=2, error_rate=0.1)
715 final_purity = rho.purity()
718 assert final_purity < initial_purity, \
719 f
"Noise should reduce purity: {initial_purity} â {final_purity}" 723 assert np.isclose(tr.real, 1.0, atol=1e-10)
726 """Test depolarizing noise converges to maximally mixed.""" 727 rho = DensityMatrix(qbit_num=2)
730 noise = DepolarizingChannel(qbit_num=2, error_rate=0.99)
734 purity = rho.purity()
735 assert np.isclose(purity, 0.25, atol=0.01), \
736 f
"Strong depolarizing should give purity â 0.25, got {purity}" 739 """Test amplitude damping channel.""" 741 psi = np.array([0, 1], dtype=complex)
742 rho = DensityMatrix(psi)
745 noise = AmplitudeDampingChannel(target_qbit=0, gamma=0.3)
749 rho_np = rho.to_numpy()
750 pop_1 = rho_np[1, 1].real
753 assert pop_1 < 1.0,
"Amplitude damping should reduce |1â© population" 754 assert pop_1 > 0.6,
"Population should not decay completely with γ=0.3" 757 """Test phase damping channel.""" 759 psi = np.array([1, 1], dtype=complex) / np.sqrt(2)
760 rho = DensityMatrix(psi)
762 rho_np_before = rho.to_numpy()
763 coherence_before = abs(rho_np_before[0, 1])
766 noise = PhaseDampingChannel(target_qbit=0, **{
'lambda': 0.5})
769 rho_np_after = rho.to_numpy()
770 coherence_after = abs(rho_np_after[0, 1])
773 assert coherence_after < coherence_before, \
774 "Phase damping should reduce coherence" 776 @pytest.mark.parametrize(
777 "channel_factory, rho_factory, validate",
780 lambda: DepolarizingChannel(2, 0.5),
781 lambda: DensityMatrix(qbit_num=2),
782 _assert_purity_reduced,
786 lambda: AmplitudeDampingChannel(0, 0.5),
787 lambda: DensityMatrix(np.array([0, 1], dtype=complex)),
788 _assert_population_transferred,
789 id=
"amplitude_damping",
792 lambda: PhaseDampingChannel(0, 0.5),
793 lambda: DensityMatrix(
794 np.array([1, 1], dtype=complex) / np.sqrt(2)
796 _assert_coherence_reduced,
802 self, channel_factory, rho_factory, validate
804 """Test legacy channel constructor signatures.""" 806 noise = channel_factory()
811 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
813 """Integration tests combining multiple features.""" 816 """Test circuit application followed by noise.""" 818 circuit = NoisyCircuit(3)
820 circuit.add_CNOT(1, 0)
821 circuit.add_CNOT(2, 1)
823 rho = DensityMatrix(qbit_num=3)
826 circuit.apply_to(np.array([]), rho)
829 purity_before = rho.purity()
830 assert np.isclose(purity_before, 1.0, atol=1e-10)
833 noise = DepolarizingChannel(qbit_num=3, error_rate=0.05)
837 purity_after = rho.purity()
838 assert purity_after < 1.0
842 assert np.isclose(tr.real, 1.0, atol=1e-10)
845 """Test partial trace operation.""" 847 circuit = NoisyCircuit(2)
849 circuit.add_CNOT(1, 0)
851 rho_full = DensityMatrix(qbit_num=2)
852 circuit.apply_to(np.array([]), rho_full)
855 rho_reduced = rho_full.partial_trace([1])
858 purity_reduced = rho_reduced.purity()
859 assert np.isclose(purity_reduced, 0.5, atol=1e-10), \
860 f
"Bell state reduced density matrix should have purity=0.5, got {purity_reduced}" 863 """Test partial trace from NumPy-constructed density matrix.""" 865 bell = np.array([[0.5, 0, 0, 0.5],
868 [0.5, 0, 0, 0.5]], dtype=complex)
869 rho = DensityMatrix.from_numpy(bell)
872 rho_reduced = rho.partial_trace([1])
874 expected = np.array([[0.5, 0], [0, 0.5]], dtype=complex)
875 np.testing.assert_allclose(rho_reduced.to_numpy(), expected, atol=1e-10)
878 """Test that density matrix evolution matches state vector for pure states.""" 882 sv_circuit = qgd_Circuit(2)
884 sv_circuit.add_CNOT(1, 0)
886 dm_circuit = NoisyCircuit(2)
888 dm_circuit.add_CNOT(1, 0)
891 sv = np.zeros(4, dtype=complex)
893 U = sv_circuit.get_Matrix(np.array([]))
897 rho = DensityMatrix(qbit_num=2)
898 dm_circuit.apply_to(np.array([]), rho)
901 rho_expected = np.outer(sv_final, sv_final.conj())
902 rho_actual = rho.to_numpy()
904 np.testing.assert_allclose(rho_actual, rho_expected, atol=1e-10)
907 """Test HEA-relevant U3 plus CNOT evolution against state vector.""" 910 sv_circuit = qgd_Circuit(2)
912 sv_circuit.add_CNOT(1, 0)
914 dm_circuit = NoisyCircuit(2)
916 dm_circuit.add_CNOT(1, 0)
918 params = np.array([0.21, -0.13, 0.37], dtype=np.float64)
920 sv = np.zeros(4, dtype=complex)
922 U = sv_circuit.get_Matrix(params)
925 rho = DensityMatrix(qbit_num=2)
926 dm_circuit.apply_to(params, rho)
928 rho_expected = np.outer(sv_final, sv_final.conj())
929 rho_actual = rho.to_numpy()
931 np.testing.assert_allclose(rho_actual, rho_expected, atol=1e-10)
934 @pytest.mark.skipif(
not HAS_MODULE, reason=
"Density matrix module not built")
937 """Performance-related tests.""" 940 """Test moderately large circuit.""" 942 circuit = NoisyCircuit(n_qubits)
947 circuit.add_CNOT(1, 0)
948 circuit.add_CNOT(2, 1)
949 circuit.add_CNOT(3, 2)
951 rho = DensityMatrix(qbit_num=n_qubits)
952 circuit.apply_to(np.array([]), rho)
954 assert rho.is_valid()
957 """Test repeated noise application.""" 958 circuit = NoisyCircuit(2)
963 circuit.add_depolarizing(2, error_rate=0.1)
965 rho = DensityMatrix(qbit_num=2)
966 circuit.apply_to(np.array([]), rho)
970 assert rho.purity() < 0.9
971 assert rho.is_valid()
975 """Test the workflow-contract bundle schema.""" 976 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation
import (
979 THRESHOLD_CORE_FIELDS,
986 assert reference_bundle[
"status"] ==
"pass" 987 assert artifact[
"status"] ==
"pass" 988 assert artifact[
"workflow_id"] == WORKFLOW_ID
989 assert artifact[
"contract_version"] == CONTRACT_VERSION
990 assert artifact[
"backend"] ==
"density_matrix" 991 assert artifact[
"reference_backend"] ==
"qiskit_aer_density_matrix" 992 assert artifact[
"requirements"][
"workflow_id"] == WORKFLOW_ID
993 assert artifact[
"requirements"][
"contract_version"] == CONTRACT_VERSION
994 assert artifact[
"requirements"][
"end_to_end_qubits"] == [4, 6]
995 assert artifact[
"requirements"][
"fixed_parameter_matrix_qubits"] == [4, 6, 8, 10]
996 assert artifact[
"requirements"][
"documented_anchor_qubit"] == 10
997 assert artifact[
"requirements"][
"fixed_parameter_sets_per_size"] == 10
998 assert artifact[
"requirements"][
"required_threshold_fields"] == list(
999 THRESHOLD_CORE_FIELDS
1001 assert artifact[
"input_contract"][
"workflow_family"] ==
"noisy_vqe_ground_state_estimation" 1002 assert artifact[
"input_contract"][
"ansatz"][
"family"] ==
"HEA" 1003 assert artifact[
"input_contract"][
"backend_selection"][
"selected_backend"] ==
"density_matrix" 1004 assert artifact[
"input_contract"][
"backend_selection"][
"silent_fallback_allowed"]
is False 1006 artifact[
"input_contract"][
"seed_policy"][
"bounded_optimization_trace"][
1007 "random_seed_required" 1011 assert artifact[
"output_contract"][
"case_status_vocabulary"] == list(STATUS_VOCABULARY)
1012 assert set(artifact[
"output_contract"][
"aggregate_status_semantics"].
keys()) == {
1016 assert "required_unsupported_case_fields" in artifact[
"output_contract"]
1017 assert artifact[
"thresholds"][
"absolute_energy_error"] == pytest.approx(1e-8)
1018 assert artifact[
"thresholds"][
"required_end_to_end_qubits"] == [4, 6]
1019 assert artifact[
"thresholds"][
"required_workflow_qubits"] == [4, 6, 8, 10]
1020 assert set(artifact[
"boundary_classification"].
keys()) == {
1026 assert artifact[
"summary"][
"mandatory_reference_artifact_count"] == 6
1027 assert artifact[
"summary"][
"reference_artifact_count"] == 6
1028 assert artifact[
"summary"][
"contract_sections_complete"]
is True 1029 assert artifact[
"summary"][
"required_threshold_field_count"] == len(
1030 THRESHOLD_CORE_FIELDS
1033 entry[
"artifact_id"] ==
"workflow_baseline_bundle" 1034 for entry
in artifact[
"reference_artifacts"]
1039 """Test that missing boundary classes fail workflow-contract validation.""" 1042 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation
import (
1044 validate_artifact_bundle,
1048 broken_artifact = copy.deepcopy(artifact)
1049 broken_artifact[
"boundary_classification"].pop(
"unsupported")
1051 with pytest.raises(ValueError, match=
"boundary classes mismatch"):
1056 """Test that missing workflow identity fails workflow-contract validation.""" 1059 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation
import (
1061 validate_artifact_bundle,
1065 broken_artifact = copy.deepcopy(artifact)
1066 broken_artifact[
"workflow_id"] =
"" 1068 with pytest.raises(ValueError, match=
"workflow_id must be non-empty"):
1073 """Test that missing threshold fields fail workflow-contract validation.""" 1076 from benchmarks.density_matrix.workflow_evidence.workflow_contract_validation
import (
1078 validate_artifact_bundle,
1082 broken_artifact = copy.deepcopy(artifact)
1083 broken_artifact[
"thresholds"].pop(
"required_workflow_qubits")
1085 with pytest.raises(ValueError, match=
"thresholds are missing required field"):
1090 """Test the end-to-end trace bundle schema.""" 1091 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation
import (
1092 MANDATORY_END_TO_END_CASE_NAMES,
1098 workflow_contract, workflow_bundle, trace_artifact, bundle =
run_validation(
1102 assert workflow_contract[
"status"] ==
"pass" 1103 assert workflow_bundle[
"status"] ==
"pass" 1104 assert trace_artifact[
"case_name"] == TRACE_CASE_NAME
1105 assert bundle[
"status"] ==
"pass" 1106 assert bundle[
"workflow_id"] == WORKFLOW_ID
1107 assert bundle[
"summary"][
"total_end_to_end_cases"] == len(MANDATORY_END_TO_END_CASE_NAMES)
1108 assert bundle[
"summary"][
"passed_end_to_end_cases"] == len(MANDATORY_END_TO_END_CASE_NAMES)
1109 assert bundle[
"summary"][
"stable_case_ids_present"]
is True 1110 assert bundle[
"summary"][
"required_trace_present"]
is True 1111 assert bundle[
"summary"][
"required_trace_completed"]
is True 1112 assert bundle[
"summary"][
"required_trace_bridge_supported"]
is True 1113 assert bundle[
"summary"][
"all_cases_match_contract"]
is True 1114 assert bundle[
"summary"][
"end_to_end_qubits_match_contract"]
is True 1115 assert bundle[
"summary"][
"trace_case_name_matches_contract"]
is True 1116 assert bundle[
"summary"][
"trace_matches_contract"]
is True 1117 assert bundle[
"summary"][
"workflow_thresholds_match_contract"]
is True 1118 assert bundle[
"summary"][
"end_to_end_gate_completed"]
is True 1119 assert bundle[
"requirements"][
"required_trace_case_name"] == TRACE_CASE_NAME
1120 assert bundle[
"requirements"][
"mandatory_end_to_end_qubits"] == [4, 6]
1121 assert bundle[
"requirements"][
"workflow_id"] == WORKFLOW_ID
1122 assert bundle[
"thresholds"][
"required_end_to_end_qubits"] == [4, 6]
1123 assert bundle[
"thresholds"][
"required_trace_case_name"] == TRACE_CASE_NAME
1124 assert [case[
"case_name"]
for case
in bundle[
"cases"]] == list(
1125 MANDATORY_END_TO_END_CASE_NAMES
1127 assert all(case[
"required_workflow_case"]
for case
in bundle[
"cases"])
1128 assert bundle[
"trace_artifact"][
"required_workflow_trace"]
is True 1132 """Test that missing mandatory end-to-end cases fail the trace gate.""" 1135 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation
import (
1136 VALIDATION_TRACE_ARTIFACT_PATH,
1137 VALIDATION_WORKFLOW_BASELINE_PATH,
1138 build_artifact_bundle,
1140 _load_workflow_contract,
1144 workflow_bundle = copy.deepcopy(
_load_json(VALIDATION_WORKFLOW_BASELINE_PATH))
1145 trace_artifact =
_load_json(VALIDATION_TRACE_ARTIFACT_PATH)
1146 workflow_bundle[
"cases"] = [
1148 for case
in workflow_bundle[
"cases"]
1149 if case[
"case_name"] !=
"exact_regime_6q_set_00" 1154 assert bundle[
"status"] ==
"fail" 1155 assert bundle[
"summary"][
"stable_case_ids_present"]
is False 1156 assert bundle[
"summary"][
"end_to_end_gate_completed"]
is False 1157 assert "exact_regime_6q_set_00" in bundle[
"summary"][
"missing_mandatory_case_names"]
1161 """Test that workflow-contract trace-name drift blocks the trace gate.""" 1164 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation
import (
1165 VALIDATION_TRACE_ARTIFACT_PATH,
1166 VALIDATION_WORKFLOW_BASELINE_PATH,
1167 build_artifact_bundle,
1169 _load_workflow_contract,
1173 workflow_bundle =
_load_json(VALIDATION_WORKFLOW_BASELINE_PATH)
1174 trace_artifact =
_load_json(VALIDATION_TRACE_ARTIFACT_PATH)
1175 workflow_contract[
"input_contract"][
"execution_modes"][
"bounded_optimization_trace"][
1176 "canonical_trace_case_name" 1177 ] =
"unexpected_trace_6q" 1181 assert bundle[
"status"] ==
"fail" 1182 assert bundle[
"summary"][
"required_trace_present"]
is False 1183 assert bundle[
"summary"][
"trace_case_name_matches_contract"]
is False 1184 assert bundle[
"summary"][
"end_to_end_gate_completed"]
is False 1188 """Test that incomplete trace evidence fails the trace gate.""" 1191 from benchmarks.density_matrix.workflow_evidence.end_to_end_trace_validation
import (
1192 VALIDATION_TRACE_ARTIFACT_PATH,
1193 VALIDATION_WORKFLOW_BASELINE_PATH,
1194 build_artifact_bundle,
1196 _load_workflow_contract,
1200 workflow_bundle =
_load_json(VALIDATION_WORKFLOW_BASELINE_PATH)
1201 trace_artifact = copy.deepcopy(
_load_json(VALIDATION_TRACE_ARTIFACT_PATH))
1202 trace_artifact[
"workflow_completed"] =
False 1206 assert bundle[
"status"] ==
"fail" 1207 assert bundle[
"summary"][
"required_trace_completed"]
is False 1208 assert bundle[
"summary"][
"end_to_end_gate_completed"]
is False 1212 """Test the matrix-baseline bundle schema.""" 1213 from benchmarks.density_matrix.workflow_evidence.matrix_baseline_validation
import (
1218 workflow_contract, end_to_end_trace_bundle, workflow_baseline_bundle, bundle =
run_validation(
1222 assert workflow_contract[
"status"] ==
"pass" 1223 assert end_to_end_trace_bundle[
"status"] ==
"pass" 1224 assert workflow_baseline_bundle[
"status"] ==
"pass" 1225 assert bundle[
"status"] ==
"pass" 1226 assert bundle[
"workflow_id"] == WORKFLOW_ID
1227 assert bundle[
"summary"][
"required_cases"] == 40
1228 assert bundle[
"summary"][
"required_passed_cases"] == 40
1229 assert bundle[
"summary"][
"required_pass_rate"] == 1.0
1230 assert bundle[
"summary"][
"stable_case_ids_present"]
is True 1231 assert bundle[
"summary"][
"stable_parameter_set_ids_present"]
is True 1232 assert bundle[
"summary"][
"documented_10q_anchor_present"]
is True 1233 assert bundle[
"summary"][
"workflow_inventory_matches_contract"]
is True 1234 assert bundle[
"summary"][
"workflow_thresholds_match_contract"]
is True 1235 assert bundle[
"summary"][
"all_cases_match_contract"]
is True 1236 assert bundle[
"summary"][
"matrix_gate_completed"]
is True 1237 assert bundle[
"requirements"][
"mandatory_workflow_qubits"] == [4, 6, 8, 10]
1238 assert bundle[
"requirements"][
"fixed_parameter_sets_per_size"] == 10
1239 assert bundle[
"requirements"][
"documented_anchor_qubit"] == 10
1240 assert bundle[
"thresholds"][
"required_workflow_qubits"] == [4, 6, 8, 10]
1241 assert bundle[
"thresholds"][
"fixed_parameter_sets_per_size"] == 10
1242 assert bundle[
"thresholds"][
"documented_anchor_qubit"] == 10
1243 assert bundle[
"required_artifacts"][
"end_to_end_trace_reference"][
"status"] ==
"pass" 1244 assert len(bundle[
"cases"]) == 40
1245 assert all(case[
"required_matrix_case"]
for case
in bundle[
"cases"])
1249 """Test that missing matrix cases fail the matrix baseline gate.""" 1252 from benchmarks.density_matrix.workflow_evidence.matrix_baseline_validation
import (
1253 VALIDATION_WORKFLOW_BASELINE_PATH,
1255 _load_workflow_contract,
1256 _load_end_to_end_trace_bundle,
1257 build_artifact_bundle,
1262 workflow_baseline_bundle = copy.deepcopy(
1263 _load_json(VALIDATION_WORKFLOW_BASELINE_PATH)
1265 workflow_baseline_bundle[
"cases"] = [
1267 for case
in workflow_baseline_bundle[
"cases"]
1268 if case[
"case_name"] !=
"exact_regime_10q_set_09" 1273 end_to_end_trace_bundle,
1274 workflow_baseline_bundle,
1277 assert bundle[
"status"] ==
"fail" 1278 assert bundle[
"summary"][
"stable_case_ids_present"]
is False 1279 assert bundle[
"summary"][
"matrix_gate_completed"]
is False 1280 assert "exact_regime_10q_set_09" in bundle[
"summary"][
"missing_mandatory_case_names"]
1284 """Test that workflow-contract threshold drift blocks the matrix gate.""" 1287 from benchmarks.density_matrix.workflow_evidence.matrix_baseline_validation
import (
1288 VALIDATION_WORKFLOW_BASELINE_PATH,
1290 _load_workflow_contract,
1291 _load_end_to_end_trace_bundle,
1292 build_artifact_bundle,
1297 workflow_baseline_bundle =
_load_json(VALIDATION_WORKFLOW_BASELINE_PATH)
1298 workflow_contract[
"thresholds"][
"absolute_energy_error"] = 1e-7
1302 end_to_end_trace_bundle,
1303 workflow_baseline_bundle,
1306 assert bundle[
"status"] ==
"fail" 1307 assert bundle[
"summary"][
"workflow_thresholds_match_contract"]
is False 1308 assert bundle[
"summary"][
"matrix_gate_completed"]
is False 1312 """Test that case-level contract mismatch fails matrix-baseline validation.""" 1315 from benchmarks.density_matrix.workflow_evidence.matrix_baseline_validation
import (
1317 validate_artifact_bundle,
1321 broken_bundle = copy.deepcopy(bundle)
1322 broken_bundle[
"cases"][0][
"workflow_id"] =
"wrong_workflow_id" 1324 with pytest.raises(ValueError, match=
"does not match bundle workflow_id"):
1329 """Test the unsupported-workflow bundle schema.""" 1330 from benchmarks.density_matrix.workflow_evidence.unsupported_workflow_validation
import (
1331 BACKEND_MISMATCH_CASE_NAME,
1337 end_to_end_trace_bundle,
1338 matrix_baseline_bundle,
1339 unsupported_noise_bundle,
1340 backend_mismatch_case,
1344 assert workflow_contract[
"status"] ==
"pass" 1345 assert end_to_end_trace_bundle[
"status"] ==
"pass" 1346 assert matrix_baseline_bundle[
"status"] ==
"pass" 1347 assert unsupported_noise_bundle[
"status"] ==
"pass" 1348 assert backend_mismatch_case[
"case_name"] == BACKEND_MISMATCH_CASE_NAME
1349 assert bundle[
"status"] ==
"pass" 1350 assert bundle[
"summary"][
"unsupported_status_cases"] == bundle[
"summary"][
"total_cases"]
1351 assert bundle[
"summary"][
"mandatory_baseline_case_count"] == 0
1352 assert bundle[
"summary"][
"backend_incompatible_case_present"]
is True 1353 assert bundle[
"summary"][
"all_cases_match_contract"]
is True 1354 assert bundle[
"summary"][
"unsupported_gate_completed"]
is True 1356 bundle[
"requirements"][
"required_case_fields"]
1357 == workflow_contract[
"output_contract"][
"required_unsupported_case_fields"]
1359 assert bundle[
"thresholds"][
"silent_fallback_allowed"]
is False 1360 assert "backend_incompatible_request" in bundle[
"summary"][
"categories_present"]
1361 assert all(case[
"required_unsupported_case"]
for case
in bundle[
"cases"])
1365 """Test that missing backend-mismatch evidence fails the unsupported gate.""" 1368 from benchmarks.density_matrix.workflow_evidence.unsupported_workflow_validation
import (
1369 BACKEND_MISMATCH_CASE_PATH,
1370 UNSUPPORTED_NOISE_BUNDLE_PATH,
1372 _load_workflow_contract,
1373 _load_end_to_end_trace_bundle,
1374 _load_matrix_baseline_bundle,
1375 build_artifact_bundle,
1381 unsupported_noise_bundle =
_load_json(UNSUPPORTED_NOISE_BUNDLE_PATH)
1382 backend_mismatch_case = copy.deepcopy(
_load_json(BACKEND_MISMATCH_CASE_PATH))
1383 backend_mismatch_case[
"case_name"] =
"missing_backend_case" 1387 end_to_end_trace_bundle,
1388 matrix_baseline_bundle,
1389 unsupported_noise_bundle,
1390 backend_mismatch_case,
1393 assert bundle[
"status"] ==
"fail" 1394 assert bundle[
"summary"][
"backend_incompatible_case_present"]
is False 1395 assert bundle[
"summary"][
"unsupported_gate_completed"]
is False 1399 """Test that missing first-unsupported-condition fields fail validation.""" 1402 from benchmarks.density_matrix.workflow_evidence.unsupported_workflow_validation
import (
1404 validate_artifact_bundle,
1408 broken_bundle = copy.deepcopy(bundle)
1409 broken_bundle[
"cases"][0].pop(
"first_unsupported_condition")
1411 with pytest.raises(ValueError, match=
"missing required fields"):
1416 """Test that workflow-contract field drift is enforced by the unsupported gate.""" 1419 from benchmarks.density_matrix.workflow_evidence.unsupported_workflow_validation
import (
1420 BACKEND_MISMATCH_CASE_PATH,
1421 UNSUPPORTED_NOISE_BUNDLE_PATH,
1423 _load_workflow_contract,
1424 _load_end_to_end_trace_bundle,
1425 _load_matrix_baseline_bundle,
1426 build_artifact_bundle,
1432 unsupported_noise_bundle =
_load_json(UNSUPPORTED_NOISE_BUNDLE_PATH)
1433 backend_mismatch_case =
_load_json(BACKEND_MISMATCH_CASE_PATH)
1434 workflow_contract[
"output_contract"][
"required_unsupported_case_fields"].append(
1435 "workflow_contract_only_required_field" 1438 with pytest.raises(ValueError, match=
"missing required fields"):
1441 end_to_end_trace_bundle,
1442 matrix_baseline_bundle,
1443 unsupported_noise_bundle,
1444 backend_mismatch_case,
1449 """Test the workflow-interpretation bundle schema.""" 1450 from benchmarks.density_matrix.workflow_evidence.workflow_interpretation_validation
import (
1457 end_to_end_trace_bundle,
1458 matrix_baseline_bundle,
1459 unsupported_workflow_bundle,
1460 optional_noise_bundle,
1464 assert workflow_contract[
"status"] ==
"pass" 1465 assert end_to_end_trace_bundle[
"status"] ==
"pass" 1466 assert matrix_baseline_bundle[
"status"] ==
"pass" 1467 assert unsupported_workflow_bundle[
"status"] ==
"pass" 1468 assert optional_noise_bundle[
"status"] ==
"pass" 1469 assert bundle[
"status"] ==
"pass" 1470 assert bundle[
"workflow_id"] == WORKFLOW_ID
1471 assert bundle[
"summary"][
"workflow_contract_complete"]
is True 1472 assert bundle[
"summary"][
"end_to_end_gate_complete"]
is True 1473 assert bundle[
"summary"][
"matrix_baseline_gate_complete"]
is True 1474 assert bundle[
"summary"][
"unsupported_workflow_gate_complete"]
is True 1475 assert bundle[
"summary"][
"unsupported_case_field_alignment"]
is True 1476 assert bundle[
"summary"][
"mandatory_artifacts_complete"]
is True 1477 assert bundle[
"summary"][
"optional_evidence_supplemental"]
is True 1478 assert bundle[
"summary"][
"unsupported_evidence_negative_only"]
is True 1479 assert bundle[
"summary"][
"main_workflow_claim_completed"]
is True 1480 assert bundle[
"summary"][
"optional_cases_count_toward_mandatory_baseline"] == 0
1481 assert bundle[
"required_artifacts"][
"unsupported_workflow_reference"][
"status"] ==
"pass" 1485 """Test that optional evidence cannot count toward the main workflow claim.""" 1488 from benchmarks.density_matrix.workflow_evidence.workflow_interpretation_validation
import (
1489 OPTIONAL_NOISE_BUNDLE_PATH,
1491 _load_workflow_contract,
1492 _load_end_to_end_trace_bundle,
1493 _load_matrix_baseline_bundle,
1494 _load_unsupported_workflow_bundle,
1495 build_artifact_bundle,
1502 optional_noise_bundle = copy.deepcopy(
_load_json(OPTIONAL_NOISE_BUNDLE_PATH))
1503 optional_noise_bundle[
"summary"][
"optional_cases_count_toward_mandatory_baseline"] = 1
1507 end_to_end_trace_bundle,
1508 matrix_baseline_bundle,
1509 unsupported_workflow_bundle,
1510 optional_noise_bundle,
1513 assert bundle[
"status"] ==
"fail" 1514 assert bundle[
"summary"][
"optional_evidence_supplemental"]
is False 1515 assert bundle[
"summary"][
"main_workflow_claim_completed"]
is False 1519 """Test that incomplete mandatory artifacts block claim closure.""" 1522 from benchmarks.density_matrix.workflow_evidence.workflow_interpretation_validation
import (
1523 OPTIONAL_NOISE_BUNDLE_PATH,
1525 _load_workflow_contract,
1526 _load_end_to_end_trace_bundle,
1527 _load_matrix_baseline_bundle,
1528 _load_unsupported_workflow_bundle,
1529 build_artifact_bundle,
1536 optional_noise_bundle =
_load_json(OPTIONAL_NOISE_BUNDLE_PATH)
1537 matrix_baseline_bundle[
"status"] =
"fail" 1541 end_to_end_trace_bundle,
1542 matrix_baseline_bundle,
1543 unsupported_workflow_bundle,
1544 optional_noise_bundle,
1547 assert bundle[
"status"] ==
"fail" 1548 assert "matrix_baseline_reference" in bundle[
"summary"][
"incomplete_mandatory_artifacts"]
1549 assert bundle[
"summary"][
"mandatory_artifacts_complete"]
is False 1550 assert bundle[
"summary"][
"main_workflow_claim_completed"]
is False 1554 """Test that unsupported-field drift blocks the main workflow claim.""" 1557 from benchmarks.density_matrix.workflow_evidence.workflow_interpretation_validation
import (
1558 OPTIONAL_NOISE_BUNDLE_PATH,
1560 _load_workflow_contract,
1561 _load_end_to_end_trace_bundle,
1562 _load_matrix_baseline_bundle,
1563 _load_unsupported_workflow_bundle,
1564 build_artifact_bundle,
1571 optional_noise_bundle =
_load_json(OPTIONAL_NOISE_BUNDLE_PATH)
1572 unsupported_workflow_bundle[
"requirements"][
"required_case_fields"].append(
1573 "exact_regime_only_field_drift" 1578 end_to_end_trace_bundle,
1579 matrix_baseline_bundle,
1580 unsupported_workflow_bundle,
1581 optional_noise_bundle,
1584 assert bundle[
"status"] ==
"fail" 1585 assert bundle[
"summary"][
"unsupported_case_field_alignment"]
is False 1586 assert bundle[
"summary"][
"unsupported_evidence_negative_only"]
is False 1587 assert bundle[
"summary"][
"main_workflow_claim_completed"]
is False 1591 """Test the workflow-publication bundle schema.""" 1592 from benchmarks.density_matrix.workflow_evidence.workflow_publication_bundle
import (
1599 end_to_end_trace_bundle,
1600 matrix_baseline_bundle,
1601 unsupported_workflow_bundle,
1602 workflow_interpretation_bundle,
1606 assert workflow_contract[
"status"] ==
"pass" 1607 assert end_to_end_trace_bundle[
"status"] ==
"pass" 1608 assert matrix_baseline_bundle[
"status"] ==
"pass" 1609 assert unsupported_workflow_bundle[
"status"] ==
"pass" 1610 assert workflow_interpretation_bundle[
"status"] ==
"pass" 1611 assert bundle[
"status"] ==
"pass" 1612 assert bundle[
"workflow_id"] == WORKFLOW_ID
1613 assert bundle[
"summary"][
"mandatory_artifact_count"] == 5
1614 assert bundle[
"summary"][
"present_artifact_count"] == 5
1615 assert bundle[
"summary"][
"status_match_count"] == 5
1616 assert bundle[
"summary"][
"workflow_identity_match_count"] == 5
1617 assert bundle[
"summary"][
"semantic_match_count"] == 5
1618 assert len(bundle[
"artifacts"]) == 5
1619 assert all(artifact[
"mandatory"]
for artifact
in bundle[
"artifacts"])
1623 """Test that missing mandatory artifact entries fail publication validation.""" 1626 from benchmarks.density_matrix.workflow_evidence.workflow_publication_bundle
import (
1629 validate_workflow_publication_bundle,
1633 broken_bundle = copy.deepcopy(bundle)
1634 broken_bundle[
"artifacts"] = broken_bundle[
"artifacts"][:-1]
1636 with pytest.raises(ValueError, match=
"missing required artifact IDs"):
1641 """Test that mismatched contract versions fail publication validation.""" 1644 from benchmarks.density_matrix.workflow_evidence.workflow_publication_bundle
import (
1647 validate_workflow_publication_bundle,
1651 broken_bundle = copy.deepcopy(bundle)
1652 broken_bundle[
"artifacts"][0][
"summary"][
"contract_version"] =
"v2" 1654 with pytest.raises(ValueError, match=
"mismatched contract_version"):
1659 """Test that missing semantic closure flags fail publication validation.""" 1662 from benchmarks.density_matrix.workflow_evidence.workflow_publication_bundle
import (
1665 validate_workflow_publication_bundle,
1669 broken_bundle = copy.deepcopy(bundle)
1670 broken_bundle[
"artifacts"][1][
"summary"][
"end_to_end_gate_completed"] =
False 1672 with pytest.raises(ValueError, match=
"missing required semantic closure flags"):
1676 if __name__ ==
"__main__":
1677 pytest.main([__file__,
"-v"])
def test_phase_damping(self)
def _assert_population_transferred(rho)
def test_missing_case_blocks_local_correctness_closure(self)
def test_matrix_baseline_threshold_contract_mismatch_blocks_closure_module_level()
def test_partial_trace(self)
def test_workflow_publication_missing_artifact_entry_fails_validation_module_level()
def test_u3_cnot_operation_info(self)
def validate_microcase(case, verbose=True)
def test_purity_pure_state(self)
def test_unsupported_workflow_required_field_mismatch_raises_module_level()
def test_workflow_contract_validation_schema_module_level()
def test_workflow_interpretation_bundle_schema_module_level()
def test_workflow_interpretation_incomplete_mandatory_artifact_blocks_closure_module_level()
def test_operation_info(self)
def test_end_to_end_trace_incomplete_trace_blocks_closure_module_level()
def test_depolarizing(self, error_rate, params, expected_param_num)
def test_entropy_pure_state(self)
def test_circuit_with_noise(self)
def test_apply_unitary(self)
def test_end_to_end_trace_missing_end_to_end_case_blocks_closure_module_level()
def test_amplitude_damping(self)
def validate_artifact_bundle(bundle)
def test_mixed_operations(self)
def test_eigenvalues(self)
def _assert_coherence_reduced(rho)
def build_artifact_bundle(results)
def test_unsupported_workflow_bundle_schema_module_level()
def test_amplitude_damping(self)
def test_phase_damping(self)
def _load_matrix_baseline_bundle
def test_local_depolarizing(self)
def test_workflow_contract_missing_threshold_field_fails_validation_module_level()
def test_purity_mixed_state(self)
def test_construction(self)
def test_workflow_publication_mismatched_contract_version_fails_validation_module_level()
def test_workflow_publication_missing_semantic_flag_fails_validation_module_level()
def test_ground_state(self)
def test_workflow_contract_missing_boundary_classification_fails_validation_module_level()
def test_matrix_baseline_case_contract_mismatch_fails_validation_module_level()
def test_depolarizing_convergence(self)
def test_bell_state_creation(self)
def test_density_energy_helper_matches_trace_estimate(self)
def run_validation(verbose=True)
def test_workflow_contract_missing_workflow_id_fails_validation_module_level()
def test_hadamard_application(self)
def test_comparison_with_state_vector(self)
def _assert_purity_reduced(rho)
def test_apply_single_qubit_unitary(self)
def _load_unsupported_workflow_bundle
def test_legacy_channel_signatures(self, channel_factory, rho_factory, validate)
def _load_workflow_contract
def test_unsupported_workflow_missing_first_condition_fails_validation_module_level()
def validate_workflow_publication_bundle
def test_partial_trace_from_numpy(self)
def test_workflow_interpretation_optional_evidence_counting_toward_mandatory_blocks_closure_module_level()
def test_circuit_construction(self)
def test_end_to_end_trace_bundle_schema_module_level()
def test_u3_cnot_comparison_with_state_vector(self)
def test_required_local_noise_micro_bundle_schema(self)
def test_local_depolarizing_invalid_target_raises(self)
def test_unsupported_workflow_missing_backend_mismatch_case_blocks_closure_module_level()
def test_workflow_interpretation_field_alignment_required_for_main_claim_module_level()
def test_matrix_baseline_missing_matrix_case_blocks_closure_module_level()
def test_entropy_maximally_mixed(self)
def test_representative_microcase_passes(self)
def test_optional_noise_classification_bundle_schema(self)
def test_mixed_required_noise_microcase_passes(self)
def test_parametric_gate(self)
def test_matrix_baseline_bundle_schema_module_level()
def test_from_state_vector(self)
def test_local_correctness_bundle_schema(self)
def test_workflow_publication_bundle_schema_module_level()
def _load_end_to_end_trace_bundle
def test_end_to_end_trace_contract_mismatch_blocks_closure_module_level()
def test_depolarizing_channel(self)