rstim 0.4.0

Stim-aligned stabilizer circuit simulator and detector error model toolkit
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Borrow compact packet events only as scalar counts replay visits them.
use super::*;

pub(super) trait RowDraw {
    fn draw(&mut self, kind: RandomKind) -> u64;
    fn noise_value(&self, _event: usize, _ordinal: usize) -> u64 {
        panic!("scheduled noise requires a complete recorded row")
    }
    fn discard_remaining(&mut self, kinds: &[RandomKind]);
    fn skip_zero_noise(&mut self, _maximum: usize) -> usize {
        0 // Live consumers retain their original draw path.
    }
}

impl<R: Rng> RowDraw for RowRandom<'_, R> {
    fn noise_value(&self, event: usize, _ordinal: usize) -> u64 {
        self.tape.expect("scheduled noise requires a recorded row")[event]
    }
    #[inline]
    fn draw(&mut self, kind: RandomKind) -> u64 {
        RowRandom::draw(self, kind)
    }
    fn discard_remaining(&mut self, kinds: &[RandomKind]) {
        RowRandom::discard_remaining(self, kinds);
    }
    #[inline]
    fn skip_zero_noise(&mut self, maximum: usize) -> usize {
        let Some(tape) = self.tape else {
            return 0;
        };
        let end = self
            .cursor
            .checked_add(maximum)
            .expect("random tape cursor overflow");
        let values = tape
            .get(self.cursor..end)
            .expect("recorded random tape is incomplete");
        let skipped = values
            .iter()
            .position(|&value| value != 0)
            .unwrap_or(maximum);
        self.cursor += skipped;
        skipped
    }
}

