1use oxilean_kernel::{BinderInfo, Declaration, Environment, Expr, Level, Name};
6
7use super::types::{
8 CSSCode, ConcatenatedCode, FaultToleranceThreshold, GKPCodeSimulator, MagicStateDistillation,
9 MagicStateProtocol, PauliOp, PauliOperator, PauliString, QECNormChecker, QuantumLDPC, ShorCode,
10 StabCode, StabilizerCode, StabilizerSimulator, SurfaceCode, SurfaceCodeDecoder,
11 SyndromeDecoder2, ThresholdEstimator,
12};
13
14pub fn app(f: Expr, a: Expr) -> Expr {
15 Expr::App(Box::new(f), Box::new(a))
16}
17pub fn app2(f: Expr, a: Expr, b: Expr) -> Expr {
18 app(app(f, a), b)
19}
20pub fn app3(f: Expr, a: Expr, b: Expr, c: Expr) -> Expr {
21 app(app2(f, a, b), c)
22}
23pub fn cst(s: &str) -> Expr {
24 Expr::Const(Name::str(s), vec![])
25}
26pub fn prop() -> Expr {
27 Expr::Sort(Level::zero())
28}
29pub fn type0() -> Expr {
30 Expr::Sort(Level::succ(Level::zero()))
31}
32pub fn pi(bi: BinderInfo, name: &str, dom: Expr, body: Expr) -> Expr {
33 Expr::Pi(bi, Name::str(name), Box::new(dom), Box::new(body))
34}
35pub fn arrow(a: Expr, b: Expr) -> Expr {
36 pi(BinderInfo::Default, "_", a, b)
37}
38pub fn bvar(n: u32) -> Expr {
39 Expr::BVar(n)
40}
41pub fn nat_ty() -> Expr {
42 cst("Nat")
43}
44pub fn real_ty() -> Expr {
45 cst("Real")
46}
47pub fn bool_ty() -> Expr {
48 cst("Bool")
49}
50pub fn list_ty(elem: Expr) -> Expr {
51 app(cst("List"), elem)
52}
53pub fn pauli_op_ty() -> Expr {
55 type0()
56}
57pub fn pauli_group_ty() -> Expr {
60 arrow(nat_ty(), type0())
61}
62pub fn pauli_phase_ty() -> Expr {
65 type0()
66}
67pub fn symplectic_representation_ty() -> Expr {
70 arrow(nat_ty(), type0())
71}
72pub fn commutator_pauli_ty() -> Expr {
75 arrow(
76 app(cst("PauliGroup"), nat_ty()),
77 arrow(app(cst("PauliGroup"), nat_ty()), bool_ty()),
78 )
79}
80pub fn weight_pauli_ty() -> Expr {
83 arrow(app(cst("PauliGroup"), nat_ty()), nat_ty())
84}
85pub fn pauli_group_non_abelian_ty() -> Expr {
88 prop()
89}
90pub fn pauli_square_identity_ty() -> Expr {
93 prop()
94}
95pub fn pauli_anti_commute_ty() -> Expr {
98 prop()
99}
100pub fn stabilizer_group_ty() -> Expr {
103 arrow(nat_ty(), arrow(nat_ty(), type0()))
104}
105pub fn stabilizer_code_ty() -> Expr {
108 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
109}
110pub fn error_syndrome_ty() -> Expr {
113 arrow(nat_ty(), arrow(nat_ty(), type0()))
114}
115pub fn logical_operator_ty() -> Expr {
118 arrow(
119 app3(cst("StabilizerCode"), nat_ty(), nat_ty(), nat_ty()),
120 arrow(app(cst("PauliGroup"), nat_ty()), type0()),
121 )
122}
123pub fn decoder_map_ty() -> Expr {
126 arrow(nat_ty(), arrow(nat_ty(), type0()))
127}
128pub fn code_distance_ty() -> Expr {
131 arrow(
132 app3(cst("StabilizerCode"), nat_ty(), nat_ty(), nat_ty()),
133 nat_ty(),
134 )
135}
136pub fn knill_laflamme_conditions_ty() -> Expr {
139 prop()
140}
141pub fn quantum_singleton_bound_ty() -> Expr {
144 prop()
145}
146pub fn quantum_hamming_bound_ty() -> Expr {
149 prop()
150}
151pub fn gottesman_knill_theorem_ty() -> Expr {
154 prop()
155}
156pub fn classical_code_ty() -> Expr {
159 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
160}
161pub fn css_code_ty() -> Expr {
164 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
165}
166pub fn css_x_stabilizer_ty() -> Expr {
169 arrow(app3(cst("CSSCode"), nat_ty(), nat_ty(), nat_ty()), type0())
170}
171pub fn css_z_stabilizer_ty() -> Expr {
174 arrow(app3(cst("CSSCode"), nat_ty(), nat_ty(), nat_ty()), type0())
175}
176pub fn css_construction_ty() -> Expr {
179 prop()
180}
181pub fn css_transversal_cnot_ty() -> Expr {
184 prop()
185}
186pub fn shor_code_ty() -> Expr {
189 type0()
190}
191pub fn shor_logical_zero_ty() -> Expr {
194 arrow(cst("ShorCode"), type0())
195}
196pub fn shor_logical_one_ty() -> Expr {
199 arrow(cst("ShorCode"), type0())
200}
201pub fn shor_bit_flip_code_ty() -> Expr {
204 type0()
205}
206pub fn shor_phase_flip_code_ty() -> Expr {
209 type0()
210}
211pub fn shor_corrects_single_errors_ty() -> Expr {
214 prop()
215}
216pub fn shor_is_css_ty() -> Expr {
219 prop()
220}
221pub fn steane_code_ty() -> Expr {
224 type0()
225}
226pub fn steane_h_matrix_ty() -> Expr {
229 type0()
230}
231pub fn steane_stabilizer_ty() -> Expr {
234 arrow(cst("SteaneCode"), arrow(nat_ty(), type0()))
235}
236pub fn steane_corrects_single_errors_ty() -> Expr {
239 prop()
240}
241pub fn steane_transversal_clifford_ty() -> Expr {
244 prop()
245}
246pub fn steane_is_css_ty() -> Expr {
249 prop()
250}
251pub fn fault_tolerant_gate_ty() -> Expr {
254 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
255}
256pub fn transversal_gate_ty() -> Expr {
259 type0()
260}
261pub fn magic_state_injection_ty() -> Expr {
264 type0()
265}
266pub fn teleported_gate_ty() -> Expr {
269 type0()
270}
271pub fn easttin_knill_theorem_ty() -> Expr {
274 prop()
275}
276pub fn transversal_clifford_css_ty() -> Expr {
279 prop()
280}
281pub fn magic_state_distillation_works_ty() -> Expr {
284 prop()
285}
286pub fn error_threshold_ty() -> Expr {
289 real_ty()
290}
291pub fn concatenated_code_ty() -> Expr {
294 arrow(
295 nat_ty(),
296 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0()))),
297 )
298}
299pub fn fault_tolerance_overhead_ty() -> Expr {
302 arrow(nat_ty(), arrow(nat_ty(), real_ty()))
303}
304pub fn pseudo_threshold_ty() -> Expr {
307 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), real_ty())))
308}
309pub fn threshold_theorem_ty() -> Expr {
312 prop()
313}
314pub fn concatenated_code_distance_ty() -> Expr {
317 prop()
318}
319pub fn polytope_bound_ty() -> Expr {
322 prop()
323}
324pub fn color_code_ty() -> Expr {
327 arrow(nat_ty(), type0())
328}
329pub fn color_code_lattice_ty() -> Expr {
332 type0()
333}
334pub fn color_code_syndrome_ty() -> Expr {
337 arrow(nat_ty(), type0())
338}
339pub fn color_code_transversal_t_ty() -> Expr {
342 prop()
343}
344pub fn color_code_equivalent_ty() -> Expr {
347 prop()
348}
349pub fn quantum_capacity_ty() -> Expr {
352 arrow(type0(), real_ty())
353}
354pub fn coherent_information_ty() -> Expr {
357 arrow(type0(), arrow(type0(), real_ty()))
358}
359pub fn hashing_bound_ty() -> Expr {
362 arrow(real_ty(), real_ty())
363}
364pub fn quantum_erasure_capacity_ty() -> Expr {
367 arrow(real_ty(), real_ty())
368}
369pub fn quantum_noise_threshold_ty() -> Expr {
372 prop()
373}
374pub fn quantum_shannon_theorem_ty() -> Expr {
377 prop()
378}
379pub fn no_cloning_capacity_ty() -> Expr {
382 prop()
383}
384pub fn kl_matrix_ty() -> Expr {
387 arrow(nat_ty(), arrow(nat_ty(), type0()))
388}
389pub fn kl_error_set_ty() -> Expr {
392 arrow(nat_ty(), type0())
393}
394pub fn kl_correctable_code_ty() -> Expr {
397 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
398}
399pub fn kl_necessary_condition_ty() -> Expr {
402 arrow(
403 app(cst("KLErrorSet"), nat_ty()),
404 arrow(
405 app3(cst("StabilizerCode"), nat_ty(), nat_ty(), nat_ty()),
406 prop(),
407 ),
408 )
409}
410pub fn kl_sufficient_condition_ty() -> Expr {
413 arrow(
414 app(cst("KLErrorSet"), nat_ty()),
415 arrow(
416 app3(cst("StabilizerCode"), nat_ty(), nat_ty(), nat_ty()),
417 prop(),
418 ),
419 )
420}
421pub fn kl_equivalence_ty() -> Expr {
424 prop()
425}
426pub fn kl_degenerate_code_ty() -> Expr {
429 prop()
430}
431pub fn reed_muller_code_ty() -> Expr {
434 arrow(nat_ty(), arrow(nat_ty(), type0()))
435}
436pub fn quantum_reed_muller_code_ty() -> Expr {
439 arrow(nat_ty(), arrow(nat_ty(), type0()))
440}
441pub fn rm_transversal_t_ty() -> Expr {
444 arrow(nat_ty(), arrow(nat_ty(), prop()))
445}
446pub fn rm_code_concatenation_ty() -> Expr {
449 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
450}
451pub fn rm_transversal_universal_ty() -> Expr {
454 prop()
455}
456pub fn rm_code_distance_ty() -> Expr {
459 prop()
460}
461pub fn surface_code_ty() -> Expr {
464 arrow(nat_ty(), type0())
465}
466pub fn toric_code_ty() -> Expr {
469 arrow(nat_ty(), type0())
470}
471pub fn surface_code_vertex_ty() -> Expr {
474 arrow(nat_ty(), type0())
475}
476pub fn surface_code_plaquette_ty() -> Expr {
479 arrow(nat_ty(), type0())
480}
481pub fn surface_code_logical_ty() -> Expr {
484 arrow(nat_ty(), type0())
485}
486pub fn surface_code_distance_ty() -> Expr {
489 prop()
490}
491pub fn toric_code_anyons_ty() -> Expr {
494 prop()
495}
496pub fn surface_code_threshold_ty() -> Expr {
499 prop()
500}
501pub fn qldpc_code_ty() -> Expr {
504 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
505}
506pub fn fiber_bundle_code_ty() -> Expr {
509 arrow(nat_ty(), arrow(nat_ty(), type0()))
510}
511pub fn good_qldpc_code_ty() -> Expr {
514 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), prop())))
515}
516pub fn qldpc_tanner_ty() -> Expr {
519 arrow(nat_ty(), type0())
520}
521pub fn qldpc_good_codes_ty() -> Expr {
524 prop()
525}
526pub fn fiber_bundle_distance_ty() -> Expr {
529 prop()
530}
531pub fn bosonic_code_ty() -> Expr {
534 type0()
535}
536pub fn cat_code_ty() -> Expr {
539 arrow(nat_ty(), type0())
540}
541pub fn gkp_code_ty() -> Expr {
544 arrow(real_ty(), type0())
545}
546pub fn binomial_code_ty() -> Expr {
549 arrow(nat_ty(), arrow(nat_ty(), type0()))
550}
551pub fn gkp_displacement_error_ty() -> Expr {
554 arrow(real_ty(), arrow(real_ty(), type0()))
555}
556pub fn gkp_corrects_bounded_displacement_ty() -> Expr {
559 prop()
560}
561pub fn cat_code_loss_tolerance_ty() -> Expr {
564 prop()
565}
566pub fn noise_model_ty() -> Expr {
569 type0()
570}
571pub fn depolarizing_noise_ty() -> Expr {
574 arrow(real_ty(), type0())
575}
576pub fn circuit_noise_threshold_ty() -> Expr {
579 arrow(cst("NoiseModel"), real_ty())
580}
581pub fn fault_tolerance_gadget_ty() -> Expr {
584 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
585}
586pub fn circuit_threshold_theorem_ty() -> Expr {
589 prop()
590}
591pub fn threshold_upper_bound_ty() -> Expr {
594 prop()
595}
596pub fn magic_state_ty() -> Expr {
599 type0()
600}
601pub fn t_state_ty() -> Expr {
604 type0()
605}
606pub fn distillation_protocol_ty() -> Expr {
609 arrow(nat_ty(), arrow(nat_ty(), arrow(real_ty(), type0())))
610}
611pub fn magic_state_robustness_ty() -> Expr {
614 real_ty()
615}
616pub fn bravyi_kitaev_distillation_ty() -> Expr {
619 prop()
620}
621pub fn magic_state_non_stabilizer_ty() -> Expr {
624 prop()
625}
626pub fn mwpm_decoder_ty() -> Expr {
629 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
630}
631pub fn belief_prop_decoder_ty() -> Expr {
634 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
635}
636pub fn union_find_decoder_ty() -> Expr {
639 arrow(nat_ty(), type0())
640}
641pub fn ml_decoder_ty() -> Expr {
644 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0())))
645}
646pub fn decoding_threshold_ty() -> Expr {
649 arrow(type0(), real_ty())
650}
651pub fn mwpm_optimal_surface_ty() -> Expr {
654 prop()
655}
656pub fn bp_convergence_ldpc_ty() -> Expr {
659 prop()
660}
661pub fn subsystem_code_ty() -> Expr {
664 arrow(
665 nat_ty(),
666 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0()))),
667 )
668}
669pub fn gauge_group_ty() -> Expr {
672 arrow(
673 nat_ty(),
674 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0()))),
675 )
676}
677pub fn bacon_shor_code_ty() -> Expr {
680 arrow(nat_ty(), type0())
681}
682pub fn operator_qec_ty() -> Expr {
685 arrow(
686 nat_ty(),
687 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), prop()))),
688 )
689}
690pub fn subsystem_code_correction_ty() -> Expr {
693 prop()
694}
695pub fn bacon_shor_single_fault_tolerant_ty() -> Expr {
698 prop()
699}
700pub fn code_switching_ty() -> Expr {
703 arrow(
704 nat_ty(),
705 arrow(
706 nat_ty(),
707 arrow(
708 nat_ty(),
709 arrow(nat_ty(), arrow(nat_ty(), arrow(nat_ty(), type0()))),
710 ),
711 ),
712 )
713}
714pub fn gate_synthesis_ty() -> Expr {
717 arrow(nat_ty(), arrow(real_ty(), type0()))
718}
719pub fn solovay_kitaev_approx_ty() -> Expr {
722 arrow(real_ty(), nat_ty())
723}
724pub fn solovay_kitaev_theorem_ty() -> Expr {
727 prop()
728}
729pub fn t_count_optimality_ty() -> Expr {
732 prop()
733}
734#[allow(clippy::too_many_arguments)]
735pub fn build_quantum_error_correction_env(env: &mut Environment) -> Result<(), String> {
736 let axioms: &[(&str, Expr)] = &[
737 ("PauliOp", pauli_op_ty()),
738 ("PauliGroup", pauli_group_ty()),
739 ("PauliPhase", pauli_phase_ty()),
740 ("SymplecticRepresentation", symplectic_representation_ty()),
741 ("CommutatorPauli", commutator_pauli_ty()),
742 ("WeightPauli", weight_pauli_ty()),
743 ("PauliGroupNonAbelian", pauli_group_non_abelian_ty()),
744 ("PauliSquareIdentity", pauli_square_identity_ty()),
745 ("PauliAntiCommute", pauli_anti_commute_ty()),
746 ("StabilizerGroup", stabilizer_group_ty()),
747 ("StabilizerCode", stabilizer_code_ty()),
748 ("ErrorSyndrome", error_syndrome_ty()),
749 ("LogicalOperator", logical_operator_ty()),
750 ("DecoderMap", decoder_map_ty()),
751 ("CodeDistance", code_distance_ty()),
752 ("KnillLaflammeConditions", knill_laflamme_conditions_ty()),
753 ("QuantumSingletonBound", quantum_singleton_bound_ty()),
754 ("QuantumHammingBound", quantum_hamming_bound_ty()),
755 ("GottesmanKnillTheorem", gottesman_knill_theorem_ty()),
756 ("ClassicalCode", classical_code_ty()),
757 ("CSSCode", css_code_ty()),
758 ("CSSXStabilizer", css_x_stabilizer_ty()),
759 ("CSSZStabilizer", css_z_stabilizer_ty()),
760 ("CSSConstruction", css_construction_ty()),
761 ("CSSTransversalCNOT", css_transversal_cnot_ty()),
762 ("ShorCode", shor_code_ty()),
763 ("ShorLogicalZero", shor_logical_zero_ty()),
764 ("ShorLogicalOne", shor_logical_one_ty()),
765 ("ShorBitFlipCode", shor_bit_flip_code_ty()),
766 ("ShorPhaseFlipCode", shor_phase_flip_code_ty()),
767 ("ShorCorrectsSingleErrors", shor_corrects_single_errors_ty()),
768 ("ShorIsCSS", shor_is_css_ty()),
769 ("SteaneCode", steane_code_ty()),
770 ("SteaneHMatrix", steane_h_matrix_ty()),
771 ("SteaneStabilizer", steane_stabilizer_ty()),
772 (
773 "SteaneCorrectsSingleErrors",
774 steane_corrects_single_errors_ty(),
775 ),
776 (
777 "SteaneTransversalClifford",
778 steane_transversal_clifford_ty(),
779 ),
780 ("SteaneIsCSS", steane_is_css_ty()),
781 ("FaultTolerantGate", fault_tolerant_gate_ty()),
782 ("TransversalGate", transversal_gate_ty()),
783 ("MagicStateInjection", magic_state_injection_ty()),
784 ("TeleportedGate", teleported_gate_ty()),
785 ("EasttinKnillTheorem", easttin_knill_theorem_ty()),
786 ("TransversalCliffordCSS", transversal_clifford_css_ty()),
787 (
788 "MagicStateDistillationWorks",
789 magic_state_distillation_works_ty(),
790 ),
791 ("ErrorThreshold", error_threshold_ty()),
792 ("ConcatenatedCode", concatenated_code_ty()),
793 ("FaultToleranceOverhead", fault_tolerance_overhead_ty()),
794 ("PseudoThreshold", pseudo_threshold_ty()),
795 ("ThresholdTheorem", threshold_theorem_ty()),
796 ("ConcatenatedCodeDistance", concatenated_code_distance_ty()),
797 ("PolytopeBound", polytope_bound_ty()),
798 ("ColorCode", color_code_ty()),
799 ("ColorCodeLattice", color_code_lattice_ty()),
800 ("ColorCodeSyndrome", color_code_syndrome_ty()),
801 ("ColorCodeTransversalT", color_code_transversal_t_ty()),
802 ("ColorCodeEquivalent", color_code_equivalent_ty()),
803 ("QuantumCapacity", quantum_capacity_ty()),
804 ("CoherentInformation", coherent_information_ty()),
805 ("HashingBound", hashing_bound_ty()),
806 ("QuantumErasureCapacity", quantum_erasure_capacity_ty()),
807 ("QuantumNoiseThreshold", quantum_noise_threshold_ty()),
808 ("QuantumShannonTheorem", quantum_shannon_theorem_ty()),
809 ("NoCloning", no_cloning_capacity_ty()),
810 ("KLMatrix", kl_matrix_ty()),
811 ("KLErrorSet", kl_error_set_ty()),
812 ("KLCorrectableCode", kl_correctable_code_ty()),
813 ("KLNecessaryCondition", kl_necessary_condition_ty()),
814 ("KLSufficientCondition", kl_sufficient_condition_ty()),
815 ("KLEquivalence", kl_equivalence_ty()),
816 ("KLDegenerateCode", kl_degenerate_code_ty()),
817 ("ReedMullerCode", reed_muller_code_ty()),
818 ("QuantumReedMullerCode", quantum_reed_muller_code_ty()),
819 ("RMTransversalT", rm_transversal_t_ty()),
820 ("RMCodeConcatenation", rm_code_concatenation_ty()),
821 ("RMTransversalUniversal", rm_transversal_universal_ty()),
822 ("RMCodeDistance", rm_code_distance_ty()),
823 ("SurfaceCode", surface_code_ty()),
824 ("ToricCode", toric_code_ty()),
825 ("SurfaceCodeVertex", surface_code_vertex_ty()),
826 ("SurfaceCodePlaquette", surface_code_plaquette_ty()),
827 ("SurfaceCodeLogical", surface_code_logical_ty()),
828 ("SurfaceCodeDistance", surface_code_distance_ty()),
829 ("ToricCodeAnyons", toric_code_anyons_ty()),
830 ("SurfaceCodeThreshold", surface_code_threshold_ty()),
831 ("QLDPCCode", qldpc_code_ty()),
832 ("FiberBundleCode", fiber_bundle_code_ty()),
833 ("GoodQLDPCCode", good_qldpc_code_ty()),
834 ("QLDPCTanner", qldpc_tanner_ty()),
835 ("QLDPCGoodCodes", qldpc_good_codes_ty()),
836 ("FiberBundleDistance", fiber_bundle_distance_ty()),
837 ("BosonicCode", bosonic_code_ty()),
838 ("CatCode", cat_code_ty()),
839 ("GKPCode", gkp_code_ty()),
840 ("BinomialCode", binomial_code_ty()),
841 ("GKPDisplacementError", gkp_displacement_error_ty()),
842 (
843 "GKPCorrectsBoundedDisplacement",
844 gkp_corrects_bounded_displacement_ty(),
845 ),
846 ("CatCodeLossTolerance", cat_code_loss_tolerance_ty()),
847 ("NoiseModel", noise_model_ty()),
848 ("DepolarizingNoise", depolarizing_noise_ty()),
849 ("CircuitNoiseThreshold", circuit_noise_threshold_ty()),
850 ("FaultToleranceGadget", fault_tolerance_gadget_ty()),
851 ("CircuitThresholdTheorem", circuit_threshold_theorem_ty()),
852 ("ThresholdUpperBound", threshold_upper_bound_ty()),
853 ("MagicState", magic_state_ty()),
854 ("TState", t_state_ty()),
855 ("DistillationProtocol", distillation_protocol_ty()),
856 ("MagicStateRobustness", magic_state_robustness_ty()),
857 ("BravyiKitaevDistillation", bravyi_kitaev_distillation_ty()),
858 ("MagicStateNonStabilizer", magic_state_non_stabilizer_ty()),
859 ("MWPMDecoder", mwpm_decoder_ty()),
860 ("BeliefPropDecoder", belief_prop_decoder_ty()),
861 ("UnionFindDecoder", union_find_decoder_ty()),
862 ("MLDecoder", ml_decoder_ty()),
863 ("DecodingThreshold", decoding_threshold_ty()),
864 ("MWPMOptimalSurface", mwpm_optimal_surface_ty()),
865 ("BPConvergenceLDPC", bp_convergence_ldpc_ty()),
866 ("SubsystemCode", subsystem_code_ty()),
867 ("GaugeGroup", gauge_group_ty()),
868 ("BaconShorCode", bacon_shor_code_ty()),
869 ("OperatorQEC", operator_qec_ty()),
870 ("SubsystemCodeCorrection", subsystem_code_correction_ty()),
871 (
872 "BaconShorSingleFaultTolerant",
873 bacon_shor_single_fault_tolerant_ty(),
874 ),
875 ("CodeSwitching", code_switching_ty()),
876 ("GateSynthesis", gate_synthesis_ty()),
877 ("SolovayKitaevApprox", solovay_kitaev_approx_ty()),
878 ("SolovayKitaevTheorem", solovay_kitaev_theorem_ty()),
879 ("TCountOptimality", t_count_optimality_ty()),
880 ];
881 for (name, ty) in axioms {
882 env.add(Declaration::Axiom {
883 name: Name::str(*name),
884 univ_params: vec![],
885 ty: ty.clone(),
886 })
887 .map_err(|e| format!("Failed to add '{}': {:?}", name, e))?;
888 }
889 Ok(())
890}
891pub fn binary_entropy(p: f64) -> f64 {
893 if p <= 0.0 || p >= 1.0 {
894 return 0.0;
895 }
896 -p * p.log2() - (1.0 - p) * (1.0 - p).log2()
897}
898pub fn hashing_bound(p: f64) -> f64 {
901 let q = 1.0 - binary_entropy(p) - p * 3.0_f64.log2();
902 q.max(0.0)
903}
904pub fn erasure_channel_capacity(epsilon: f64) -> f64 {
907 (1.0 - 2.0 * epsilon).max(0.0)
908}
909pub fn amplitude_damping_capacity(gamma: f64) -> f64 {
912 (1.0 - binary_entropy(gamma)).max(0.0)
913}
914pub fn distill_t_state_fidelity(f_in: f64) -> f64 {
917 let eps = 1.0 - f_in;
918 let f_out = 1.0 - 35.0 * eps.powi(3);
919 f_out.min(1.0).max(0.0)
920}
921pub fn distillation_rounds(f_init: f64, f_target: f64) -> usize {
923 let mut f = f_init;
924 let mut rounds = 0;
925 while f < f_target && rounds < 100 {
926 f = distill_t_state_fidelity(f);
927 rounds += 1;
928 }
929 rounds
930}
931pub fn distillation_overhead(rounds: usize) -> u64 {
934 15u64.pow(rounds as u32)
935}
936#[cfg(test)]
937mod tests {
938 use super::*;
939 #[test]
940 fn test_pauli_commutation() {
941 assert!(!PauliOp::X.commutes_with(PauliOp::Z));
942 assert!(!PauliOp::X.commutes_with(PauliOp::Y));
943 assert!(PauliOp::X.commutes_with(PauliOp::X));
944 for &p in &[PauliOp::I, PauliOp::X, PauliOp::Y, PauliOp::Z] {
945 assert!(PauliOp::I.commutes_with(p));
946 }
947 }
948 #[test]
949 fn test_pauli_string_commutation() {
950 let a = PauliString::new(vec![PauliOp::Z, PauliOp::Z, PauliOp::I]);
951 let b = PauliString::new(vec![PauliOp::I, PauliOp::Z, PauliOp::Z]);
952 assert!(a.commutes_with(&b));
953 let c = PauliString::new(vec![PauliOp::X, PauliOp::X, PauliOp::I]);
954 assert!(!c.commutes_with(&b));
955 }
956 #[test]
957 fn test_stabilizer_code_3qubit() {
958 let code = StabilizerCode::bit_flip_3();
959 assert_eq!(code.n, 3);
960 assert_eq!(code.k, 1);
961 let x_err = vec![0u8; 3];
962 let z_err = vec![0u8; 3];
963 let synd = code.syndrome(&x_err, &z_err);
964 assert!(synd.iter().all(|&s| s == 0));
965 let x_err1 = vec![1u8, 0, 0];
966 assert!(code.detects_error(&x_err1, &z_err));
967 }
968 #[test]
969 fn test_steane_7_code() {
970 let code = StabilizerCode::steane_7();
971 assert_eq!(code.n, 7);
972 assert_eq!(code.k, 1);
973 assert_eq!(code.d, 3);
974 let x_err = vec![0u8; 7];
975 let z_err = vec![0u8; 7];
976 let synd = code.syndrome(&x_err, &z_err);
977 assert!(
978 synd.iter().all(|&s| s == 0),
979 "No-error syndrome should be zero"
980 );
981 assert!(code.satisfies_singleton_bound());
982 assert!(code.satisfies_hamming_bound());
983 }
984 #[test]
985 fn test_shor_code() {
986 let shor = ShorCode::new();
987 assert_eq!(shor.n(), 9);
988 assert_eq!(shor.k(), 1);
989 assert_eq!(shor.d(), 3);
990 assert!(shor.corrects_single_qubit_error());
991 assert!(shor.satisfies_singleton_bound());
992 let x_err = vec![0u8; 9];
993 let z_err = vec![0u8; 9];
994 let synd = shor.syndrome(&x_err, &z_err);
995 assert!(synd.iter().all(|&s| s == 0));
996 }
997 #[test]
998 fn test_css_steane_syndrome() {
999 let css = CSSCode::steane();
1000 let no_err = vec![0u8; 7];
1001 assert!(!css.detects_x_error(&no_err));
1002 assert!(!css.detects_z_error(&no_err));
1003 let mut x_err = vec![0u8; 7];
1004 x_err[0] = 1;
1005 assert!(css.detects_x_error(&x_err));
1006 }
1007 #[test]
1008 fn test_hashing_bound() {
1009 assert!((hashing_bound(0.0) - 1.0).abs() < 1e-9);
1010 let q = hashing_bound(0.25);
1011 assert!(q >= 0.0);
1012 assert!((erasure_channel_capacity(0.0) - 1.0).abs() < 1e-9);
1013 assert!((erasure_channel_capacity(0.5)).abs() < 1e-9);
1014 assert!(erasure_channel_capacity(0.3) > 0.0);
1015 }
1016 #[test]
1017 fn test_magic_state_distillation() {
1018 let f_init = 0.95;
1019 let f_out = distill_t_state_fidelity(f_init);
1020 assert!(f_out > f_init, "Distillation should improve fidelity");
1021 let rounds = distillation_rounds(f_init, 0.9999);
1022 assert!(rounds >= 1);
1023 assert_eq!(distillation_overhead(1), 15);
1024 assert_eq!(distillation_overhead(2), 225);
1025 }
1026 #[test]
1027 fn test_concatenated_code() {
1028 let cc = ConcatenatedCode::new(7, 3, 2);
1029 assert_eq!(cc.num_physical_qubits(), 49);
1030 assert_eq!(cc.distance(), 9);
1031 let cc1 = ConcatenatedCode::new(7, 3, 1);
1032 let cc2 = ConcatenatedCode::new(7, 3, 2);
1033 let p = 0.001;
1034 let th = 0.01;
1035 assert!(cc2.logical_error_rate(p, th) < cc1.logical_error_rate(p, th));
1036 }
1037 #[test]
1038 fn test_build_quantum_error_correction_env() {
1039 let mut env = oxilean_kernel::Environment::new();
1040 let result = build_quantum_error_correction_env(&mut env);
1041 assert!(
1042 result.is_ok(),
1043 "build_quantum_error_correction_env failed: {:?}",
1044 result.err()
1045 );
1046 }
1047 #[test]
1048 fn test_surface_code_decoder() {
1049 let decoder = SurfaceCodeDecoder::new(3);
1050 let corrections = decoder.decode(&[]);
1051 assert!(corrections.is_empty());
1052 let d3 = SurfaceCodeDecoder::new(3);
1053 let d5 = SurfaceCodeDecoder::new(5);
1054 let p = 0.001;
1055 assert!(d5.logical_error_rate(p) < d3.logical_error_rate(p));
1056 assert!(d3.logical_error_rate(0.005) < 0.5);
1057 }
1058 #[test]
1059 fn test_surface_code_decoder_syndrome_matching() {
1060 let decoder = SurfaceCodeDecoder::new(5);
1061 let syndrome = vec![(1, 1), (1, 2)];
1062 let corrections = decoder.decode(&syndrome);
1063 assert!(!corrections.is_empty());
1064 }
1065 #[test]
1066 fn test_stabilizer_simulator_init() {
1067 let sim = StabilizerSimulator::new(3);
1068 assert_eq!(sim.measure_z(0), 0);
1069 assert_eq!(sim.measure_z(1), 0);
1070 assert_eq!(sim.measure_z(2), 0);
1071 }
1072 #[test]
1073 fn test_stabilizer_simulator_hadamard() {
1074 let mut sim = StabilizerSimulator::new(2);
1075 sim.hadamard(0);
1076 assert_eq!(sim.stab_x[0][0], 1);
1077 assert_eq!(sim.stab_z[0][0], 0);
1078 }
1079 #[test]
1080 fn test_stabilizer_simulator_phase_gate() {
1081 let mut sim = StabilizerSimulator::new(1);
1082 sim.phase_gate(0);
1083 assert_eq!(sim.stab_z[0][0], 1);
1084 assert_eq!(sim.stab_x[0][0], 0);
1085 }
1086 #[test]
1087 fn test_fault_tolerance_threshold() {
1088 let ft = FaultToleranceThreshold::new(7, 1, 3);
1089 let p_th = ft.estimate_threshold();
1090 assert!(p_th > 0.0);
1091 assert!((ft.logical_error_rate(p_th, 1) - 0.5).abs() < 1e-10);
1092 let p = p_th * 0.5;
1093 assert!(ft.logical_error_rate(p, 2) < ft.logical_error_rate(p, 1));
1094 let level = ft.min_level(p, 1e-6);
1095 assert!(level < u32::MAX);
1096 }
1097 #[test]
1098 fn test_qec_norm_checker_steane() {
1099 let code = StabilizerCode::steane_7();
1100 let checker = QECNormChecker::new(code);
1101 assert!(
1102 checker.all_single_qubit_errors_correctable(),
1103 "Steane code should correct all single-qubit errors"
1104 );
1105 }
1106 #[test]
1107 fn test_qec_norm_checker_kl_condition() {
1108 let code = StabilizerCode::steane_7();
1109 let checker = QECNormChecker::new(code);
1110 assert!(checker.satisfies_kl_conditions(&[0u8; 7], &[0u8; 7]));
1111 let mut x_err = vec![0u8; 7];
1112 x_err[0] = 1;
1113 assert!(checker.satisfies_kl_conditions(&x_err, &[0u8; 7]));
1114 }
1115 #[test]
1116 fn test_gkp_code_simulator_correction() {
1117 let gkp = GKPCodeSimulator::new();
1118 let delta = gkp.lattice_spacing;
1119 assert!(gkp.is_correctable(delta * 0.1, delta * 0.1));
1120 assert!(!gkp.is_correctable(delta * 0.6, 0.0));
1121 let (rq, rp) = gkp.correct_displacement(0.1, 0.05);
1122 assert!(rq.abs() < delta / 2.0);
1123 assert!(rp.abs() < delta / 2.0);
1124 }
1125 #[test]
1126 fn test_gkp_code_simulator_logical_error_rate() {
1127 let gkp = GKPCodeSimulator::new();
1128 let rate_small = gkp.logical_error_rate_gaussian(0.1);
1129 let rate_large = gkp.logical_error_rate_gaussian(0.5);
1130 assert!(
1131 rate_small < rate_large,
1132 "Larger noise should give larger error rate"
1133 );
1134 assert_eq!(gkp.logical_error_rate_gaussian(0.0), 0.0);
1135 }
1136 #[test]
1137 fn test_gkp_simulate_errors() {
1138 let gkp = GKPCodeSimulator::new();
1139 let delta = gkp.lattice_spacing;
1140 let displacements = vec![
1141 (delta * 0.1, delta * 0.1),
1142 (delta * 0.4, delta * 0.4),
1143 (delta * 0.6, 0.0),
1144 ];
1145 let uncorrected = gkp.simulate_errors(&displacements);
1146 assert_eq!(
1147 uncorrected, 1,
1148 "Only one displacement should be uncorrectable"
1149 );
1150 }
1151}
1152#[cfg(test)]
1153mod tests_qec_extra {
1154 use super::*;
1155 #[test]
1156 fn test_pauli_commutation() {
1157 let x = PauliOperator::single_x(2, 0);
1158 let z = PauliOperator::single_z(2, 0);
1159 assert!(!x.commutes_with(&z), "X and Z anticommute");
1160 let x1 = PauliOperator::single_x(2, 0);
1161 let x2 = PauliOperator::single_x(2, 1);
1162 assert!(x1.commutes_with(&x2), "X on different qubits commute");
1163 }
1164 #[test]
1165 fn test_five_qubit_code() {
1166 let code = StabCode::five_qubit_code();
1167 assert_eq!(code.n_physical, 5);
1168 assert_eq!(code.k_logical, 1);
1169 assert_eq!(code.generators.len(), 4);
1170 for i in 0..4 {
1171 for j in (i + 1)..4 {
1172 assert!(
1173 code.generators[i].commutes_with(&code.generators[j]),
1174 "Generator {} must commute with {}",
1175 i,
1176 j
1177 );
1178 }
1179 }
1180 }
1181 #[test]
1182 fn test_steane_code_structure() {
1183 let code = StabCode::steane_code();
1184 assert_eq!(code.n_physical, 7);
1185 assert_eq!(code.k_logical, 1);
1186 assert_eq!(code.generators.len(), 6);
1187 }
1188 #[test]
1189 fn test_syndrome_decoder_weight1() {
1190 let code = StabCode::five_qubit_code();
1191 let mut decoder = SyndromeDecoder2::new(code);
1192 decoder.build_weight1_table();
1193 assert!(decoder.lookup_table.len() <= 15);
1194 }
1195 #[test]
1196 fn test_threshold_estimator() {
1197 let est = ThresholdEstimator::new(0.001, 3, 10);
1198 let threshold = 0.01;
1199 assert!(est.is_below_threshold(threshold));
1200 let logical_rate = est.logical_error_rate(threshold);
1201 assert!(
1202 logical_rate < est.physical_error_rate,
1203 "Logical rate should improve below threshold"
1204 );
1205 }
1206 #[test]
1207 fn test_surface_code() {
1208 let sc = SurfaceCode::new(5);
1209 assert_eq!(sc.code_distance(), 5);
1210 assert_eq!(sc.n_data_qubits, 25);
1211 assert!(sc.total_qubits() > 25);
1212 assert!(SurfaceCode::depolarizing_threshold() > 0.0);
1213 }
1214 #[test]
1215 fn test_magic_state_distillation() {
1216 let msd = MagicStateDistillation::new(0.01, 1e-10, MagicStateProtocol::FifteenToOne);
1217 let out = msd.output_error_rate();
1218 assert!(
1219 out < msd.input_error_rate,
1220 "Distillation should reduce error"
1221 );
1222 let rounds = msd.n_rounds_needed();
1223 assert!(rounds > 0);
1224 }
1225 #[test]
1226 fn test_quantum_ldpc() {
1227 let code = QuantumLDPC::new(100, 10, 10, 6, 6);
1228 assert!(code.rate() > 0.0);
1229 let hp = QuantumLDPC::hypergraph_product(4, 8, 4, 8);
1230 assert!(hp.n > 0);
1231 }
1232}