quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Topological Quantum Computing Example
//!
//! This example demonstrates topological quantum computing concepts including:
//! - Anyonic systems and braiding operations
//! - Fibonacci anyons and their fusion rules
//! - Topological error correction
//! - Surface codes and their error correction capabilities
//! - Majorana fermions and topological qubits
//!
//! Topological quantum computing leverages topological properties of matter
//! for inherently fault-tolerant quantum computation.
//!
//! Run with: cargo run --example topological_quantum_computing

use quantrs2_core::{gate::GateOp, qubit::QubitId};
use scirs2_core::Complex64;

fn main() {
    println!("=================================================================");
    println!("   QuantRS2-Core: Topological Quantum Computing");
    println!("=================================================================\n");

    // Demonstrate anyonic systems
    demonstrate_anyonic_systems();
    println!();

    // Demonstrate braiding operations
    demonstrate_braiding_operations();
    println!();

    // Demonstrate topological error correction
    demonstrate_topological_error_correction();
    println!();

    // Demonstrate surface codes
    demonstrate_surface_codes();
    println!();

    // Demonstrate Majorana fermions
    demonstrate_majorana_fermions();
    println!();

    println!("=================================================================");
    println!("   Example Complete!");
    println!("=================================================================");
}

/// Demonstrate anyonic systems and their exotic statistics
fn demonstrate_anyonic_systems() {
    println!("ANYONIC SYSTEMS & EXOTIC STATISTICS");
    println!("-----------------------------------------------------------------");

    println!("Anyons are quasi-particles that exist only in 2D systems.");
    println!("They have exotic exchange statistics beyond bosons and fermions.");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Particle Statistics in Quantum Mechanics");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("1. BOSONS (3D and higher)");
    println!("   Exchange two identical bosons:");
    println!("     |ψ⟩ → |ψ⟩  (symmetric)");
    println!("   Phase acquired: e^(i×0) = 1");
    println!("   Examples: photons, phonons, ⁴He atoms");
    println!();

    println!("2. FERMIONS (3D and higher)");
    println!("   Exchange two identical fermions:");
    println!("     |ψ⟩ → −|ψ⟩  (antisymmetric)");
    println!("   Phase acquired: e^(i×π) = −1");
    println!("   Examples: electrons, quarks, ³He atoms");
    println!("   Consequence: Pauli exclusion principle");
    println!();

    println!("3. ANYONS (2D only!)");
    println!("   Exchange two identical anyons:");
    println!("     |ψ⟩ → e^(iθ)|ψ⟩  (any phase θ)");
    println!("   Phase can be ANY value: 0 ≤ θ < 2π");
    println!("   Only possible in 2D due to topological constraints");
    println!();

    println!("Why 2D is Special:");
    println!("  • In 3D: Particle exchange paths are homotopic");
    println!("           (can be continuously deformed into each other)");
    println!("  • In 2D: Particle trajectories create braids");
    println!("           (distinct topological classes)");
    println!("  • Braid group ≠ permutation group in 2D");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Types of Anyons");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("ABELIAN ANYONS");
    println!("-----------------------------------------------------------------");
    println!();
    println!("  Properties:");
    println!("    • Exchange yields scalar phase: e^(iθ)");
    println!("    • Fusion outcome unique");
    println!("    • Examples: Fractional quantum Hall states (ν=1/3)");
    println!();
    println!("  Fusion Rule Example:");
    println!("    a × a = 1  (anyon fuses with itself → vacuum)");
    println!("    a × 1 = a  (fusion with vacuum → anyon)");
    println!();
    println!("  NOT sufficient for universal quantum computing");
    println!("    (Can only implement Clifford gates)");
    println!();

    println!("NON-ABELIAN ANYONS");
    println!("-----------------------------------------------------------------");
    println!();
    println!("  Properties:");
    println!("    • Exchange yields matrix operation (rotation in fusion space)");
    println!("    • Multiple possible fusion outcomes");
    println!("    • Fusion space is degenerate");
    println!("    • Order of braiding matters: σ₁σ₂ ≠ σ₂σ₁");
    println!();
    println!("  Examples:");
    println!("    • Ising anyons (ν=5/2 quantum Hall state)");
    println!("    • Fibonacci anyons (hypothetical, universal)");
    println!("    • SU(2)_k Chern-Simons anyons");
    println!("    • Majorana fermions (Ising anyons)");
    println!();
    println!("  CAN achieve universal quantum computing!");
    println!("    (Fibonacci anyons are computationally universal)");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Fibonacci Anyons: The Gold Standard");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Fusion Rules:");
    println!("  τ × τ = 1 + τ");
    println!();
    println!("  Meaning: Two Fibonacci anyons (τ) can fuse to:");
    println!("    • Vacuum (1) with amplitude a");
    println!("    • Another Fibonacci anyon (τ) with amplitude b");
    println!();
    println!("  |a|² + |b|² = 1  (probability conservation)");
    println!();

    println!("Fusion Tree Example (4 anyons):");
    println!();
    println!("          ╱──τ──╲");
    println!("    τ──τ─┤      ├──?");
    println!("         ╲──τ──╱");
    println!("");
    println!("          ╱──τ──╲");
    println!("    τ──τ─┤      ├──?");
    println!("         ╲──τ──╱");
    println!();
    println!("  Fusion space dimension:");
    println!("    N anyons → d_N ≈ φ^(N-2) states");
    println!("    where φ = (1+√5)/2 ≈ 1.618 (golden ratio)");
    println!();
    println!("  4 anyons: d₄ = 2 dimensions → 1 qubit!");
    println!("  6 anyons: d₆ = 5 dimensions → 2+ qubits");
    println!("  8 anyons: d₈ = 13 dimensions → 3+ qubits");
    println!();

    println!("Quantum Dimensions:");
    println!("  Each anyon type has quantum dimension d_a");
    println!();
    println!("  Ising anyons:");
    println!("    d_1 = 1  (vacuum)");
    println!("    d_σ = √2 (non-Abelian anyon)");
    println!("    d_ψ = 1  (fermion)");
    println!();
    println!("  Fibonacci anyons:");
    println!("    d_1 = 1  (vacuum)");
    println!("    d_τ = φ = (1+√5)/2  (golden ratio!)");
    println!();

    println!("Why Fibonacci Anyons are Special:");
    println!("  ✓ Computationally universal (can approximate any unitary)");
    println!("  ✓ Simplest universal non-Abelian anyon");
    println!("  ✓ Rich mathematical structure (quantum groups)");
    println!("  ✓ Dense braid group representations");
    println!("  ✗ Not yet experimentally realized");
    println!();

    println!("Physical Realizations (Proposed/Explored):");
    println!("  • Fractional quantum Hall effect (ν=12/5, ν=13/5)");
    println!("  • Non-Abelian spin liquids");
    println!("  • Topological superconductors");
    println!("  • Engineered lattice models");
    println!();

    println!("  ✓ Anyonic systems demonstrated");
    println!("  ✓ Foundation for topologically protected quantum computation");
}

