forked from nzy1997/ILPQEC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface_code_example.py
More file actions
140 lines (102 loc) · 3.68 KB
/
surface_code_example.py
File metadata and controls
140 lines (102 loc) · 3.68 KB
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
"""
Surface code decoding example using Stim integration.
Requirements:
pip install stim ilpdecoder
Plus a solver (SCIP, HiGHS, etc.)
"""
import numpy as np
try:
import stim
STIM_AVAILABLE = True
except ImportError:
STIM_AVAILABLE = False
from ilpdecoder import Decoder, get_available_solvers
def surface_code_example():
"""Decode a rotated surface code using ILP."""
print("=" * 60)
print("Surface Code Decoding with ILPDecoder")
print("=" * 60)
if not STIM_AVAILABLE:
print("Stim is not installed. Install with: pip install stim")
return
if not get_available_solvers():
print("No solver available. Install SCIP, HiGHS, CBC, or GLPK.")
return
distance = 3
rounds = 3
noise = 0.01
print(f"\nGenerating surface code circuit:")
print(f" Distance: {distance}")
print(f" Rounds: {rounds}")
print(f" Noise: {noise}")
circuit = stim.Circuit.generated(
"surface_code:rotated_memory_x",
distance=distance,
rounds=rounds,
after_clifford_depolarization=noise
)
dem = circuit.detector_error_model(decompose_errors=True)
print(f"\nDetector error model:")
print(f" Detectors: {dem.num_detectors}")
print(f" Observables: {dem.num_observables}")
print(f" Error mechanisms: {dem.num_errors}")
decoder = Decoder.from_stim_dem(dem)
print(f"\nDecoder: {decoder}")
num_shots = 100
print(f"\nSampling {num_shots} shots...")
sampler = circuit.compile_detector_sampler()
detection_events, actual_observables = sampler.sample(
shots=num_shots,
separate_observables=True
)
print("Decoding...")
num_correct = 0
num_detected = 0
for i in range(num_shots):
if np.any(detection_events[i]):
num_detected += 1
_, predicted = decoder.decode(detection_events[i])
if np.array_equal(predicted, actual_observables[i]):
num_correct += 1
print(f"\nResults:")
print(f" Shots with detections: {num_detected}/{num_shots}")
print(f" Correct predictions: {num_correct}/{num_shots}")
print(f" Logical error rate: {(num_shots - num_correct) / num_shots:.2%}")
def compare_solvers():
"""Compare solve times between different solvers."""
print("\n" + "=" * 60)
print("Solver Comparison")
print("=" * 60)
if not STIM_AVAILABLE or not get_available_solvers():
print("Stim or solver not available. Skipping.")
return
import time
circuit = stim.Circuit.generated(
"surface_code:rotated_memory_x",
distance=3,
rounds=3,
after_clifford_depolarization=0.01
)
dem = circuit.detector_error_model(decompose_errors=True)
sampler = circuit.compile_detector_sampler()
detection_events, _ = sampler.sample(shots=20, separate_observables=True)
available = get_available_solvers()
print(f"\nAvailable solvers: {available}")
print(f"Testing with {len(detection_events)} shots...")
for solver in available:
decoder = Decoder.from_stim_dem(dem, solver=solver)
start = time.time()
for i in range(len(detection_events)):
decoder.decode(detection_events[i])
elapsed = time.time() - start
avg_time = elapsed / len(detection_events)
print(f" {solver.upper():8s}: {avg_time*1000:6.1f} ms/shot")
def main():
print("ILPDecoder Surface Code Example")
print("=" * 60)
surface_code_example()
compare_solvers()
print("\n" + "=" * 60)
print("Example completed!")
if __name__ == "__main__":
main()