sinter 1.14.dev1712801263__tar.gz → 1.14.dev1712884560__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sinter might be problematic. Click here for more details.

Files changed (53) hide show
  1. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/PKG-INFO +346 -346
  2. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/README.md +332 -332
  3. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/requirements.txt +4 -4
  4. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/setup.cfg +4 -4
  5. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/setup.py +42 -42
  6. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/__init__.py +56 -56
  7. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_anon_task_stats.py +86 -86
  8. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_anon_task_stats_test.py +35 -35
  9. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection.py +456 -456
  10. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection_options.py +106 -106
  11. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection_options_test.py +24 -24
  12. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection_test.py +210 -210
  13. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection_tracker_for_single_task.py +230 -230
  14. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_collection_work_manager.py +275 -275
  15. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_csv_out.py +74 -74
  16. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding.py +419 -419
  17. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_all_built_in_decoders.py +12 -12
  18. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_decoder_class.py +144 -144
  19. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_fusion_blossom.py +190 -190
  20. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_pymatching.py +81 -81
  21. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_test.py +388 -388
  22. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_decoding_vacuous.py +38 -38
  23. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_existing_data.py +172 -172
  24. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_existing_data_test.py +41 -41
  25. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main.py +39 -39
  26. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_collect.py +350 -350
  27. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_collect_test.py +452 -452
  28. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_combine.py +85 -85
  29. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_combine_test.py +153 -153
  30. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_plot.py +775 -775
  31. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_plot_test.py +445 -445
  32. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_predict.py +75 -75
  33. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_main_predict_test.py +36 -36
  34. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_plotting.py +467 -467
  35. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_plotting_test.py +105 -105
  36. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_predict.py +383 -383
  37. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_predict_test.py +227 -227
  38. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_printer.py +65 -65
  39. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_probability_util.py +502 -502
  40. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_probability_util_test.py +281 -281
  41. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_task.py +311 -311
  42. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_task_stats.py +178 -178
  43. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_task_stats_test.py +89 -89
  44. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_task_test.py +38 -38
  45. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_worker.py +212 -212
  46. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter/_worker_test.py +134 -134
  47. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/PKG-INFO +346 -346
  48. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/readme_example_plot.png +0 -0
  49. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/SOURCES.txt +0 -0
  50. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/dependency_links.txt +0 -0
  51. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/entry_points.txt +0 -0
  52. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/requires.txt +0 -0
  53. {sinter-1.14.dev1712801263 → sinter-1.14.dev1712884560}/src/sinter.egg-info/top_level.txt +0 -0