/// Demonstrate braiding operations and their computational power
fn demonstrate_braiding_operations() {
    println!("BRAIDING OPERATIONS & QUANTUM GATES");
    println!("-----------------------------------------------------------------");

    println!("Braiding anyons implements quantum gates in a topologically");
    println!("protected manner. The gate is determined by the braid topology,");
    println!("not precise control of parameters.");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Braid Group Basics");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Braid Group B_n (n strands):");
    println!();
    println!("  Generators: σ₁, σ₂, ..., σ_{{n-1}}");
    println!("    σᵢ: exchange strand i and i+1 (counterclockwise)");
    println!("    σᵢ⁻¹: exchange strand i and i+1 (clockwise)");
    println!();

    println!("  Relations:");
    println!("    σᵢσⱼ = σⱼσᵢ           if |i-j| ≥ 2  (distant braids commute)");
    println!("    σᵢσᵢ₊₁σᵢ = σᵢ₊₁σᵢσᵢ₊₁ (Yang-Baxter equation)");
    println!();

    println!("Example Braids (3 strands):");
    println!();
    println!("  Identity (no braiding):");
    println!("    │ │ │");
    println!("    │ │ │");
    println!("    │ │ │");
    println!();
    println!("  σ₁ (braid strands 1 and 2):");
    println!("    │ │ │");
    println!("    │╱│ │");
    println!("    ╱ │ │");
    println!("    │ │ │");
    println!();
    println!("  σ₂ (braid strands 2 and 3):");
    println!("    │ │ │");
    println!("    │ │╱│");
    println!("    │ ╱ │");
    println!("    │ │ │");
    println!();
    println!("  σ₁σ₂ (sequential braiding):");
    println!("    │ │ │");
    println!("    │╱│ │  ← σ₁");
    println!("    ╱ │╱│  ← σ₂");
    println!("    │ ╱ │");
    println!("    │ │ │");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("From Braids to Quantum Gates");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Braid Representation:");
    println!("  Each braid element → unitary matrix");
    println!("  Braid composition → matrix multiplication");
    println!();
    println!("  ρ: B_n → U(d)");
    println!("    where d = dimension of fusion space");
    println!();

    println!("For Fibonacci Anyons (4 anyons = 1 qubit):");
    println!();
    println!("  Basis states:");
    println!("    |0⟩ ≡ ((τ × τ → 1) × (τ × τ → 1) → 1)");
    println!("    |1⟩ ≡ ((τ × τ → τ) × (τ × τ → τ) → 1)");
    println!();

    println!("  Elementary braids:");
    println!();
    println!("    σ₁ braiding (strands 1 & 2):");
    println!("      ┌                    ┐");
    println!("      │ e^(4πi/5)    0     │");
    println!("      │   0      e^(−3πi/5)│");
    println!("      └                    ┘");
    println!();
    println!("    σ₂ braiding (strands 2 & 3):");
    println!("      ┌                              ┐");
    println!("      │ −φ⁻¹   φ⁻½                  │");
    println!("      │  φ⁻½   φ⁻¹                   │");
    println!("      └                              ┘");
    println!("      (up to phase, φ = golden ratio)");
    println!();

    println!("  More complex gates via braid sequences");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Universal Gate Set via Braiding");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Fibonacci Anyons:");
    println!("  Braiding alone → dense in SU(2)");
    println!("  Meaning: Can approximate ANY single-qubit gate!");
    println!();
    println!("  Solovay-Kitaev: ε-approximation requires O(log^c(1/ε)) braids");
    println!("    where c ≈ 2-4 depending on constants");
    println!();

    println!("  Two-qubit gates:");
    println!("    Need 8 anyons for 2 qubits");
    println!("    Braiding between groups → entangling gates");
    println!("    Complete universal gate set achievable");
    println!();

    println!("Ising Anyons (Majorana modes):");
    println!("  Braiding alone → Clifford gates only");
    println!("    (NOT universal!)");
    println!();
    println!("  Need additional non-topological operations:");
    println!("    • Magic state distillation");
    println!("    • Measurement-based completion");
    println!("    → Universal computation");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Topological Protection Mechanism");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Why Braiding is Fault-Tolerant:");
    println!();
    println!("  1. NON-LOCAL ENCODING");
    println!("     Quantum information stored in:");
    println!("       • Global topological properties");
    println!("       • Fusion channel of all anyons");
    println!("     NOT in:");
    println!("       • Local degrees of freedom");
    println!("       • Positions of anyons");
    println!();
    println!("  2. ENERGY GAP PROTECTION");
    println!("     Topological degeneracy:");
    println!("       • Ground states separated by energy gap Δ");
    println!("       • Local perturbations cannot mix states");
    println!("       • Thermal fluctuations: kT << Δ");
    println!();
    println!("  3. CONTINUOUS DEFORMATIONS");
    println!("     Braid trajectory details don't matter:");
    println!("       • Only topology matters (homotopy class)");
    println!("       • Small errors in path → same gate");
    println!("       • No fine-tuning of parameters needed");
    println!();

    println!("Error Rates:");
    println!();
    println!("  Standard qubits:");
    println!("    Gate error: 10⁻³ - 10⁻⁴  (current state-of-art)");
    println!();
    println!("  Topological qubits (theoretical):");
    println!("    Gate error: 10⁻¹⁰ - 10⁻²⁰  (exponentially suppressed!)");
    println!("    Error ∝ e^(-L/ξ)");
    println!("      L: anyon separation");
    println!("      ξ: correlation length");
    println!();

    println!("Practical Challenges:");
    println!("  ✗ Creating and maintaining anyonic systems (difficult)");
    println!("  ✗ Detecting and braiding individual anyons");
    println!("  ✗ Long braiding times → slow gates");
    println!("  ✗ Still need measurement and initialization");
    println!("  ✓ When achieved, far more robust than standard qubits");
    println!();

    println!("  ✓ Braiding operations demonstrated");
    println!("  ✓ Topologically protected quantum gates");
}