// All caller RNG events were already drawn when the packet was produced.
// Active values remain in the tape; compact Noise and Independent cells may
// contain retained values from an unrelated prior packet and must not be read.
pub(super) struct CompactReplay<'a> {
    tape: &'a [u64],
    noise: Option<&'a NoisePacket>,
    independent: Option<&'a IndependentPacket>,
    lane: usize,
    cursor: usize,
    noise_event: usize,
    independent_event: usize,
}
impl<'a> CompactReplay<'a> {
    pub(super) fn new(
        tape: &'a [u64],
        noise: Option<&'a NoisePacket>,
        independent: Option<&'a IndependentPacket>,
        lane: usize,
    ) -> Self {
        debug_assert!(lane < 64);
        Self {
            tape,
            noise,
            independent,
            lane,
            cursor: 0,
            noise_event: 0,
            independent_event: 0,
        }
    }
    pub(super) fn cursor(&self) -> usize {
        self.cursor
    }
}
impl RowDraw for CompactReplay<'_> {
    fn noise_value(&self, event: usize, ordinal: usize) -> u64 {
        self.noise
            .map_or_else(|| self.tape[event], |p| p.value(ordinal, self.lane))
    }
    fn skip_zero_noise(&mut self, maximum: usize) -> usize {
        // The prepared span contains only Noise nodes, each with one event.
        // Compact cells in the full tape may be stale: consult the sidecar
        // when present, then advance both cursors only past literal zeroes.
        let end = self
            .cursor
            .checked_add(maximum)
            .expect("random tape cursor overflow");
        let values = self
            .tape
            .get(self.cursor..end)
            .expect("recorded random tape is incomplete");
        let skipped = if let Some(noise) = self.noise {
            noise.zero_prefix(self.noise_event, maximum, self.lane)
        } else {
            values
                .iter()
                .position(|&value| value != 0)
                .unwrap_or(maximum)
        };
        self.cursor += skipped;
        if self.noise.is_some() {
            self.noise_event += skipped;
        }
        skipped
    }

    #[inline]
    fn draw(&mut self, kind: RandomKind) -> u64 {
        assert!(
            self.cursor < self.tape.len(),
            "recorded random tape is incomplete"
        );
        let value = match kind {
            RandomKind::Noise { .. } if self.noise.is_some() => {
                let value = self.noise.unwrap().value(self.noise_event, self.lane);
                self.noise_event += 1;
                value
            }
            RandomKind::Independent if self.independent.is_some() => {
                let value =
                    (self.independent.unwrap().mask(self.independent_event) >> self.lane) & 1;
                self.independent_event += 1;
                value
            }
            _ => self.tape[self.cursor],
        };
        self.cursor += 1;
        value
    }
    fn discard_remaining(&mut self, kinds: &[RandomKind]) {
        let end = self
            .cursor
            .checked_add(kinds.len())
            .expect("random tape cursor overflow");
        assert!(end <= self.tape.len(), "recorded random tape is incomplete");
        self.cursor = end;
        // This retires the row. No subsequent event is read from its sidecars.
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{RngCore, SeedableRng, rngs::StdRng};

    #[test]
    fn zero_noise_spans_stop_at_hits_without_reading_stale_optional_cells() {
        let noise_kind = RandomKind::Noise {
            probability: 0.001,
            choices: 15,
        };
        let mut kinds = vec![noise_kind; 10];
        kinds.extend([RandomKind::Independent, RandomKind::Active]);
        kinds.extend([noise_kind; 4]);
        kinds.push(RandomKind::Independent);
        kinds.extend(std::iter::repeat_n(RandomKind::Independent, 127));
        let mut expected = vec![0, 0, 0, 15, 0, 0, 0, 0, 8, 0, 1, 0xabcdef, 0, 0, 1, 0, 0];
        expected.extend((0..127).map(|i| (i % 2) as u64));
        for lane in [0, 1, 31, 32, 63] {
            let mut noise = NoisePacket::new(&kinds, 14, usize::MAX).unwrap();
            let mask = 1u64 << lane;
            let mut n = 0;
            for (&kind, &value) in kinds.iter().zip(&expected) {
                if matches!(kind, RandomKind::Noise { .. }) {
                    for bit in 0..4 {
                        noise.planes()[n][bit] = !mask | (((value >> bit) & 1) << lane);
                    }
                    n += 1;
                }
            }
            let mut independent = IndependentPacket::new(129, usize::MAX).unwrap();
            let words = independent.row(lane);
            let mut i = 0;
            for (&kind, &value) in kinds.iter().zip(&expected) {
                if matches!(kind, RandomKind::Independent) {
                    words[i / 64] |= value << (i % 64);
                    i += 1;
                }
            }
            independent.transpose(64);
            for use_noise in [false, true] {
                for use_independent in [false, true] {
                    let tape: Vec<_> = kinds
                        .iter()
                        .zip(&expected)
                        .map(|(&kind, &value)| {
                            if (use_noise && matches!(kind, RandomKind::Noise { .. }))
                                || (use_independent && matches!(kind, RandomKind::Independent))
                            {
                                u64::MAX
                            } else {
                                value
                            }
                        })
                        .collect();
                    let mut replay = CompactReplay::new(
                        &tape,
                        use_noise.then_some(&noise),
                        use_independent.then_some(&independent),
                        lane,
                    );
                    assert_eq!(replay.skip_zero_noise(0), 0);
                    assert_eq!(replay.skip_zero_noise(3), 3);
                    assert_eq!(replay.draw(noise_kind), 15);
                    assert_eq!(replay.skip_zero_noise(6), 4);
                    assert_eq!(replay.draw(noise_kind), 8);
                    assert_eq!(replay.skip_zero_noise(1), 1);
                    assert_eq!(replay.draw(RandomKind::Independent), 1);
                    assert_eq!(replay.draw(RandomKind::Active), 0xabcdef);
                    assert_eq!(replay.skip_zero_noise(4), 2);
                    assert_eq!(replay.draw(noise_kind), 1);
                    assert_eq!(replay.skip_zero_noise(1), 1);
                    assert_eq!(replay.draw(RandomKind::Independent), 0);
                    for &value in &expected[17..] {
                        assert_eq!(replay.draw(RandomKind::Independent), value);
                    }
                    assert_eq!(replay.cursor(), expected.len());
                    assert_eq!(replay.noise_event, if use_noise { 14 } else { 0 });
                    assert_eq!(
                        replay.independent_event,
                        if use_independent { 129 } else { 0 }
                    );
                }
            }
        }
    }

    #[test]
    fn prepared_compact_counts_skip_zero_noise_with_literal_records_and_continuation() {
        struct Observed<'a> {
            row: CompactReplay<'a>,
            skipped: usize,
        }
        impl RowDraw for Observed<'_> {
            fn draw(&mut self, kind: RandomKind) -> u64 {
                self.row.draw(kind)
            }
            fn discard_remaining(&mut self, kinds: &[RandomKind]) {
                self.row.discard_remaining(kinds);
            }
            fn skip_zero_noise(&mut self, maximum: usize) -> usize {
                let n = self.row.skip_zero_noise(maximum);
                self.skipped += n;
                n
            }
        }
        let text = "H 3\nM 3\nH 0 1 2\nT 0 1 2\nREPEAT 40 {\nDEPOLARIZE1(0) 0 1 2\nX_ERROR(0) 1\n}\nDEPOLARIZE1(1) 3\nMY(1) 0\nCX rec[-1] 2\nCX sweep[1] 1\nT_DAG 2\nMPP X1*Y2\nM 0 1 2\nDETECTOR rec[-1] rec[-2]\nOBSERVABLE_INCLUDE(7) rec[-3]\n";
        for policy in [
            CompiledRotationArithmetic::Strict,
            CompiledRotationArithmetic::Fused,
        ] {
            let plan =
                CompiledNearCliffordExecutor::compile_text_with_arithmetic(text, policy).unwrap();
            assert!(plan.noise_spans.is_some());
            for seed in [739, 1739, 9173, 1667] {
                let mut caller = StdRng::seed_from_u64(seed);
                let tape: Vec<_> = {
                    let mut live = RowRandom::live(&mut caller);
                    plan.random_kinds
                        .iter()
                        .map(|&kind| live.draw(kind))
                        .collect()
                };
                let mut untouched = caller.clone();
                let mut noise =
                    NoisePacket::new(&plan.random_kinds, plan.noise_event_count, usize::MAX)
                        .unwrap();
                let mut n = 0;
                for (&kind, &value) in plan.random_kinds.iter().zip(&tape) {
                    if matches!(kind, RandomKind::Noise { .. }) {
                        for bit in 0..4 {
                            noise.planes()[n][bit] = ((value >> bit) & 1) << 63;
                        }
                        n += 1;
                    }
                }
                for sidecar in [None, Some(&noise)] {
                    let stale: Vec<_> = plan
                        .random_kinds
                        .iter()
                        .zip(&tape)
                        .map(|(&kind, &value)| {
                            if sidecar.is_some() && matches!(kind, RandomKind::Noise { .. }) {
                                u64::MAX
                            } else {
                                value
                            }
                        })
                        .collect();
                    let mut replay = Observed {
                        row: CompactReplay::new(&stale, sidecar, None, 63),
                        skipped: 0,
                    };
                    let selected = plan
                        .prepare_sampler()
                        .unwrap()
                        .row_with_random_kernel::<true, true>(&[true, false], &mut replay)
                        .unwrap();
                    assert!(
                        replay.skipped >= 160,
                        "must actually retire eligible compact zero Noise nodes"
                    );
                    assert_eq!(replay.row.cursor(), tape.len());
                    let expected = plan
                        .prepare_sampler()
                        .unwrap()
                        .row_with_random_mode::<false>(
                            &[true, false],
                            &mut RowRandom::recorded(&tape, &mut caller),
                        )
                        .unwrap();
                    if selected.detectors.iter().all(|&bit| !bit) {
                        assert_eq!(selected, expected);
                    } else {
                        assert!(expected.detectors.iter().any(|&bit| bit));
                    }
                }
                for _ in 0..16 {
                    assert_eq!(caller.next_u64(), untouched.next_u64());
                }
            }
            let initial = plan
                .prepare_sampler()
                .unwrap()
                .coefficient_cache_reserved_bytes();
            let mut original = plan.clone();
            original.random_runs = None;
            original.noise_spans = None;
            for extra in [0, 288] {
                let mut native = plan
                    .prepare_sampler_with_cache_budget(initial + extra)
                    .unwrap();
                let mut reference = original.prepare_sampler_with_cache_budget(0).unwrap();
                let mut a = StdRng::seed_from_u64(1739);
                let mut b = a.clone();
                for shots in [64, 65, 129] {
                    let rows = reference
                        .sample_with_sweep(shots, &[true, false], &mut a)
                        .unwrap();
                    let accepted: Vec<_> = rows
                        .iter()
                        .filter(|row| row.detectors.iter().all(|&bit| !bit))
                        .collect();
                    let logical_errors = accepted
                        .iter()
                        .filter(|row| {
                            row.observables
                                .iter()
                                .filter(|(index, _)| *index == 7)
                                .fold(false, |parity, (_, bit)| parity ^ bit)
                        })
                        .count();
                    let counts = native
                        .sample_postselected_counts_with_sweep(shots, 7, &[true, false], &mut b)
                        .unwrap();
                    assert_eq!(
                        counts,
                        NearCliffordPostselectedCounts {
                            attempted: shots,
                            accepted: accepted.len(),
                            logical_errors
                        }
                    );
                    assert_eq!(
                        native.last_packet_live, 0,
                        "exercise admission-failure replay"
                    );
                    for _ in 0..16 {
                        assert_eq!(a.next_u64(), b.next_u64());
                    }
                }
            }
        }
    }

    #[test]
    fn each_optional_sidecar_replays_literal_typed_events_without_reading_stale_cells() {
        let mut kinds = vec![RandomKind::Active];
        kinds.extend(std::iter::repeat_n(RandomKind::Independent, 131));
        for probability in [0., 0.001, 0.01, 0.37, 1.] {
            for choices in [1, 3, 15] {
                kinds.push(RandomKind::Noise {
                    probability,
                    choices,
                });
            }
            kinds.push(RandomKind::Active);
        }
        for seed in [173, 917] {
            let mut rng = StdRng::seed_from_u64(seed);
            let expected: Vec<_> = {
                let mut live = RowRandom::live(&mut rng);
                kinds.iter().map(|&kind| live.draw(kind)).collect()
            };
            let mut untouched = rng.clone();
            let mut noise = NoisePacket::new(&kinds, 15, PACKET_BYTE_BUDGET).unwrap();
            let mut independent = IndependentPacket::new(131, PACKET_BYTE_BUDGET).unwrap();
            let (mut n, mut i) = (0, 0);
            for (&kind, &value) in kinds.iter().zip(&expected) {
                match kind {
                    RandomKind::Noise { .. } => {
                        for bit in 0..4 {
                            noise.planes()[n][bit] = ((value >> bit) & 1) << 63;
                        }
                        n += 1;
                    }
                    RandomKind::Independent => {
                        i += 1;
                    }
                    _ => {}
                }
            }
            assert_eq!(i, 131);
            let words = independent.row(63);
            i = 0;
            for (&kind, &value) in kinds.iter().zip(&expected) {
                if matches!(kind, RandomKind::Independent) {
                    words[i / 64] |= value << (i % 64);
                    i += 1;
                }
            }
            independent.transpose(64);
            for use_noise in [false, true] {
                for use_independent in [false, true] {
                    let tape: Vec<_> = kinds
                        .iter()
                        .zip(&expected)
                        .map(|(&kind, &value)| {
                            if (use_noise && matches!(kind, RandomKind::Noise { .. }))
                                || (use_independent && matches!(kind, RandomKind::Independent))
                            {
                                u64::MAX
                            } else {
                                value
                            }
                        })
                        .collect();
                    let mut replay = CompactReplay::new(
                        &tape,
                        use_noise.then_some(&noise),
                        use_independent.then_some(&independent),
                        63,
                    );
                    for (&kind, &value) in kinds.iter().zip(&expected) {
                        assert_eq!(
                            replay.draw(kind),
                            value,
                            "seed={seed} noise={use_noise} independent={use_independent}"
                        );
                    }
                    assert_eq!(replay.cursor(), kinds.len());
                }
            }
            for _ in 0..16 {
                assert_eq!(rng.next_u64(), untouched.next_u64());
            }
        }
    }
}