forked from aksirot/stim_work
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface_code_sim.py
More file actions
357 lines (297 loc) · 11.8 KB
/
Copy pathsurface_code_sim.py
File metadata and controls
357 lines (297 loc) · 11.8 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
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
"""
Surface code simulation using Stim + PyMatching.
Supports:
- Rotated and unrotated surface codes (distance d)
- Isotropic Pauli (depolarizing) noise + independent measurement noise
- PyMatching MWPM decoder
- Logical idle experiments: logical error rate vs rounds / error rate
"""
from __future__ import annotations
import numpy as np
import stim
import pymatching
from dataclasses import dataclass
from enum import Enum
from typing import Dict, List, Optional
# ---------------------------------------------------------------------------
# Code type
# ---------------------------------------------------------------------------
class CodeType(str, Enum):
ROTATED_Z = "surface_code:rotated_memory_z"
ROTATED_X = "surface_code:rotated_memory_x"
UNROTATED_Z = "surface_code:unrotated_memory_z"
UNROTATED_X = "surface_code:unrotated_memory_x"
# ---------------------------------------------------------------------------
# Noise model
# ---------------------------------------------------------------------------
@dataclass
class ErrorModel:
"""
Isotropic Pauli noise + measurement noise.
p_phys : Depolarizing rate applied after every Clifford gate.
Single-qubit gates get 1Q depolarizing; 2-qubit gates get 2Q
depolarizing — both controlled by this single parameter
(the "isotropic" / symmetric-Pauli assumption).
p_meas : Bit-flip probability applied before every measurement and
after every reset (measurement noise).
"""
p_phys: float
p_meas: float
def __post_init__(self) -> None:
for name, val in [("p_phys", self.p_phys), ("p_meas", self.p_meas)]:
if not (0.0 <= val <= 1.0):
raise ValueError(f"{name} must be in [0, 1], got {val}")
# ------------------------------------------------------------------
# Convenience constructors
# ------------------------------------------------------------------
@classmethod
def symmetric(cls, p: float) -> "ErrorModel":
"""Equal gate and measurement error rates."""
return cls(p_phys=p, p_meas=p)
@classmethod
def gate_dominated(cls, p_phys: float, meas_ratio: float = 0.1) -> "ErrorModel":
"""Measurement error is *meas_ratio* times the gate error rate."""
return cls(p_phys=p_phys, p_meas=p_phys * meas_ratio)
@classmethod
def meas_dominated(cls, p_meas: float, gate_ratio: float = 0.1) -> "ErrorModel":
"""Gate error is *gate_ratio* times the measurement error rate."""
return cls(p_phys=p_meas * gate_ratio, p_meas=p_meas)
def __repr__(self) -> str:
return f"ErrorModel(p_phys={self.p_phys:.3g}, p_meas={self.p_meas:.3g})"
# ---------------------------------------------------------------------------
# Decoders
# ---------------------------------------------------------------------------
class Decoder:
"""Abstract decoder interface."""
def setup(self, circuit: stim.Circuit) -> None:
"""Pre-compute matching graph from the circuit's detector error model."""
raise NotImplementedError
def decode_batch(self, detection_events: np.ndarray) -> np.ndarray:
"""
Decode a batch of detection events.
Parameters
----------
detection_events : bool array of shape (shots, num_detectors)
Returns
-------
predictions : bool array of shape (shots, num_observables)
"""
raise NotImplementedError
class PyMatchingDecoder(Decoder):
"""
Minimum-weight perfect matching via PyMatching.
Uses the detector error model derived from the Stim circuit to build the
matching graph, so no hand-crafted weights are needed.
"""
def __init__(self) -> None:
self._matching: Optional[pymatching.Matching] = None
def setup(self, circuit: stim.Circuit) -> None:
dem = circuit.detector_error_model(decompose_errors=True)
self._matching = pymatching.Matching.from_detector_error_model(dem)
def decode_batch(self, detection_events: np.ndarray) -> np.ndarray:
if self._matching is None:
raise RuntimeError("Call setup(circuit) before decode_batch.")
return self._matching.decode_batch(detection_events)
# ---------------------------------------------------------------------------
# Result container
# ---------------------------------------------------------------------------
@dataclass
class SimulationResult:
"""Statistics from one logical idle experiment."""
distance: int
rounds: int
error_model: ErrorModel
shots: int
num_logical_errors: int
logical_error_rate: float
logical_error_rate_se: float # binomial standard error
def __repr__(self) -> str:
return (
f"SimulationResult("
f"d={self.distance}, rounds={self.rounds}, "
f"{self.error_model!r}, "
f"LER={self.logical_error_rate:.4g} ± {self.logical_error_rate_se:.2g})"
)
# ---------------------------------------------------------------------------
# Core simulator
# ---------------------------------------------------------------------------
class SurfaceCodeSimulator:
"""
Build Stim circuits, inject noise, and decode to estimate logical error rates
for a surface code logical idle experiment.
Parameters
----------
distance : Code distance d (number of physical qubits per row/column).
code_type : Which surface code variant to use (default: rotated Z-memory).
"""
def __init__(
self,
distance: int,
code_type: CodeType = CodeType.ROTATED_Z,
) -> None:
if distance < 2:
raise ValueError("distance must be >= 2")
self.distance = distance
self.code_type = code_type
# ------------------------------------------------------------------
# Circuit builder
# ------------------------------------------------------------------
def build_circuit(
self,
error_model: ErrorModel,
rounds: int,
) -> stim.Circuit:
"""
Return a noisy Stim circuit for the logical idle experiment.
Noise layers
------------
after_clifford_depolarization → isotropic Pauli noise on gates
before_measure_flip_probability → measurement bit-flip noise
after_reset_flip_probability → reset / state-prep noise
"""
return stim.Circuit.generated(
self.code_type.value,
distance=self.distance,
rounds=rounds,
after_clifford_depolarization=error_model.p_phys,
before_measure_flip_probability=error_model.p_meas,
after_reset_flip_probability=error_model.p_meas,
)
# ------------------------------------------------------------------
# Single run
# ------------------------------------------------------------------
def run(
self,
error_model: ErrorModel,
rounds: int,
shots: int,
decoder: Optional[Decoder] = None,
seed: Optional[int] = None,
) -> SimulationResult:
"""
Run a logical idle experiment and return logical error statistics.
Parameters
----------
error_model : Noise model.
rounds : Syndrome extraction rounds (≥ 1).
shots : Number of Monte Carlo samples.
decoder : Decoder instance (default: PyMatchingDecoder).
seed : RNG seed for reproducibility.
Returns
-------
SimulationResult
"""
circuit = self.build_circuit(error_model, rounds)
if decoder is None:
decoder = PyMatchingDecoder()
decoder.setup(circuit)
sampler = circuit.compile_detector_sampler(seed=seed)
detection_events, observable_flips = sampler.sample(
shots, separate_observables=True
)
predictions = decoder.decode_batch(detection_events)
# A logical error occurs when ANY observable is mispredicted
logical_errors_per_shot = np.any(predictions != observable_flips, axis=1)
n_err = int(np.sum(logical_errors_per_shot))
ler = n_err / shots
ler_se = float(np.sqrt(ler * (1.0 - ler) / shots))
return SimulationResult(
distance=self.distance,
rounds=rounds,
error_model=error_model,
shots=shots,
num_logical_errors=n_err,
logical_error_rate=ler,
logical_error_rate_se=ler_se,
)
# ------------------------------------------------------------------
# Convenience sweeps
# ------------------------------------------------------------------
def sweep_p(
self,
p_values: List[float],
rounds: Optional[int] = None,
shots: int = 10_000,
p_meas_factor: float = 1.0,
decoder: Optional[Decoder] = None,
seed: Optional[int] = None,
) -> List[SimulationResult]:
"""
Sweep physical error rates. p_meas = p_phys * p_meas_factor.
Parameters
----------
p_values : Physical error rates to iterate over.
rounds : Syndrome rounds (default: distance).
shots : Shots per data point.
p_meas_factor: Ratio of measurement to gate error rate.
decoder : Decoder (default: PyMatchingDecoder).
seed : RNG seed.
"""
if rounds is None:
rounds = self.distance
results = []
for p in p_values:
em = ErrorModel(p_phys=p, p_meas=p * p_meas_factor)
results.append(
self.run(em, rounds=rounds, shots=shots, decoder=decoder, seed=seed)
)
return results
def sweep_rounds(
self,
round_values: List[int],
error_model: ErrorModel,
shots: int = 10_000,
decoder: Optional[Decoder] = None,
seed: Optional[int] = None,
) -> List[SimulationResult]:
"""
Sweep syndrome extraction rounds at a fixed error model.
"""
results = []
for r in round_values:
results.append(
self.run(error_model, rounds=r, shots=shots, decoder=decoder, seed=seed)
)
return results
# ---------------------------------------------------------------------------
# Multi-distance threshold sweep
# ---------------------------------------------------------------------------
def threshold_sweep(
distances: List[int],
p_values: List[float],
rounds_per_distance: Optional[Dict[int, int]] = None,
shots: int = 10_000,
p_meas_factor: float = 1.0,
code_type: CodeType = CodeType.ROTATED_Z,
decoder_cls=PyMatchingDecoder,
seed: Optional[int] = None,
) -> Dict[int, List[SimulationResult]]:
"""
Sweep distances × error rates to locate the error threshold.
Parameters
----------
distances : List of code distances.
p_values : Physical error rates to sweep.
rounds_per_distance : Optional dict mapping d → rounds (default: d).
shots : Shots per data point.
p_meas_factor : p_meas = p_phys * p_meas_factor.
code_type : Surface code variant.
decoder_cls : Decoder class (must implement Decoder interface).
seed : RNG seed.
Returns
-------
Dict mapping distance → List[SimulationResult]
"""
all_results: Dict[int, List[SimulationResult]] = {}
for d in distances:
sim = SurfaceCodeSimulator(distance=d, code_type=code_type)
rounds = (rounds_per_distance or {}).get(d, d)
all_results[d] = sim.sweep_p(
p_values,
rounds=rounds,
shots=shots,
p_meas_factor=p_meas_factor,
decoder=decoder_cls(),
seed=seed,
)
return all_results