/// Demonstrate topological error correction concepts
fn demonstrate_topological_error_correction() {
    println!("TOPOLOGICAL ERROR CORRECTION");
    println!("-----------------------------------------------------------------");

    println!("Topological codes protect quantum information using");
    println!("many-body entanglement with topological properties.");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Toric Code: Canonical Topological Code");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Structure:");
    println!("  • Qubits on edges of L×L lattice (torus topology)");
    println!("  • Total qubits: 2L²");
    println!("  • Logical qubits: 2 (independent of L!)");
    println!("  • Code distance: L");
    println!();

    println!("Lattice (4×4 example):");
    println!();
    println!("    v───q───v───q───v");
    println!("    │   X   │   X   │");
    println!("    q   p   q   p   q     v: vertex");
    println!("    │   X   │   X   │     q: qubit (edge)");
    println!("    v───q───v───q───v     p: plaquette");
    println!("    │   X   │   X   │     X: stabilizer");
    println!("    q   p   q   p   q");
    println!("    │   X   │   X   │");
    println!("    v───q───v───q───v");
    println!();

    println!("Stabilizer Generators:");
    println!();
    println!("  Vertex operators A_v:");
    println!("    A_v = X₁ ⊗ X₂ ⊗ X₃ ⊗ X₄");
    println!("    (X on 4 qubits around vertex v)");
    println!();
    println!("  Plaquette operators B_p:");
    println!("    B_p = Z₁ ⊗ Z₂ ⊗ Z₃ ⊗ Z₄");
    println!("    (Z on 4 qubits around plaquette p)");
    println!();
    println!("  Ground state: |ψ⟩ such that");
    println!("    A_v|ψ⟩ = |ψ⟩  ∀ vertices v");
    println!("    B_p|ψ⟩ = |ψ⟩  ∀ plaquettes p");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Error Detection & Correction");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Error Syndromes:");
    println!();
    println!("  Bit flip error (X error):");
    println!("    • Violates plaquette operators B_p");
    println!("    • Creates pair of -1 eigenvalues");
    println!("    • Viewed as e⁻-e⁺ pair endpoints");
    println!();
    println!("  Phase flip error (Z error):");
    println!("    • Violates vertex operators A_v");
    println!("    • Creates pair of -1 eigenvalues");
    println!("    • Viewed as m-m̄ pair endpoints (magnetic charges)");
    println!();

    println!("Example: Single X Error");
    println!();
    println!("    v───q───v───q───v");
    println!("    │       │   ✗   │   ← X error here");
    println!("    q   p₁  q   p₂  q");
    println!("    │   −   │   −   │   ← B_p₁ = B_p₂ = −1");
    println!("    v───q───v───q───v");
    println!();
    println!("  Syndrome: Two adjacent plaquettes with -1");
    println!();

    println!("Minimum Weight Perfect Matching (MWPM):");
    println!();
    println!("  1. Measure all stabilizers → syndrome");
    println!("  2. Identify -1 eigenvalue locations (defects)");
    println!("  3. Pair up defects minimizing total distance");
    println!("  4. Apply corrections along shortest paths");
    println!();

    println!("Error Correction Threshold:");
    println!();
    println!("  If physical error rate p < p_th:");
    println!("    → Logical error rate decreases with L");
    println!("    → P_L ∝ (p/p_th)^((L+1)/2)");
    println!();
    println!("  Toric code threshold:");
    println!("    p_th ≈ 11% (independent depolarizing noise)");
    println!("    p_th ≈ 3% (circuit-level noise model)");
    println!();

    println!("Scalability:");
    println!();
    println!("  For logical error rate P_L:");
    println!("    L ≈ log(1/P_L) / log(p_th/p)");
    println!();
    println!("  Example: p = 0.1%, P_L = 10⁻¹⁵");
    println!("    → L ≈ 15-20 (moderate size)");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Logical Operations on Toric Code");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Logical operators:");
    println!();
    println!("  Homological structure:");
    println!("    Torus has 2 independent non-contractible loops");
    println!("    → 2 logical qubits");
    println!();
    println!("  Logical X̄:");
    println!("    Chain of X operators around torus horizontally");
    println!();
    println!("  Logical Z̄:");
    println!("    Chain of Z operators around torus vertically");
    println!();
    println!("  [X̄, Z̄] ≠ 0 → proper logical qubit");
    println!();

    println!("Gate Implementation:");
    println!();
    println!("  Transversal gates:");
    println!("    • Logical H: Apply H to all physical qubits");
    println!("    • Logical CNOT: Lattice surgery");
    println!();
    println!("  Non-transversal gates:");
    println!("    • T gate: Magic state distillation");
    println!("    • Requires 3D color codes or lattice surgery");
    println!();

    println!("  ✓ Topological error correction demonstrated");
    println!("  ✓ Toric code provides robust quantum memory");
}

