forked from ionq-publications/BeamSearchDecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_functions.py
More file actions
381 lines (349 loc) · 18 KB
/
simulation_functions.py
File metadata and controls
381 lines (349 loc) · 18 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# This file is part of BeamSearchDecoder.
# Copyright (c) 2025 IonQ, Inc., all rights reserved
# Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike
# 4.0 International License (CC BY-NC-SA 4.0).
# You may obtain a copy of the License at:
# https://creativecommons.org/licenses/by-nc-sa/4.0/
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import json
import sinter
import stim
import multiprocessing
import time
from beamsearch import BeamSearch
from sinter_beamsearch import SinterDecoder_BeamSearch
from stimbposd import BPOSD, SinterDecoder_BPOSD
decoder_dictionary = {
"beam8_230iters": SinterDecoder_BeamSearch(),
"beam32_340iters": SinterDecoder_BeamSearch(beam_width=32, initial_iters=40, iters_per_round=30),
"beam64_640iters": SinterDecoder_BeamSearch(max_rounds=20, beam_width=64, initial_iters=40, iters_per_round=30),
"beam64_32res_640iters": SinterDecoder_BeamSearch(
max_rounds=20, beam_width=64, num_results=32, initial_iters=40, iters_per_round=30
),
"bp30+osd": SinterDecoder_BPOSD(max_bp_iters=30, bp_method="min_sum", osd_order=10, osd_method="osd_cs")
}
distance_dictionary = {
(144, 12): 12,
(90, 8): 10,
(450, 32): 8
}
StimCircuit_dictionary = {
(144, 12, 0.001, "x"): "BB[[144,12,12]],memory_X,error_rate=0.001,syndrome_rounds=12.stim",
(144, 12, 0.001, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.001,syndrome_rounds=12.stim",
(144, 12, 0.002, "x"): "BB[[144,12,12]],memory_X,error_rate=0.002,syndrome_rounds=12.stim",
(144, 12, 0.002, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.002,syndrome_rounds=12.stim",
(144, 12, 0.003, "x"): "BB[[144,12,12]],memory_X,error_rate=0.003,syndrome_rounds=12.stim",
(144, 12, 0.003, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.003,syndrome_rounds=12.stim",
(144, 12, 0.004, "x"): "BB[[144,12,12]],memory_X,error_rate=0.004,syndrome_rounds=12.stim",
(144, 12, 0.004, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.004,syndrome_rounds=12.stim",
(144, 12, 0.005, "x"): "BB[[144,12,12]],memory_X,error_rate=0.005,syndrome_rounds=12.stim",
(144, 12, 0.005, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.005,syndrome_rounds=12.stim",
(144, 12, 0.006, "x"): "BB[[144,12,12]],memory_X,error_rate=0.006,syndrome_rounds=12.stim",
(144, 12, 0.006, "z"): "BB[[144,12,12]],memory_Z,error_rate=0.006,syndrome_rounds=12.stim",
(90, 8, 0.002, "x"): "BB[[90,8,10]],memory_X,error_rate=0.002,syndrome_rounds=10.stim",
(90, 8, 0.002, "z"): "BB[[90,8,10]],memory_Z,error_rate=0.002,syndrome_rounds=10.stim",
(90, 8, 0.003, "x"): "BB[[90,8,10]],memory_X,error_rate=0.003,syndrome_rounds=10.stim",
(90, 8, 0.003, "z"): "BB[[90,8,10]],memory_Z,error_rate=0.003,syndrome_rounds=10.stim",
(90, 8, 0.004, "x"): "BB[[90,8,10]],memory_X,error_rate=0.004,syndrome_rounds=10.stim",
(90, 8, 0.004, "z"): "BB[[90,8,10]],memory_Z,error_rate=0.004,syndrome_rounds=10.stim",
(90, 8, 0.005, "x"): "BB[[90,8,10]],memory_X,error_rate=0.005,syndrome_rounds=10.stim",
(90, 8, 0.005, "z"): "BB[[90,8,10]],memory_Z,error_rate=0.005,syndrome_rounds=10.stim",
(90, 8, 0.002, "xyz_x"): "BB[[90,8,10]],memory_X,error_rate=0.002,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.002, "xyz_z"): "BB[[90,8,10]],memory_Z,error_rate=0.002,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.003, "xyz_x"): "BB[[90,8,10]],memory_X,error_rate=0.003,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.003, "xyz_z"): "BB[[90,8,10]],memory_Z,error_rate=0.003,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.004, "xyz_x"): "BB[[90,8,10]],memory_X,error_rate=0.004,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.004, "xyz_z"): "BB[[90,8,10]],memory_Z,error_rate=0.004,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.005, "xyz_x"): "BB[[90,8,10]],memory_X,error_rate=0.005,syndrome_rounds=10,XYZ_circuit.stim",
(90, 8, 0.005, "xyz_z"): "BB[[90,8,10]],memory_Z,error_rate=0.005,syndrome_rounds=10,XYZ_circuit.stim",
(450, 32, 0.002, "x"): "HGP[[450,32,8]],memory_X,error_rate=0.002,syndrome_rounds=8.stim",
(450, 32, 0.002, "z"): "HGP[[450,32,8]],memory_Z,error_rate=0.002,syndrome_rounds=8.stim",
(450, 32, 0.003, "x"): "HGP[[450,32,8]],memory_X,error_rate=0.003,syndrome_rounds=8.stim",
(450, 32, 0.003, "z"): "HGP[[450,32,8]],memory_Z,error_rate=0.003,syndrome_rounds=8.stim",
(450, 32, 0.004, "x"): "HGP[[450,32,8]],memory_X,error_rate=0.004,syndrome_rounds=8.stim",
(450, 32, 0.004, "z"): "HGP[[450,32,8]],memory_Z,error_rate=0.004,syndrome_rounds=8.stim",
(450, 32, 0.005, "x"): "HGP[[450,32,8]],memory_X,error_rate=0.005,syndrome_rounds=8.stim",
(450, 32, 0.005, "z"): "HGP[[450,32,8]],memory_Z,error_rate=0.005,syndrome_rounds=8.stim"
}
def one_point_simulation(
n: int,
k: int,
p_CNOT: float,
circuit_type: str,
decoder: str,
maximum_shots: int = 100_000,
maximum_errors: int = 100,
):
if circuit_type == "normal":
if (n, k, p_CNOT, "x") in StimCircuit_dictionary:
x_circuit = stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(n, k, p_CNOT, "x")])
else:
raise ValueError(f"({n}, {k}, {p_CNOT}, {circuit_type}) circuit not available")
if (n, k, p_CNOT, "z") in StimCircuit_dictionary:
z_circuit = stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(n, k, p_CNOT, "z")])
else:
raise ValueError(f"({n}, {k}, {p_CNOT}, {circuit_type}) circuit not available")
elif circuit_type == "XYZ":
if (n, k, p_CNOT, "xyz_x") in StimCircuit_dictionary:
x_circuit = stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(n, k, p_CNOT, "xyz_x")])
else:
raise ValueError(f"({n}, {k}, {p_CNOT}, {circuit_type}) circuit not available")
if (n, k, p_CNOT, "xyz_z") in StimCircuit_dictionary:
z_circuit = stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(n, k, p_CNOT, "xyz_z")])
else:
raise ValueError(f"({n}, {k}, {p_CNOT}, {circuit_type}) circuit not available")
else:
raise ValueError("circuit_type should be either normal or XYZ")
x_samples = sinter.collect(
num_workers=multiprocessing.cpu_count() - 1,
max_shots=maximum_shots,
max_errors=maximum_errors,
tasks=[sinter.Task(circuit=x_circuit, json_metadata={"p_CNOT": p_CNOT, "x_or_z": "x"})],
decoders=[decoder],
custom_decoders=decoder_dictionary,
print_progress=True,
)
x_num_errors = x_samples[0].errors
x_num_shots = x_samples[0].shots
x_logical_error_rate = (x_num_errors / x_num_shots) / distance_dictionary[(n, k)]
z_samples = sinter.collect(
num_workers=multiprocessing.cpu_count() - 1,
max_shots=maximum_shots,
max_errors=maximum_errors,
tasks=[sinter.Task(circuit=z_circuit, json_metadata={"p_CNOT": p_CNOT, "x_or_z": "z"})],
decoders=[decoder],
custom_decoders=decoder_dictionary,
print_progress=True,
)
z_num_errors = z_samples[0].errors
z_num_shots = z_samples[0].shots
z_logical_error_rate = (z_num_errors / z_num_shots) / distance_dictionary[(n, k)]
tt_logical_error_rate = x_logical_error_rate + z_logical_error_rate
print(f"{decoder} {circuit_type} circuit memory_X results:")
print(f"num errors is {x_num_errors}, num shots is {x_num_shots}, X logical error rate is {x_logical_error_rate}")
print(f"{decoder} {circuit_type} circuit memory_Z results:")
print(f"num errors is {z_num_errors}, num shots is {z_num_shots}, Z logical error rate is {z_logical_error_rate}")
print(f"Total (X+Z) logical error rate is {tt_logical_error_rate}")
# print(sinter.CSV_HEADER.strip())
# for sample in x_samples:
# print(sample.to_csv_line().strip())
# for sample in z_samples:
# print(sample.to_csv_line().strip())
def decoding_time(p_CNOT: float, decoder: str, num_shots: int = 10_000):
print(f"{decoder} decodes [[144,12,12]] BB code with 12 syndrome extraction rounds at noise rate {p_CNOT}:")
if p_CNOT == 0.0005:
z_circuit = stim.Circuit.from_file("StimCircuit/BB[[144,12,12]],memory_Z,error_rate=0.0005,syndrome_rounds=12.stim")
elif p_CNOT == 0.001:
z_circuit = stim.Circuit.from_file("StimCircuit/BB[[144,12,12]],memory_Z,error_rate=0.001,syndrome_rounds=12.stim")
else:
raise ValueError("Only support p_CNOT = 0.0005 or 0.001")
sampler = z_circuit.compile_detector_sampler()
shots, observables = sampler.sample(num_shots, separate_observables=True)
if decoder == "bp30+osd":
mydec = BPOSD(
z_circuit.detector_error_model(),
max_bp_iters=30,
bp_method="min_sum",
osd_order=10,
osd_method="osd_cs",
)
elif decoder == "beam8_230iters":
mydec = BeamSearch(z_circuit.detector_error_model())
elif decoder == "beam32_340iters":
mydec = BeamSearch(
z_circuit.detector_error_model(), beam_width=32, initial_iters=40, iters_per_round=30
)
elif decoder == "beam64_640iters":
mydec = BeamSearch(
z_circuit.detector_error_model(),
max_rounds=20,
beam_width=64,
initial_iters=40,
iters_per_round=30,
)
else:
raise ValueError("Only support 4 decoders bp30+osd, beam8_230iters, beam32_340iters, beam64_640iters")
time_array = np.zeros(num_shots)
for i in range(num_shots):
start_time = time.perf_counter()
mydec.decode(shots[i])
end_time = time.perf_counter()
time_array[i] = end_time - start_time
time_array.sort()
tail_index = int(0.999 * num_shots) - 1
print(f"The average decoding time is {time_array.mean()}, and the 99.9 percentile decoding time is {time_array[tail_index]}")
def generate_tasks(n: int, k: int):
if n == 144 and k == 12:
physical_error_rate_table = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006]
code_name = "[[144, 12, 12]] BB code"
elif n == 90 and k == 8:
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
code_name = "[[90, 8, 10]] BB code"
elif n == 450 and k == 32:
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
code_name = "[[450, 32, 8]] HGP code"
else:
raise ValueError(f"Do not support this (n={n}, k={k}) pair")
for p_CNOT in physical_error_rate_table:
for x_or_z in ["x", "z"]:
yield sinter.Task(
circuit=stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(n, k, p_CNOT, x_or_z)]),
json_metadata={
"code": code_name,
"syndrome_rounds": distance_dictionary[(n, k)],
"p_CNOT": p_CNOT,
"x_or_z": x_or_z,
},
)
def generate_tasks_for_XYZ_circuit():
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
code_name = "[[90, 8, 10]] BB code"
for p_CNOT in physical_error_rate_table:
for x_or_z in ["xyz_x", "xyz_z"]:
yield sinter.Task(
circuit=stim.Circuit.from_file("StimCircuit/" + StimCircuit_dictionary[(90, 8, p_CNOT, x_or_z)]),
json_metadata={
"code": code_name,
"syndrome_rounds": distance_dictionary[(90, 8)],
"p_CNOT": p_CNOT,
"x_or_z": x_or_z,
},
)
def full_simulation(n: int, k: int, maximum_shots: int = 1_000_000, maximum_errors: int = 100):
if n == 144 and k == 12:
file_name = "[[144,12,12]]_BB_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam32_340iters', 'beam64_640iters', 'beam64_32res_640iters']
elif n == 90 and k == 8:
file_name = "[[90,8,10]]_BB_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam64_640iters']
elif n == 450 and k == 32:
file_name = "[[450,32,8]]_HGP_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam64_640iters']
else:
raise ValueError(f"Do not support this (n={n}, k={k}) pair")
temp_file = "simulation_results/temp_files/" + file_name
final_file = f"simulation_results/" + file_name
num_cpus = multiprocessing.cpu_count() - 1
samples = sinter.collect(
num_workers=num_cpus,
max_shots=maximum_shots,
max_errors=maximum_errors,
tasks=generate_tasks(n, k),
decoders=decoder_list,
custom_decoders=decoder_dictionary,
save_resume_filepath=temp_file,
# BB_code.csv stores the (intermediate) results produced by different workers/CPUs.
# If the python interpreter is stopped or killed, calling this method again
# with the same save_resume_filepath will load the previous results
# from the file so it can resume where it left off.
print_progress=True,
)
# Save results to final_results.csv
with open(final_file, "w") as csvfile:
print(sinter.CSV_HEADER.strip(), file=csvfile)
for sample in samples:
print(sample.to_csv_line().strip(), file=csvfile)
if n == 90 and k == 8:
file_name = "[[90,8,10]]_BB_code_XYZ_circuit.csv"
decoder_list = ['bp30+osd', 'beam64_640iters']
temp_file = "simulation_results/temp_files/" + file_name
final_file = f"simulation_results/" + file_name
num_cpus = multiprocessing.cpu_count() - 1
samples = sinter.collect(
num_workers=num_cpus,
max_shots=maximum_shots,
max_errors=maximum_errors,
tasks=generate_tasks_for_XYZ_circuit(),
decoders=decoder_list,
custom_decoders=decoder_dictionary,
save_resume_filepath=temp_file,
# BB_code.csv stores the (intermediate) results produced by different workers/CPUs.
# If the python interpreter is stopped or killed, calling this method again
# with the same save_resume_filepath will load the previous results
# from the file so it can resume where it left off.
print_progress=True,
)
# Save results to final_results.csv
with open(final_file, "w") as csvfile:
print(sinter.CSV_HEADER.strip(), file=csvfile)
for sample in samples:
print(sample.to_csv_line().strip(), file=csvfile)
def plot_logical_error_rate(n: int, k: int):
if n == 144 and k == 12:
physical_error_rate_table = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006]
code_name = "[[144, 12, 12]] BB code"
file_name = "simulation_results/[[144,12,12]]_BB_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam32_340iters', 'beam64_640iters', 'beam64_32res_640iters']
elif n == 90 and k == 8:
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
code_name = "[[90, 8, 10]] BB code"
file_name = "simulation_results/[[90,8,10]]_BB_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam64_640iters']
elif n == 450 and k == 32:
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
code_name = "[[450, 32, 8]] HGP code"
file_name = "simulation_results/[[450,32,8]]_HGP_code.csv"
decoder_list = ['bp30+osd', 'beam8_230iters', 'beam64_640iters']
else:
raise ValueError(f"Do not support this (n={n}, k={k}) pair")
error_rate_dict = {item: index for index, item in enumerate(physical_error_rate_table)}
try:
df = pd.read_csv(file_name)
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
df["json_metadata"] = df["json_metadata"].apply(lambda x: json.loads(x) if pd.notna(x) and x != "" else {})
df.columns = df.columns.str.strip()
for decoder in decoder_list:
logical_error_list = [0.0 for _ in physical_error_rate_table]
for i in range(df.shape[0]):
if df["decoder"][i] == decoder:
if df["json_metadata"][i]["p_CNOT"] in error_rate_dict:
logical_error_list[error_rate_dict[df["json_metadata"][i]["p_CNOT"]]] += df["errors"][i] / df["shots"][i]
logical_error_list = [item / distance_dictionary[(n, k)] for item in logical_error_list]
# print(decoder)
# print(physical_error_rate_table)
# print(logical_error_list)
plt.plot(
physical_error_rate_table,
logical_error_list,
marker="o",
label=decoder,
)
if n == 90 and k == 8:
physical_error_rate_table = [0.002, 0.003, 0.004, 0.005]
file_name = "simulation_results/[[90,8,10]]_BB_code_XYZ_circuit.csv"
decoder_list = ['bp30+osd', 'beam64_640iters']
error_rate_dict = {item: index for index, item in enumerate(physical_error_rate_table)}
try:
df = pd.read_csv(file_name)
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
df["json_metadata"] = df["json_metadata"].apply(lambda x: json.loads(x) if pd.notna(x) and x != "" else {})
df.columns = df.columns.str.strip()
for decoder in decoder_list:
logical_error_list = [0.0 for _ in physical_error_rate_table]
for i in range(df.shape[0]):
if df["decoder"][i] == decoder:
if df["json_metadata"][i]["p_CNOT"] in error_rate_dict:
logical_error_list[error_rate_dict[df["json_metadata"][i]["p_CNOT"]]] += df["errors"][i] / df["shots"][i]
logical_error_list = [item / distance_dictionary[(n, k)] for item in logical_error_list]
# print(decoder + "_XYZ")
# print(physical_error_rate_table)
# print(logical_error_list)
plt.plot(
physical_error_rate_table,
logical_error_list,
marker="o",
label=decoder+"_XYZ",
)
plt.loglog()
plt.xlim([8e-4, 7e-3])
plt.xlabel("physical error rate")
plt.ylabel("logical error rate per syndrome extraction round")
plt.legend()
plt.title(code_name)
plt.grid(which="major")
plt.grid(which="minor")
plt.show()