Commit 008a3d2
authored
Return Python decode result as NumPy arrays (#558)
## Description
Updates Python decoder result access so decoded correction data is
exposed as NumPy arrays instead of Python lists.
This is a breaking Python API change: `DecoderResult.result` now returns
a 1-D NumPy array. This affects:
- `decoder.decode(...).result`
- `decoder.decode_async(...).get().result`
- tuple unpacking of `DecoderResult`
- `decoder.decode_batch(...)[i].result`
- iteration over `BatchDecoderResult`
Existing code that reads, indexes, iterates, or numerically consumes
`result` should generally continue to work. Code that requires `result`
to be an actual Python `list`, mutates it with list APIs, compares it
with list equality, or serializes it directly should migrate to NumPy
semantics or call `.tolist()`.
Also updates `decode_batch(...)` to return `BatchDecoderResult` instead
of `list[DecoderResult]`.
`BatchDecoderResult` exposes vectorized batch fields:
- `result`: 2D NumPy array
- `converged`: 1D NumPy bool array
- `opt_results`: list-like per-shot optional results
Compatibility for existing batch consumers is preserved through
indexing, slicing, `len(...)`, and iteration:
- `batch[i]` materializes a per-shot `DecoderResult`
- `batch[a:b]` returns another `BatchDecoderResult`
- `for r in batch` continues to yield per-shot `DecoderResult` objects
This avoids forcing user code onto the old per-shot Python list
extraction path when the natural batch output is already array-shaped.
Python decoder plugin authors with custom `decode_batch(...)` overrides
must now return `BatchDecoderResult`, not `list[DecoderResult]`.
User-facing Sphinx documentation will be updated in a separate docs PR,
per project guidance that feature docs should not land before release.
## Runtime / performance impact
This change is intended to reduce Python-side result extraction overhead
for batch decoding by allowing users to read batch results directly as
NumPy arrays instead of iterating through a list of `DecoderResult`
objects.
Minibench run on commit `246114b0a33e9a58ff3a96e9abb77ac72e945fd1` using
the C++ `single_error_lut` decoder.
The benchmark uses a CUDA-Q/Stim Steane memory syndrome workload, then
splits the fixed shot set into chunks of `batch_size`. Each timed repeat
loops over all chunks and measures wall-clock time from calling
`decode_batch(...)` through reading the decoded result.
Two current-branch access patterns are compared:
- `numpy_view`: `decoded = decoder.decode_batch(chunk)`, then read
`decoded.result` and `decoded.converged`
- `compat_iteration`: `decoded = decoder.decode_batch(chunk)`, then
iterate `for r in decoded` and read `r.result` and `r.converged` (this
is the backward compatible list access pattern)
Each row reports the median of 5 measured repeats. Speedup is
`compat_iteration median / numpy_view median`.
| shots | batch size | decode_batch calls | result width | NumPy view
median ms | compat iteration median ms | speedup |
| ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| 1000 | 1 | 1000 | 697 | 3.892 | 7.234 | 1.86x |
| 1000 | 32 | 32 | 697 | 1.990 | 4.111 | 2.07x |
| 1000 | 256 | 4 | 697 | 2.399 | 3.936 | 1.64x |
| 3000 | 1 | 3000 | 697 | 11.748 | 21.462 | 1.83x |
| 3000 | 32 | 94 | 697 | 6.040 | 12.162 | 2.01x |
| 3000 | 256 | 12 | 697 | 5.923 | 11.818 | 1.99x |
In this minibench, direct NumPy batch access is about `1.6x-2.1x` faster
than compatibility iteration for the measured `single_error_lut` cases.
---------
Signed-off-by: Melody Ren <melodyr@nvidia.com>1 parent d199363 commit 008a3d2
8 files changed
Lines changed: 601 additions & 80 deletions
File tree
- libs/qec/python
- bindings
- cudaq_qec
- plugins/decoders
- tests
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
| 44 | + | |
43 | 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 | + | |
44 | 81 | | |
45 | 82 | | |
46 | 83 | | |
47 | 84 | | |
48 | 85 | | |
49 | 86 | | |
50 | 87 | | |
| 88 | + | |
51 | 89 | | |
52 | 90 | | |
53 | 91 | | |
| |||
Lines changed: 18 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
416 | 417 | | |
417 | 418 | | |
418 | 419 | | |
419 | | - | |
| 420 | + | |
420 | 421 | | |
421 | 422 | | |
422 | 423 | | |
423 | 424 | | |
424 | 425 | | |
425 | 426 | | |
426 | | - | |
427 | | - | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
428 | 431 | | |
429 | 432 | | |
430 | 433 | | |
| |||
459 | 462 | | |
460 | 463 | | |
461 | 464 | | |
462 | | - | |
| 465 | + | |
463 | 466 | | |
464 | | - | |
465 | | - | |
466 | | - | |
| 467 | + | |
467 | 468 | | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
472 | 479 | | |
473 | 480 | | |
474 | 481 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 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 | + | |
51 | 79 | | |
52 | 80 | | |
53 | 81 | | |
| |||
59 | 87 | | |
60 | 88 | | |
61 | 89 | | |
62 | | - | |
63 | | - | |
| 90 | + | |
| 91 | + | |
64 | 92 | | |
65 | 93 | | |
66 | 94 | | |
| |||
72 | 100 | | |
73 | 101 | | |
74 | 102 | | |
75 | | - | |
76 | | - | |
| 103 | + | |
| 104 | + | |
77 | 105 | | |
78 | 106 | | |
79 | 107 | | |
| |||
85 | 113 | | |
86 | 114 | | |
87 | 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 | + | |
88 | 217 | | |
89 | 218 | | |
90 | 219 | | |
| |||
133 | 262 | | |
134 | 263 | | |
135 | 264 | | |
136 | | - | |
| 265 | + | |
137 | 266 | | |
138 | 267 | | |
139 | 268 | | |
140 | 269 | | |
141 | 270 | | |
142 | 271 | | |
143 | 272 | | |
144 | | - | |
145 | | - | |
| 273 | + | |
| 274 | + | |
146 | 275 | | |
147 | 276 | | |
148 | 277 | | |
| |||
158 | 287 | | |
159 | 288 | | |
160 | 289 | | |
161 | | - | |
162 | | - | |
| 290 | + | |
| 291 | + | |
163 | 292 | | |
164 | 293 | | |
165 | 294 | | |
| |||
187 | 316 | | |
188 | 317 | | |
189 | 318 | | |
190 | | - | |
| 319 | + | |
191 | 320 | | |
192 | 321 | | |
193 | 322 | | |
| |||
338 | 467 | | |
339 | 468 | | |
340 | 469 | | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
341 | 483 | | |
342 | 484 | | |
343 | 485 | | |
| |||
353 | 495 | | |
354 | 496 | | |
355 | 497 | | |
356 | | - | |
357 | | - | |
| 498 | + | |
| 499 | + | |
358 | 500 | | |
359 | 501 | | |
360 | 502 | | |
| |||
0 commit comments