/// Demonstrate surface codes - the most practical topological code
fn demonstrate_surface_codes() {
    println!("SURFACE CODES: PRACTICAL TOPOLOGICAL QEC");
    println!("-----------------------------------------------------------------");

    println!("Surface codes are planar versions of toric codes.");
    println!("Currently the leading candidate for scalable quantum computing.");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Surface Code Architecture");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Distance-3 Surface Code (13 data qubits, 12 syndrome qubits):");
    println!();
    println!("      X   Z   X   Z   X");
    println!("    ┌───┬───┬───┬───┬───┐");
    println!("  Z │ D │ S │ D │ S │ D │");
    println!("    ├───┼───┼───┼───┼───┤");
    println!("  X │ S │ D │ S │ D │ S │ X");
    println!("    ├───┼───┼───┼───┼───┤");
    println!("  Z │ D │ S │ D │ S │ D │");
    println!("    ├───┼───┼───┼───┼───┤");
    println!("  X │ S │ D │ S │ D │ S │ X");
    println!("    ├───┼───┼───┼───┼───┤");
    println!("  Z │ D │ S │ D │ S │ D │");
    println!("    └───┴───┴───┴───┴───┘");
    println!("      X   Z   X   Z   X");
    println!();
    println!("  D: Data qubit");
    println!("  S: Syndrome (ancilla) qubit");
    println!("  X/Z on boundary: Logical operators");
    println!();

    println!("Properties:");
    println!("  • Code distance d = L (linear in size)");
    println!("  • Physical qubits: ≈ d²");
    println!("  • Logical qubits: 1 (per patch)");
    println!("  • Overhead: Very high, but best known");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Syndrome Measurement Circuit");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("X-type stabilizer measurement:");
    println!();
    println!("  |anc⟩ ──H──●────●────●────●──H──M → syndrome");
    println!("            │    │    │    │");
    println!("  |d₁⟩  ────┼────X────┼────┼────");
    println!("            │         │    │");
    println!("  |d₂⟩  ────X─────────┼────┼────");
    println!("                      │    │");
    println!("  |d₃⟩  ──────────────X────┼────");
    println!("");
    println!("  |d₄⟩  ───────────────────X────");
    println!();
    println!("  Measures: A = X₁X₂X₃X₄");
    println!();

    println!("Z-type stabilizer measurement:");
    println!();
    println!("  |anc⟩ ─────●────●────●────●──M → syndrome");
    println!("             │    │    │    │");
    println!("  |d₁⟩  ─────Z────┼────┼────┼────");
    println!("                  │    │    │");
    println!("  |d₂⟩  ──────────Z────┼────┼────");
    println!("                       │    │");
    println!("  |d₃⟩  ─────────────────Z────┼────");
    println!("");
    println!("  |d₄⟩  ──────────────────────Z────");
    println!();
    println!("  Measures: B = Z₁Z₂Z₃Z₄");
    println!();

    println!("Syndrome Extraction Cycle:");
    println!("  1. Initialize all syndrome qubits to |0⟩");
    println!("  2. Apply X-stabilizer measurement circuits");
    println!("  3. Apply Z-stabilizer measurement circuits");
    println!("  4. Measure all syndrome qubits");
    println!("  5. Decode and correct");
    println!("  6. Repeat (~1000s of cycles for computation)");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Lattice Surgery: Quantum Gates via Code Deformation");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Multi-Patch Architecture:");
    println!();
    println!("   ┌──────┐     ┌──────┐");
    println!("   │      │     │      │");
    println!("   │  L₁  │     │  L₂  │  ← Logical qubits");
    println!("   │      │     │      │");
    println!("   └──────┘     └──────┘");
    println!();
    println!("  Each patch = 1 logical qubit");
    println!();

    println!("Logical CNOT via Lattice Surgery:");
    println!();
    println!("  Step 1: Merge patches");
    println!("   ┌──────┬──────┐");
    println!("   │  L₁  │  L₂  │");
    println!("   └──────┴──────┘");
    println!();
    println!("  Step 2: Perform joint measurement");
    println!("    Measure stabilizers across boundary");
    println!();
    println!("  Step 3: Split patches");
    println!("   ┌──────┐ ┌──────┐");
    println!("   │  L₁' │ │  L₂' │ ← Entangled state");
    println!("   └──────┘ └──────┘");
    println!();
    println!("  Time: O(d) stabilizer cycles");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Resource Requirements for Useful Computation");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Example: Factor 2048-bit RSA (Shor's algorithm)");
    println!();
    println!("  Logical qubits needed: ~2×2048 ≈ 4000");
    println!("  Logical gate count: ~10¹¹ gates");
    println!("  Target logical error: 10⁻¹⁵");
    println!();
    println!("  If physical error rate p = 0.1%:");
    println!("    Code distance needed: d ≈ 20");
    println!("    Physical qubits per logical: d² ≈ 400");
    println!("    Total physical qubits: 4000 × 400 = 1.6M");
    println!();
    println!("  Cycle time: 1 μs (optimistic)");
    println!("  Runtime: ~1 day");
    println!();

    println!("Current Experimental Status:");
    println!();
    println!("  ✓ Distance-3, 5 codes demonstrated");
    println!("  ✓ Below-threshold error rates achieved");
    println!("  ✓ Logical lifetime > physical lifetime");
    println!("  ~ Distance-7+ codes in progress");
    println!("  ✗ Full-scale systems: still years away");
    println!();

    println!("Major Players:");
    println!("  • Google: Superconducting qubits, surface codes");
    println!("  • IBM: Heavy-hexagon lattice, surface code variant");
    println!("  • Microsoft: Topological qubits (Majorana)");
    println!("  • IonQ, Honeywell: Trapped ions, LDPC codes");
    println!("  • Amazon, Rigetti: Various approaches");
    println!();

    println!("  ✓ Surface codes demonstrated");
    println!("  ✓ Most practical path to fault-tolerant quantum computing");
}