@@ -1,346 +1,346 @@
1
- Metadata-Version: 2.1
2
- Name: sinter
3
- Version: 1.14.dev1712801263
4
- Summary: Samples stim circuits and decodes them using pymatching.
5
- Author: Craig Gidney
6
- Author-email: craig.gidney@gmail.com
7
- License: Apache 2
8
- Requires-Python: >=3.7.0
9
- Description-Content-Type: text/markdown
10
- Requires-Dist: matplotlib~=3.5
11
- Requires-Dist: numpy~=1.22
12
- Requires-Dist: stim
13
- Requires-Dist: scipy~=1.9
14
-
15
- # sinter: fast QEC sampling
16
-
17
- Sinter is a software tool/library for doing fast monte carlo sampling of
18
- quantum error correction circuits.
19
-
20
- - [How it works](#how_it_works)
21
- - [How to install](#how_to_install)
22
- - [How to use: Python API](#how_to_use_python)
23
- - [Sinter Python API Reference](doc/sinter_api.md)
24
- - [How to use: Linux Command Line](#how_to_use_linux)
25
- - [Sinter Command Line Reference](doc/sinter_command_line.md)
26
- - [The csv format for sample statistics](#csv_format)
27
-
28
- <a name="how_to_works"></a>
29
- # How it works
30
-
31
- Sinter takes Stim circuits annotated with noise, detectors, and logical
32
- observables.
33
- It uses stim to sample the circuits and a decoder such as pymatching to predict
34
- whether the logical observables were flipped or not, given the detector data.
35
- It records how often this succeeds, and how often it fails (the error rate).
36
-
37
- Sinter uses python multiprocessing to do parallel sampling across multiple CPU
38
- cores, dynamically decides which circuits need more samples based on parameters
39
- specified by the user (such as a target number of errors), saves the results to
40
- as simple CSV format, and has some basic plotting functionality for viewing the
41
- results.
42
-
43
- Sinter doesn't support cloud compute, but it does scale well on a single
44
- machine.
45
- I've tested it on 2 core machines, 4 core machines, and 96 core machines.
46
- Although there are potential pitfalls (e.g. setting batch sizes too large causes
47
- thrashing), sinter generally achieves good resource utilization of the processes
48
- you assign to it.
49
-
50
- <a name="how_to_install"></a>
51
- # How to install
52
-
53
- Sinter is available as a pypi package. It can be installed using pip:
54
-
55
- ```
56
- pip install sinter
57
- ```
58
-
59
- When you are in a python virtual environment with sinter installed, you have
60
- access to a command line command `sinter` which can be used to perform tasks
61
- from the command line. You can also `import sinter` in a python program in order
62
- to use sinter's python API.
63
-
64
- <a name="how_to_use_python"></a>
65
- # How to use: Python API
66
-
67
- This example assumes you are in a python environment with `sinter` and
68
- `pymatching` installed.
69
-
70
- ```python
71
- import stim
72
- import sinter
73
- import matplotlib.pyplot as plt
74
-
75
-
76
- # Generates surface code circuit tasks using Stim's circuit generation.
77
- def generate_example_tasks():
78
- for p in [0.001, 0.005, 0.01]:
79
- for d in [3, 5]:
80
- yield sinter.Task(
81
- circuit=stim.Circuit.generated(
82
- rounds=d,
83
- distance=d,
84
- after_clifford_depolarization=p,
85
- code_task=f'surface_code:rotated_memory_x',
86
- ),
87
- json_metadata={
88
- 'p': p,
89
- 'd': d,
90
- },
91
- )
92
-
93
-
94
- def main():
95
- # Collect the samples (takes a few minutes).
96
- samples = sinter.collect(
97
- num_workers=4,
98
- max_shots=1_000_000,
99
- max_errors=1000,
100
- tasks=generate_example_tasks(),
101
- decoders=['pymatching'],
102
- )
103
-
104
- # Print samples as CSV data.
105
- print(sinter.CSV_HEADER)
106
- for sample in samples:
107
- print(sample.to_csv_line())
108
-
109
- # Render a matplotlib plot of the data.
110
- fig, ax = plt.subplots(1, 1)
111
- sinter.plot_error_rate(
112
- ax=ax,
113
- stats=samples,
114
- group_func=lambda stat: f"Rotated Surface Code d={stat.json_metadata['d']}",
115
- x_func=lambda stat: stat.json_metadata['p'],
116
- )
117
- ax.loglog()
118
- ax.set_ylim(1e-5, 1)
119
- ax.grid()
120
- ax.set_title('Logical Error Rate vs Physical Error Rate')
121
- ax.set_ylabel('Logical Error Probability (per shot)')
122
- ax.set_xlabel('Physical Error Rate')
123
- ax.legend()
124
-
125
- # Save to file and also open in a window.
126
- fig.savefig('plot.png')
127
- plt.show()
128
-
129
-
130
- # NOTE: This is actually necessary! If the code inside 'main()' was at the
131
- # module level, the multiprocessing children spawned by sinter.collect would
132
- # also attempt to run that code.
133
- if __name__ == '__main__':
134
- main()
135
- ```
136
-
137
- Example output to stdout:
138
-
139
- ```
140
- shots, errors, discards, seconds,decoder,strong_id,json_metadata
141
- 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
142
- 53498, 1099, 0, 6.52,pymatching,3f40432443a99b933fb548b831fb54e7e245d9d73a35c03ea5a2fb2ce270f8c8,"{""d"":3,""p"":0.005}"
143
- 16269, 1023, 0, 3.23,pymatching,17b2e0c99560d20307204494ac50e31b33e50721b4ebae99d9e3577ae7248874,"{""d"":3,""p"":0.01}"
144
- 1000000, 151, 0, 77.3,pymatching,e179a18739201250371ffaae0197d8fa19d26b58dfc2942f9f1c85568645387a,"{""d"":5,""p"":0.001}"
145
- 11363, 1068, 0, 12.5,pymatching,a4dec28934a033215ff1389651a26114ecc22016a6e122008830cf7dd04ba5ad,"{""d"":5,""p"":0.01}"
146
- 61569, 1001, 0, 24.5,pymatching,2fefcc356752482fb4c6d912c228f6d18762f5752796c668b6abeb7775f5de92,"{""d"":5,""p"":0.005}"
147
- ```
148
-
149
- and the corresponding image saved to `plot.png`:
150
-
151
- ![Example plot](readme_example_plot.png)
152
-
153
- ## python API utility methods
154
-
155
- Sinter's python module exposes a variety of methods that are handy for plotting
156
- or analyzing QEC data.
157
- See the [sinter API reference](https://github.com/quantumlib/Stim/blob/main/doc/sinter_api.md).
158
-
159
- <a name="how_to_use_linux"></a>
160
- # How to use: Linux Command Line
161
-
162
- This example assumes you are using a linux command line in a python virtualenv with `sinter` installed.
163
-
164
- ## pick circuits
165
-
166
- For this example, we will use Stim's circuit generation functionality to produce
167
- circuits to benchmark.
168
- We will make rotated surface code circuits with various physical error rates,
169
- with filenames like `rotated_d5_p0.001_surface_code.stim`.
170
-
171
- ```bash
172
- mkdir -p circuits
173
- python -c "
174
-
175
- import stim
176
-
177
- for p in [0.001, 0.005, 0.01]:
178
- for d in [3, 5]:
179
- with open(f'circuits/d={d},p={p},b=X,type=rotated_surface_memory.stim', 'w') as f:
180
- c = stim.Circuit.generated(
181
- rounds=d,
182
- distance=d,
183
- after_clifford_depolarization=p,
184
- after_reset_flip_probability=p,
185
- before_measure_flip_probability=p,
186
- before_round_data_depolarization=p,
187
- code_task=f'surface_code:rotated_memory_x')
188
- print(c, file=f)
189
- "
190
- ```
191
-
192
- Normally, making the circuit files is the hardest step, because they are what
193
- specifies the problem you are sampling from.
194
- Almost all of the work you do will generally involve creating the exact perfect
195
- circuit file for your needs.
196
- But this is just an example, so we'll use normal surface code circuits.
197
-
198
- # collect
199
-
200
- You can use sinter to collect statistics on each circuit by using the `sinter collect` command.
201
- This command takes options specifying how much data to collect, how to do decoding, etc.
202
-
203
- The `processes` argument decides how many workers to use. Set it to `auto` to set
204
- it to the number of CPUs on your machine.
205
-
206
- The `metadata_func` argument can be used to specify custom python expression that turns the `path`
207
- into a dictionary or other JSON object associated with the circuit.
208
- If you set `metadata_func` to `auto` then will use the method
209
- `sinter.comma_separated_key_values(path)` which parses
210
- stim circuit paths like `folder/a=2,b=test.stim` into a dictionary like `{'a': 2, 'b': 'test'}`.
211
-
212
- By default, sinter writes the collected statistics to stdout as CSV data.
213
- One particularly important option that changes this behavior is `--save_resume_filepath`,
214
- which allows the command to be interrupted and restarted without losing data.
215
- Any data already at the file specified by `--save_resume_filepath` will count towards the
216
- amount of statistics asked to be collected, and sinter will append new statistics to this file
217
- instead of overwriting it.
218
-
219
- ```bash
220
- sinter collect \
221
- --processes auto \
222
- --circuits circuits/*.stim \
223
- --metadata_func auto \
224
- --decoders pymatching \
225
- --max_shots 1_000_000 \
226
- --max_errors 1000 \
227
- --save_resume_filepath stats.csv
228
- ```
229
-
230
- Beware that if you SIGKILL or SIGTEM sinter, instead of just using SIGINT, it's possible
231
- (though unlikely) that you are killing it just as it writes a row of CSV data. This truncates
232
- the data, which requires manual intervention on your part to fix (e.g. by deleting the partial row
233
- using a text editor).
234
-
235
- # combine
236
-
237
- Note that the CSV data written by sinter will contain multiple rows for each case, because
238
- sinter starts by running small batches to see roughly what the error rate is before moving
239
- to larger batch sizes.
240
-
241
- You can get a single-row-per-case CSV file by using `sinter combine`:
242
-
243
- ```bash
244
- sinter combine stats.csv
245
- ```
246
-
247
- ```
248
- shots, errors, discards, seconds,decoder,strong_id,json_metadata
249
- 58591, 1067, 0, 5.50,pymatching,bb46c8fca4d9fd9d4d27a5039686332ac5e24011a7f2aea5a65f6040445567c0,"{""b"":""X"",""d"":3,""p"":0.005,""type"":""rotated_surface_memory""}"
250
- 1000000, 901, 0, 73.4,pymatching,4c0780830fe1747ab22767b69d1178f803943c83dd4afa6d241acf02e6dfa71f,"{""b"":""X"",""d"":3,""p"":0.001,""type"":""rotated_surface_memory""}"
251
- 16315, 1026, 0, 2.39,pymatching,64d81b177ef1a455644ac3e03f374394cd8ad385ba2ee0ac147b2405107564fc,"{""b"":""X"",""d"":3,""p"":0.01,""type"":""rotated_surface_memory""}"
252
- 1000000, 157, 0, 116.5,pymatching,100855c078af0936d098cecbd8bfb7591c0951ae69527c002c9c5f4c79bde129,"{""b"":""X"",""d"":5,""p"":0.001,""type"":""rotated_surface_memory""}"
253
- 61677, 1005, 0, 21.2,pymatching,6d7b8b312a5460c7fe08119d3c7a040daa25bd34d524611160e4aac6196293fe,"{""b"":""X"",""d"":5,""p"":0.005,""type"":""rotated_surface_memory""}"
254
- 10891, 1021, 0, 7.43,pymatching,477252e968f0f22f64ccb058c0e1e9c77b765f60f74df8b6707de7ec65ed13b7,"{""b"":""X"",""d"":5,""p"":0.01,""type"":""rotated_surface_memory""}"
255
- ```
256
-
257
- # plot
258
-
259
- You can use `sinter plot` to view the results you've collected.
260
- This command takes a CSV file, an argument `--group_func` indicating how to
261
- group the statistics into curves, an argument `--x_func` indicating how to
262
- pick the X coordinate of each point, and various other arguments. Each `*_func`
263
- argument takes a string that will be evaluated as a python expression, with
264
- various useful values in scope such as a `metadata` value containing the
265
- json metadata for the various points being evaluated. There is also a special
266
- `m` value where `m.key` is shorthand for `metadata.get('key', None)`.
267
-
268
- Here is an example of a `sinter plot` command:
269
-
270
- ```bash
271
- sinter plot \
272
- --in stats.csv \
273
- --group_func "f'''Rotated Surface Code d={m.d}'''" \
274
- --x_func m.p \
275
- --xaxis "[log]Physical Error Rate" \
276
- --fig_size 1024 1024 \
277
- --out surface_code_figure.png \
278
- --show
279
- ```
280
-
281
- Which will save a png image of, and also open a window showing, a plot like this one:
282
-
283
- ![Example plot](readme_example_plot.png)
284
-
285
- <a name="csv_format"></a>
286
- # The csv format for sample statistics
287
-
288
- Sinter saves samples as a table using a Comma Separated Value format.
289
- For example:
290
-
291
- ```
292
- shots,errors,discards,seconds,decoder,strong_id,json_metadata
293
- 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
294
- 53498, 1099, 0, 6.52,pymatching,3f40432443a99b933fb548b831fb54e7e245d9d73a35c03ea5a2fb2ce270f8c8,"{""d"":3,""p"":0.005}"
295
- 16269, 1023, 0, 3.23,pymatching,17b2e0c99560d20307204494ac50e31b33e50721b4ebae99d9e3577ae7248874,"{""d"":3,""p"":0.01}"
296
- 1000000, 151, 0, 77.3,pymatching,e179a18739201250371ffaae0197d8fa19d26b58dfc2942f9f1c85568645387a,"{""d"":5,""p"":0.001}"
297
- 11363, 1068, 0, 12.5,pymatching,a4dec28934a033215ff1389651a26114ecc22016a6e122008830cf7dd04ba5ad,"{""d"":5,""p"":0.01}"
298
- 61569, 1001, 0, 24.5,pymatching,2fefcc356752482fb4c6d912c228f6d18762f5752796c668b6abeb7775f5de92,"{""d"":5,""p"":0.005}"
299
- ```
300
-
301
- The columns are:
302
-
303
- - `shots` (unsigned int): How many times the circuit was sampled.
304
- - `errors` (unsigned int): How many times the decoder failed to predict any logical observable.
305
- - `discards` (unsigned int): How many times a shot was discarded because a postselected detector fired or because the decoder incorrectly predicted the value of a postselected observable. Discarded shots never count as errors.
306
- - `seconds` (non-negative float): How many CPU core seconds it took to simulate and decode these shots.
307
- - `decoder` (str): Which decoder was used.
308
- - `strong_id` (str):
309
- Hex representation of a cryptographic hash of the problem
310
- being sampled from.
311
- The hashed data includes the exact circuit that was simulated,
312
- the decoder that was used,
313
- the exact detector error model that was given to the decoder,
314
- the postselection rules that were applied,
315
- and the metadata associated with the circuit.
316
- The purpose of the strong id is to make it impossible to accidentally combine
317
- shots that were from separate circuits or separate versions of a circuit.
318
- - `json_metadata` (json): A free form field that can store any value representable in
319
- [Java Script Object Notation](https://json.org). For example, this could be a
320
- dictionary with helpful keys like "noise_level" or "circuit_name". The json
321
- value is serialized into JSON and then escaped so that it can be put into the
322
- CSV data (e.g. quotes get doubled up).
323
- - `custom_counts` (json[Dict[str, int]]): An optional field that can store a
324
- dictionary from string keys to integer counts represented in
325
- [Java Script Object Notation](https://json.org).
326
- The counts can be a huge variety of things, ranging from per-observable error
327
- counts to detection event counts. In general, any value that should be added
328
- when merging rows could be in these counters.
329
-
330
- Note shots may be spread across multiple rows.
331
- For example, this data:
332
-
333
- ```
334
- shots,errors,discards,seconds,decoder,strong_id,json_metadata
335
- 500000, 437, 0, 20.5,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
336
- 500000, 400, 0, 16.1,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
337
- ```
338
-
339
- has the same total statistics as this data:
340
-
341
- ```
342
- shots,errors,discards,seconds,decoder,strong_id,json_metadata
343
- 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
344
- ```
345
-
346
- just split over two rows instead of combined into one.
1
+ Metadata-Version: 2.1
2
+ Name: sinter
3
+ Version: 1.14.dev1712884560
4
+ Summary: Samples stim circuits and decodes them using pymatching.
5
+ Author: Craig Gidney
6
+ Author-email: craig.gidney@gmail.com
7
+ License: Apache 2
8
+ Requires-Python: >=3.7.0
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: matplotlib~=3.5
11
+ Requires-Dist: numpy~=1.22
12
+ Requires-Dist: stim
13
+ Requires-Dist: scipy~=1.9
14
+
15
+ # sinter: fast QEC sampling
16
+
17
+ Sinter is a software tool/library for doing fast monte carlo sampling of
18
+ quantum error correction circuits.
19
+
20
+ - [How it works](#how_it_works)
21
+ - [How to install](#how_to_install)
22
+ - [How to use: Python API](#how_to_use_python)
23
+ - [Sinter Python API Reference](doc/sinter_api.md)
24
+ - [How to use: Linux Command Line](#how_to_use_linux)
25
+ - [Sinter Command Line Reference](doc/sinter_command_line.md)
26
+ - [The csv format for sample statistics](#csv_format)
27
+
28
+ <a name="how_to_works"></a>
29
+ # How it works
30
+
31
+ Sinter takes Stim circuits annotated with noise, detectors, and logical
32
+ observables.
33
+ It uses stim to sample the circuits and a decoder such as pymatching to predict
34
+ whether the logical observables were flipped or not, given the detector data.
35
+ It records how often this succeeds, and how often it fails (the error rate).
36
+
37
+ Sinter uses python multiprocessing to do parallel sampling across multiple CPU
38
+ cores, dynamically decides which circuits need more samples based on parameters
39
+ specified by the user (such as a target number of errors), saves the results to
40
+ as simple CSV format, and has some basic plotting functionality for viewing the
41
+ results.
42
+
43
+ Sinter doesn't support cloud compute, but it does scale well on a single
44
+ machine.
45
+ I've tested it on 2 core machines, 4 core machines, and 96 core machines.
46
+ Although there are potential pitfalls (e.g. setting batch sizes too large causes
47
+ thrashing), sinter generally achieves good resource utilization of the processes
48
+ you assign to it.
49
+
50
+ <a name="how_to_install"></a>
51
+ # How to install
52
+
53
+ Sinter is available as a pypi package. It can be installed using pip:
54
+
55
+ ```
56
+ pip install sinter
57
+ ```
58
+
59
+ When you are in a python virtual environment with sinter installed, you have
60
+ access to a command line command `sinter` which can be used to perform tasks
61
+ from the command line. You can also `import sinter` in a python program in order
62
+ to use sinter's python API.
63
+
64
+ <a name="how_to_use_python"></a>
65
+ # How to use: Python API
66
+
67
+ This example assumes you are in a python environment with `sinter` and
68
+ `pymatching` installed.
69
+
70
+ ```python
71
+ import stim
72
+ import sinter
73
+ import matplotlib.pyplot as plt
74
+
75
+
76
+ # Generates surface code circuit tasks using Stim's circuit generation.
77
+ def generate_example_tasks():
78
+ for p in [0.001, 0.005, 0.01]:
79
+ for d in [3, 5]:
80
+ yield sinter.Task(
81
+ circuit=stim.Circuit.generated(
82
+ rounds=d,
83
+ distance=d,
84
+ after_clifford_depolarization=p,
85
+ code_task=f'surface_code:rotated_memory_x',
86
+ ),
87
+ json_metadata={
88
+ 'p': p,
89
+ 'd': d,
90
+ },
91
+ )
92
+
93
+
94
+ def main():
95
+ # Collect the samples (takes a few minutes).
96
+ samples = sinter.collect(
97
+ num_workers=4,
98
+ max_shots=1_000_000,
99
+ max_errors=1000,
100
+ tasks=generate_example_tasks(),
101
+ decoders=['pymatching'],
102
+ )
103
+
104
+ # Print samples as CSV data.
105
+ print(sinter.CSV_HEADER)
106
+ for sample in samples:
107
+ print(sample.to_csv_line())
108
+
109
+ # Render a matplotlib plot of the data.
110
+ fig, ax = plt.subplots(1, 1)
111
+ sinter.plot_error_rate(
112
+ ax=ax,
113
+ stats=samples,
114
+ group_func=lambda stat: f"Rotated Surface Code d={stat.json_metadata['d']}",
115
+ x_func=lambda stat: stat.json_metadata['p'],
116
+ )
117
+ ax.loglog()
118
+ ax.set_ylim(1e-5, 1)
119
+ ax.grid()
120
+ ax.set_title('Logical Error Rate vs Physical Error Rate')
121
+ ax.set_ylabel('Logical Error Probability (per shot)')
122
+ ax.set_xlabel('Physical Error Rate')
123
+ ax.legend()
124
+
125
+ # Save to file and also open in a window.
126
+ fig.savefig('plot.png')
127
+ plt.show()
128
+
129
+
130
+ # NOTE: This is actually necessary! If the code inside 'main()' was at the
131
+ # module level, the multiprocessing children spawned by sinter.collect would
132
+ # also attempt to run that code.
133
+ if __name__ == '__main__':
134
+ main()
135
+ ```
136
+
137
+ Example output to stdout:
138
+
139
+ ```
140
+ shots, errors, discards, seconds,decoder,strong_id,json_metadata
141
+ 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
142
+ 53498, 1099, 0, 6.52,pymatching,3f40432443a99b933fb548b831fb54e7e245d9d73a35c03ea5a2fb2ce270f8c8,"{""d"":3,""p"":0.005}"
143
+ 16269, 1023, 0, 3.23,pymatching,17b2e0c99560d20307204494ac50e31b33e50721b4ebae99d9e3577ae7248874,"{""d"":3,""p"":0.01}"
144
+ 1000000, 151, 0, 77.3,pymatching,e179a18739201250371ffaae0197d8fa19d26b58dfc2942f9f1c85568645387a,"{""d"":5,""p"":0.001}"
145
+ 11363, 1068, 0, 12.5,pymatching,a4dec28934a033215ff1389651a26114ecc22016a6e122008830cf7dd04ba5ad,"{""d"":5,""p"":0.01}"
146
+ 61569, 1001, 0, 24.5,pymatching,2fefcc356752482fb4c6d912c228f6d18762f5752796c668b6abeb7775f5de92,"{""d"":5,""p"":0.005}"
147
+ ```
148
+
149
+ and the corresponding image saved to `plot.png`:
150
+
151
+ ![Example plot](readme_example_plot.png)
152
+
153
+ ## python API utility methods
154
+
155
+ Sinter's python module exposes a variety of methods that are handy for plotting
156
+ or analyzing QEC data.
157
+ See the [sinter API reference](https://github.com/quantumlib/Stim/blob/main/doc/sinter_api.md).
158
+
159
+ <a name="how_to_use_linux"></a>
160
+ # How to use: Linux Command Line
161
+
162
+ This example assumes you are using a linux command line in a python virtualenv with `sinter` installed.
163
+
164
+ ## pick circuits
165
+
166
+ For this example, we will use Stim's circuit generation functionality to produce
167
+ circuits to benchmark.
168
+ We will make rotated surface code circuits with various physical error rates,
169
+ with filenames like `rotated_d5_p0.001_surface_code.stim`.
170
+
171
+ ```bash
172
+ mkdir -p circuits
173
+ python -c "
174
+
175
+ import stim
176
+
177
+ for p in [0.001, 0.005, 0.01]:
178
+ for d in [3, 5]:
179
+ with open(f'circuits/d={d},p={p},b=X,type=rotated_surface_memory.stim', 'w') as f:
180
+ c = stim.Circuit.generated(
181
+ rounds=d,
182
+ distance=d,
183
+ after_clifford_depolarization=p,
184
+ after_reset_flip_probability=p,
185
+ before_measure_flip_probability=p,
186
+ before_round_data_depolarization=p,
187
+ code_task=f'surface_code:rotated_memory_x')
188
+ print(c, file=f)
189
+ "
190
+ ```
191
+
192
+ Normally, making the circuit files is the hardest step, because they are what
193
+ specifies the problem you are sampling from.
194
+ Almost all of the work you do will generally involve creating the exact perfect
195
+ circuit file for your needs.
196
+ But this is just an example, so we'll use normal surface code circuits.
197
+
198
+ # collect
199
+
200
+ You can use sinter to collect statistics on each circuit by using the `sinter collect` command.
201
+ This command takes options specifying how much data to collect, how to do decoding, etc.
202
+
203
+ The `processes` argument decides how many workers to use. Set it to `auto` to set
204
+ it to the number of CPUs on your machine.
205
+
206
+ The `metadata_func` argument can be used to specify custom python expression that turns the `path`
207
+ into a dictionary or other JSON object associated with the circuit.
208
+ If you set `metadata_func` to `auto` then will use the method
209
+ `sinter.comma_separated_key_values(path)` which parses
210
+ stim circuit paths like `folder/a=2,b=test.stim` into a dictionary like `{'a': 2, 'b': 'test'}`.
211
+
212
+ By default, sinter writes the collected statistics to stdout as CSV data.
213
+ One particularly important option that changes this behavior is `--save_resume_filepath`,
214
+ which allows the command to be interrupted and restarted without losing data.
215
+ Any data already at the file specified by `--save_resume_filepath` will count towards the
216
+ amount of statistics asked to be collected, and sinter will append new statistics to this file
217
+ instead of overwriting it.
218
+
219
+ ```bash
220
+ sinter collect \
221
+ --processes auto \
222
+ --circuits circuits/*.stim \
223
+ --metadata_func auto \
224
+ --decoders pymatching \
225
+ --max_shots 1_000_000 \
226
+ --max_errors 1000 \
227
+ --save_resume_filepath stats.csv
228
+ ```
229
+
230
+ Beware that if you SIGKILL or SIGTEM sinter, instead of just using SIGINT, it's possible
231
+ (though unlikely) that you are killing it just as it writes a row of CSV data. This truncates
232
+ the data, which requires manual intervention on your part to fix (e.g. by deleting the partial row
233
+ using a text editor).
234
+
235
+ # combine
236
+
237
+ Note that the CSV data written by sinter will contain multiple rows for each case, because
238
+ sinter starts by running small batches to see roughly what the error rate is before moving
239
+ to larger batch sizes.
240
+
241
+ You can get a single-row-per-case CSV file by using `sinter combine`:
242
+
243
+ ```bash
244
+ sinter combine stats.csv
245
+ ```
246
+
247
+ ```
248
+ shots, errors, discards, seconds,decoder,strong_id,json_metadata
249
+ 58591, 1067, 0, 5.50,pymatching,bb46c8fca4d9fd9d4d27a5039686332ac5e24011a7f2aea5a65f6040445567c0,"{""b"":""X"",""d"":3,""p"":0.005,""type"":""rotated_surface_memory""}"
250
+ 1000000, 901, 0, 73.4,pymatching,4c0780830fe1747ab22767b69d1178f803943c83dd4afa6d241acf02e6dfa71f,"{""b"":""X"",""d"":3,""p"":0.001,""type"":""rotated_surface_memory""}"
251
+ 16315, 1026, 0, 2.39,pymatching,64d81b177ef1a455644ac3e03f374394cd8ad385ba2ee0ac147b2405107564fc,"{""b"":""X"",""d"":3,""p"":0.01,""type"":""rotated_surface_memory""}"
252
+ 1000000, 157, 0, 116.5,pymatching,100855c078af0936d098cecbd8bfb7591c0951ae69527c002c9c5f4c79bde129,"{""b"":""X"",""d"":5,""p"":0.001,""type"":""rotated_surface_memory""}"
253
+ 61677, 1005, 0, 21.2,pymatching,6d7b8b312a5460c7fe08119d3c7a040daa25bd34d524611160e4aac6196293fe,"{""b"":""X"",""d"":5,""p"":0.005,""type"":""rotated_surface_memory""}"
254
+ 10891, 1021, 0, 7.43,pymatching,477252e968f0f22f64ccb058c0e1e9c77b765f60f74df8b6707de7ec65ed13b7,"{""b"":""X"",""d"":5,""p"":0.01,""type"":""rotated_surface_memory""}"
255
+ ```
256
+
257
+ # plot
258
+
259
+ You can use `sinter plot` to view the results you've collected.
260
+ This command takes a CSV file, an argument `--group_func` indicating how to
261
+ group the statistics into curves, an argument `--x_func` indicating how to
262
+ pick the X coordinate of each point, and various other arguments. Each `*_func`
263
+ argument takes a string that will be evaluated as a python expression, with
264
+ various useful values in scope such as a `metadata` value containing the
265
+ json metadata for the various points being evaluated. There is also a special
266
+ `m` value where `m.key` is shorthand for `metadata.get('key', None)`.
267
+
268
+ Here is an example of a `sinter plot` command:
269
+
270
+ ```bash
271
+ sinter plot \
272
+ --in stats.csv \
273
+ --group_func "f'''Rotated Surface Code d={m.d}'''" \
274
+ --x_func m.p \
275
+ --xaxis "[log]Physical Error Rate" \
276
+ --fig_size 1024 1024 \
277
+ --out surface_code_figure.png \
278
+ --show
279
+ ```
280
+
281
+ Which will save a png image of, and also open a window showing, a plot like this one:
282
+
283
+ ![Example plot](readme_example_plot.png)
284
+
285
+ <a name="csv_format"></a>
286
+ # The csv format for sample statistics
287
+
288
+ Sinter saves samples as a table using a Comma Separated Value format.
289
+ For example:
290
+
291
+ ```
292
+ shots,errors,discards,seconds,decoder,strong_id,json_metadata
293
+ 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
294
+ 53498, 1099, 0, 6.52,pymatching,3f40432443a99b933fb548b831fb54e7e245d9d73a35c03ea5a2fb2ce270f8c8,"{""d"":3,""p"":0.005}"
295
+ 16269, 1023, 0, 3.23,pymatching,17b2e0c99560d20307204494ac50e31b33e50721b4ebae99d9e3577ae7248874,"{""d"":3,""p"":0.01}"
296
+ 1000000, 151, 0, 77.3,pymatching,e179a18739201250371ffaae0197d8fa19d26b58dfc2942f9f1c85568645387a,"{""d"":5,""p"":0.001}"
297
+ 11363, 1068, 0, 12.5,pymatching,a4dec28934a033215ff1389651a26114ecc22016a6e122008830cf7dd04ba5ad,"{""d"":5,""p"":0.01}"
298
+ 61569, 1001, 0, 24.5,pymatching,2fefcc356752482fb4c6d912c228f6d18762f5752796c668b6abeb7775f5de92,"{""d"":5,""p"":0.005}"
299
+ ```
300
+
301
+ The columns are:
302
+
303
+ - `shots` (unsigned int): How many times the circuit was sampled.
304
+ - `errors` (unsigned int): How many times the decoder failed to predict any logical observable.
305
+ - `discards` (unsigned int): How many times a shot was discarded because a postselected detector fired or because the decoder incorrectly predicted the value of a postselected observable. Discarded shots never count as errors.
306
+ - `seconds` (non-negative float): How many CPU core seconds it took to simulate and decode these shots.
307
+ - `decoder` (str): Which decoder was used.
308
+ - `strong_id` (str):
309
+ Hex representation of a cryptographic hash of the problem
310
+ being sampled from.
311
+ The hashed data includes the exact circuit that was simulated,
312
+ the decoder that was used,
313
+ the exact detector error model that was given to the decoder,
314
+ the postselection rules that were applied,
315
+ and the metadata associated with the circuit.
316
+ The purpose of the strong id is to make it impossible to accidentally combine
317
+ shots that were from separate circuits or separate versions of a circuit.
318
+ - `json_metadata` (json): A free form field that can store any value representable in
319
+ [Java Script Object Notation](https://json.org). For example, this could be a
320
+ dictionary with helpful keys like "noise_level" or "circuit_name". The json
321
+ value is serialized into JSON and then escaped so that it can be put into the
322
+ CSV data (e.g. quotes get doubled up).
323
+ - `custom_counts` (json[Dict[str, int]]): An optional field that can store a
324
+ dictionary from string keys to integer counts represented in
325
+ [Java Script Object Notation](https://json.org).
326
+ The counts can be a huge variety of things, ranging from per-observable error
327
+ counts to detection event counts. In general, any value that should be added
328
+ when merging rows could be in these counters.
329
+
330
+ Note shots may be spread across multiple rows.
331
+ For example, this data:
332
+
333
+ ```
334
+ shots,errors,discards,seconds,decoder,strong_id,json_metadata
335
+ 500000, 437, 0, 20.5,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
336
+ 500000, 400, 0, 16.1,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
337
+ ```
338
+
339
+ has the same total statistics as this data:
340
+
341
+ ```
342
+ shots,errors,discards,seconds,decoder,strong_id,json_metadata
343
+ 1000000, 837, 0, 36.6,pymatching,9f7e20c54fec45b6aef7491b774dd5c0a3b9a005aa82faf5b9c051d6e40d60a9,"{""d"":3,""p"":0.001}"
344
+ ```
345
+
346
+ just split over two rows instead of combined into one.