/// Demonstrate Majorana fermions and topological qubits
fn demonstrate_majorana_fermions() {
    println!("MAJORANA FERMIONS & TOPOLOGICAL QUBITS");
    println!("-----------------------------------------------------------------");

    println!("Majorana fermions are particles that are their own antiparticles.");
    println!("In condensed matter, they emerge as zero-energy excitations.");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Majorana Zero Modes (MZMs)");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Fermion Decomposition:");
    println!();
    println!("  Regular fermion (complex):");
    println!("    c = (γ₁ + iγ₂) / 2");
    println!("    c† = (γ₁ − iγ₂) / 2");
    println!();
    println!("  Majorana fermions (real):");
    println!("    γ₁ = c + c†");
    println!("    γ₂ = −i(c − c†)");
    println!();
    println!("  Properties:");
    println!("    γ† = γ  (self-adjoint)");
    println!("    {{γᵢ, γⱼ}} = 2δᵢⱼ  (anticommutation)");
    println!();

    println!("Physical Realization:");
    println!();
    println!("  Topological superconductor + spin-orbit coupling:");
    println!();
    println!("   Normal    Topological");
    println!("   ───────────────────");
    println!("               ╱╲        γ₁: Left end MZM");
    println!("              ╱  ╲");
    println!("   ──────────╱────╲────  γ₂: Right end MZM");
    println!("            │      │");
    println!("         Energy gap Δ");
    println!();
    println!("  • MZMs localized at ends (exponential decay)");
    println!("  • Zero energy (protected by gap)");
    println!("  • Non-local: γ₁ and γ₂ form one fermion");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Topological Qubit from Majoranas");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Four Majorana Modes → One Qubit:");
    println!();
    println!("   Wire 1:  γ₁────────────γ₂");
    println!("              ");
    println!("   Wire 2:  γ₃────────────γ₄");
    println!();

    println!("  Form two complex fermions:");
    println!("    c₁ = (γ₁ + iγ₂) / 2");
    println!("    c₂ = (γ₃ + iγ₄) / 2");
    println!();
    println!("  Occupation states:");
    println!("    |00⟩: Both c₁, c₂ empty → qubit |0⟩");
    println!("    |11⟩: Both c₁, c₂ occupied → qubit |1⟩");
    println!();
    println!("  Parity conserved:");
    println!("    P = (−i)γ₁γ₂ = 2c₁†c₁ − 1");
    println!("    |0⟩: P = −1,  |1⟩: P = +1");
    println!();

    println!("Topological Protection:");
    println!();
    println!("  Information encoded in:");
    println!("    • Fermion parity (global property)");
    println!("    • NOT in local degrees of freedom");
    println!();
    println!("  Errors require:");
    println!("    • Exciting quasiparticles across gap Δ");
    println!("    • Or tunneling between distant MZMs");
    println!();
    println!("  Error rate:");
    println!("    Γ ∝ e^(−L/ξ) × e^(−Δ/kT)");
    println!("      L: MZM separation");
    println!("      ξ: coherence length");
    println!("      Δ: energy gap");
    println!("      T: temperature");
    println!();
    println!("  Exponential suppression!");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Braiding Majoranas for Quantum Gates");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Gate via Adiabatic Exchange:");
    println!();
    println!("  Initial:   γ₁ ─── γ₂       γ₃ ─── γ₄");
    println!("                     ╲       ╱");
    println!("                      ╲     ╱");
    println!("  Braided:   γ₁ ───   ╳    ─── γ₄");
    println!("                     ╱  ╲  ");
    println!("                    ╱    ╲");
    println!("             γ₁ ─── γ₃      γ₂ ─── γ₄");
    println!();

    println!("  Effect: Unitary rotation");
    println!("    |ψ⟩ → e^(±iπ/8)|ψ⟩  (depends on braid direction)");
    println!();

    println!("Clifford Gates from Braiding:");
    println!();
    println!("  X gate: σ_x = γ₁γ₂");
    println!("  Y gate: σ_y = γ₁γ₃  (non-local!)");
    println!("  Z gate: σ_z = iγ₁γ₂γ₃γ₄");
    println!("  H gate: Combination of braids");
    println!("  CNOT: Multi-MZM braiding");
    println!();

    println!("Limitation:");
    println!("  • Ising anyons (MZMs) → Clifford group only");
    println!("  • NOT computationally universal");
    println!();
    println!("  Need T gate for universality:");
    println!("    Option 1: Magic state distillation");
    println!("    Option 2: Measurement-based completion");
    println!("    Option 3: Non-topological T gates (trade-off)");
    println!();

    println!("═══════════════════════════════════════════════════════════════");
    println!("Experimental Status & Challenges");
    println!("═══════════════════════════════════════════════════════════════");
    println!();

    println!("Candidate Platforms:");
    println!();
    println!("  1. Semiconductor-Superconductor Nanowires");
    println!("     InAs or InSb wire + Al shell");
    println!("     Evidence: Zero-bias conductance peaks");
    println!("     Status: Controversial, non-conclusive");
    println!();
    println!("  2. Topological Insulator-Superconductor");
    println!("     Bi₂Te₃ + superconductor");
    println!("     Advantage: 2D system, easier fabrication");
    println!("     Status: Early stages");
    println!();
    println!("  3. Iron-Based Superconductors");
    println!("     Fe(Te,Se)");
    println!("     Vortex cores host MZMs");
    println!("     Status: Promising signatures observed");
    println!();

    println!("Verification Challenges:");
    println!();
    println!("  Smoking gun signatures:");
    println!("    ✓ Zero-bias conductance peak");
    println!("    ✓ Peak height = 2e²/h (quantized)");
    println!("    ✓ Robust to perturbations");
    println!("    ~ Exponential length dependence");
    println!("    ✗ Braiding signatures (not yet observed)");
    println!();
    println!("  Alternative explanations:");
    println!("    • Andreev bound states");
    println!("    • Disorder-induced states");
    println!("    • Kondo effect");
    println!();
    println!("  Need: Unambiguous braiding demonstration");
    println!();

    println!("Microsoft's Approach:");
    println!("  • Heavy investment in Majorana platform");
    println!("  • Scalable topological qubit architecture");
    println!("  • Combined with conventional error correction");
    println!("  • Timeline: Still in development phase");
    println!();

    println!("Advantages if Realized:");
    println!("  ✓ Intrinsically fault-tolerant gates");
    println!("  ✓ Dramatically reduced overhead");
    println!("  ✓ Simpler control electronics");
    println!("  ✓ Potentially higher temperatures");
    println!();

    println!("Challenges:");
    println!("  ✗ Experimental verification difficult");
    println!("  ✗ Still need magic states for universality");
    println!("  ✗ Scalability to many qubits unclear");
    println!("  ✗ Longer development timeline");
    println!();

    println!("  ✓ Majorana fermions & topological qubits demonstrated");
    println!("  ✓ Promising long-term approach to quantum computing");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_example_runs() {
        assert!(main().is_ok());
    }

    #[test]
    fn test_anyonic_systems() {
        assert!(demonstrate_anyonic_systems().is_ok());
    }

    #[test]
    fn test_braiding_operations() {
        assert!(demonstrate_braiding_operations().is_ok());
    }

    #[test]
    fn test_topological_error_correction() {
        assert!(demonstrate_topological_error_correction().is_ok());
    }

    #[test]
    fn test_surface_codes() {
        assert!(demonstrate_surface_codes().is_ok());
    }

    #[test]
    fn test_majorana_fermions() {
        assert!(demonstrate_majorana_fermions().is_ok());
    }
}