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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
use rand::Rng;

use crate::ir::{StimInstr, StimTarget};
use crate::rare_error_iterator::RareErrorIndexSampler;
use crate::sim::bit_table::BitTable;

/// A compiled, allocation-light execution plan for the common loss-aware sampling subset.
///
/// Unsupported instructions cause compilation to return `None`, allowing callers to preserve
/// the general executor as a correctness fallback.
#[derive(Debug)]
pub(crate) struct LossSamplerPlan {
    num_simulated_qubits: usize,
    num_measurements: usize,
    ops: Vec<LossOp>,
}

#[derive(Debug)]
enum LossOp {
    H(Vec<usize>),
    Cx(Vec<(usize, usize)>),
    X(Vec<usize>),
    Y(Vec<usize>),
    Z(Vec<usize>),
    ResetZ(Vec<usize>),
    MeasureZ(Vec<(usize, bool)>),
    MeasureResetZ(Vec<(usize, bool)>),
    XError {
        probability: f64,
        qubits: Vec<usize>,
    },
    YError {
        probability: f64,
        qubits: Vec<usize>,
    },
    ZError {
        probability: f64,
        qubits: Vec<usize>,
    },
    Depolarize1 {
        probability: f64,
        qubits: Vec<usize>,
    },
    Depolarize2 {
        probability: f64,
        pairs: Vec<(usize, usize)>,
    },
    Loss {
        probability: f64,
        qubits: Vec<usize>,
    },
    Repeat {
        count: u64,
        body: Vec<LossOp>,
    },
}

impl LossSamplerPlan {
    pub(crate) fn try_compile(instrs: &[StimInstr]) -> Option<Self> {
        let (mut ops, has_loss) = compile_ops(instrs)?;
        if !has_loss {
            return None;
        }
        let num_simulated_qubits = compact_qubit_indices(&mut ops);
        Some(Self {
            num_simulated_qubits,
            num_measurements: crate::stats::num_measurements(instrs),
            ops,
        })
    }

    pub(crate) fn num_measurements(&self) -> usize {
        self.num_measurements
    }

    pub(crate) fn run_batch<R: Rng + ?Sized>(
        &self,
        num_shots: usize,
        reference_sample: &[bool],
        rng: &mut R,
    ) -> Result<BitTable, String> {
        if reference_sample.len() != self.num_measurements {
            return Err(format!(
                "loss frame expected {} reference measurements, got {}",
                self.num_measurements,
                reference_sample.len()
            ));
        }

        let mut batch = LossFrameBatch::try_new(
            self.num_simulated_qubits,
            self.num_measurements,
            num_shots,
            rng,
        )?;
        batch.execute(&self.ops, rng)?;
        if batch.measurement_index != self.num_measurements {
            return Err(format!(
                "loss frame produced {} measurements, expected {}",
                batch.measurement_index, self.num_measurements
            ));
        }
        Ok(batch.measurements)
    }
}

/// A CSS-frame batch with one loss bit per qubit and shot.
///
/// The complete randomized CSS frame includes ideal Paulis, so every shot may follow its own
/// Clifford path when loss skips gates. A lost target is frozen until reset, paired gates are
/// masked whenever either endpoint is lost, and lost measurements are forced to one. Keeping shots
/// in the minor dimension makes every layer operate on 64 shots per machine word.
struct LossFrameBatch {
    batch_size: usize,
    x_table: BitTable,
    z_table: BitTable,
    lost_table: BitTable,
    measurements: BitTable,
    measurement_index: usize,
}

impl LossFrameBatch {
    fn try_new<R: Rng + ?Sized>(
        num_qubits: usize,
        num_measurements: usize,
        batch_size: usize,
        rng: &mut R,
    ) -> Result<Self, String> {
        let alloc = |rows| {
            BitTable::try_new(rows, batch_size)
                .map_err(|error| format!("loss frame allocation failed: {error:?}"))
        };
        let x_table = alloc(num_qubits)?;
        let mut z_table = alloc(num_qubits)?;
        for q in 0..num_qubits {
            z_table.randomize_row(q, rng);
        }
        Ok(Self {
            batch_size,
            x_table,
            z_table,
            lost_table: alloc(num_qubits)?,
            measurements: alloc(num_measurements)?,
            measurement_index: 0,
        })
    }

    fn execute<R: Rng + ?Sized>(&mut self, ops: &[LossOp], rng: &mut R) -> Result<(), String> {
        for op in ops {
            match op {
                LossOp::H(qubits) => {
                    for &q in qubits {
                        self.h(q);
                    }
                }
                LossOp::Cx(pairs) => {
                    for &(control, target) in pairs {
                        self.cx(control, target);
                    }
                }
                LossOp::X(qubits) => self.ideal_pauli(qubits, 1),
                LossOp::Y(qubits) => self.ideal_pauli(qubits, 2),
                LossOp::Z(qubits) => self.ideal_pauli(qubits, 3),
                LossOp::ResetZ(qubits) => {
                    for &q in qubits {
                        self.reset_z(q, rng);
                    }
                }
                LossOp::MeasureZ(targets) => {
                    for &(q, inverted) in targets {
                        self.measure_z(q, inverted, false, rng);
                    }
                }
                LossOp::MeasureResetZ(targets) => {
                    for &(q, inverted) in targets {
                        self.measure_z(q, inverted, true, rng);
                    }
                }
                LossOp::XError {
                    probability,
                    qubits,
                } => self.independent_pauli(*probability, qubits, 1, rng)?,
                LossOp::YError {
                    probability,
                    qubits,
                } => self.independent_pauli(*probability, qubits, 2, rng)?,
                LossOp::ZError {
                    probability,
                    qubits,
                } => self.independent_pauli(*probability, qubits, 3, rng)?,
                LossOp::Depolarize1 {
                    probability,
                    qubits,
                } => self.depolarize1(*probability, qubits, rng)?,
                LossOp::Depolarize2 { probability, pairs } => {
                    self.depolarize2(*probability, pairs, rng)?
                }
                LossOp::Loss {
                    probability,
                    qubits,
                } => self.loss(*probability, qubits, rng)?,
                LossOp::Repeat { count, body } => {
                    for _ in 0..*count {
                        self.execute(body, rng)?;
                    }
                }
            }
        }
        Ok(())
    }

    fn h(&mut self, q: usize) {
        let words = self.x_table.words_per_row();
        for word in 0..words {
            let x = self.x_table.row_words(q)[word];
            let z = self.z_table.row_words(q)[word];
            let lost = self.lost_table.row_words(q)[word];
            let delta = (x ^ z) & !lost;
            self.x_table.row_words_mut(q)[word] ^= delta;
            self.z_table.row_words_mut(q)[word] ^= delta;
        }
    }

    fn cx(&mut self, control: usize, target: usize) {
        let words = self.x_table.words_per_row();
        for word in 0..words {
            let present = !(self.lost_table.row_words(control)[word]
                | self.lost_table.row_words(target)[word]);
            let control_x = self.x_table.row_words(control)[word];
            let target_z = self.z_table.row_words(target)[word];
            self.x_table.row_words_mut(target)[word] ^= control_x & present;
            self.z_table.row_words_mut(control)[word] ^= target_z & present;
        }
    }

    fn reset_z<R: Rng + ?Sized>(&mut self, q: usize, rng: &mut R) {
        self.x_table.clear_row(q);
        self.z_table.randomize_row(q, rng);
        self.lost_table.clear_row(q);
    }

    fn measure_z<R: Rng + ?Sized>(&mut self, q: usize, inverted: bool, reset: bool, rng: &mut R) {
        let words = self.x_table.words_per_row();
        for word in 0..words {
            let lost = self.lost_table.row_words(q)[word];
            let present_value = self.x_table.row_words(q)[word];
            self.measurements.row_words_mut(self.measurement_index)[word] = if inverted {
                !present_value & !lost
            } else {
                present_value | lost
            };
        }
        self.measurement_index += 1;

        if reset {
            self.reset_z(q, rng);
        } else {
            for word in 0..words {
                let lost = self.lost_table.row_words(q)[word];
                let old_z = self.z_table.row_words(q)[word];
                self.z_table.row_words_mut(q)[word] = (old_z & lost) | (rng.r#gen::<u64>() & !lost);
            }
        }
    }

    fn independent_pauli<R: Rng + ?Sized>(
        &mut self,
        probability: f64,
        qubits: &[usize],
        pauli: u8,
        rng: &mut R,
    ) -> Result<(), String> {
        let attempts = self.attempt_count(qubits.len())?;
        let mut events = RareErrorIndexSampler::new(probability, attempts);
        while let Some(index) = events.next_index(rng) {
            let q = qubits[index / self.batch_size];
            let shot = index % self.batch_size;
            if !self.lost_table.get(q, shot) {
                self.apply_pauli(q, shot, pauli);
            }
        }
        Ok(())
    }

    fn depolarize1<R: Rng + ?Sized>(
        &mut self,
        probability: f64,
        qubits: &[usize],
        rng: &mut R,
    ) -> Result<(), String> {
        let attempts = self.attempt_count(qubits.len())?;
        let mut events = RareErrorIndexSampler::new(probability, attempts);
        while let Some(index) = events.next_index(rng) {
            let q = qubits[index / self.batch_size];
            let shot = index % self.batch_size;
            if !self.lost_table.get(q, shot) {
                self.apply_pauli(q, shot, rng.gen_range(1..=3));
            }
        }
        Ok(())
    }

    fn depolarize2<R: Rng + ?Sized>(
        &mut self,
        probability: f64,
        pairs: &[(usize, usize)],
        rng: &mut R,
    ) -> Result<(), String> {
        let attempts = self.attempt_count(pairs.len())?;
        let mut events = RareErrorIndexSampler::new(probability, attempts);
        while let Some(index) = events.next_index(rng) {
            let (a, b) = pairs[index / self.batch_size];
            let shot = index % self.batch_size;
            if self.lost_table.get(a, shot) || self.lost_table.get(b, shot) {
                continue;
            }
            let (pa, pb) = two_qubit_pauli(rng.gen_range(0..15));
            self.apply_pauli(a, shot, pa);
            self.apply_pauli(b, shot, pb);
        }
        Ok(())
    }

    fn loss<R: Rng + ?Sized>(
        &mut self,
        probability: f64,
        qubits: &[usize],
        rng: &mut R,
    ) -> Result<(), String> {
        let attempts = self.attempt_count(qubits.len())?;
        let mut events = RareErrorIndexSampler::new(probability, attempts);
        while let Some(index) = events.next_index(rng) {
            let q = qubits[index / self.batch_size];
            let shot = index % self.batch_size;
            self.lost_table.set(q, shot, true);
        }
        Ok(())
    }

    fn apply_pauli(&mut self, q: usize, shot: usize, pauli: u8) {
        if matches!(pauli, 1 | 2) {
            self.x_table.toggle(q, shot);
        }
        if matches!(pauli, 2 | 3) {
            self.z_table.toggle(q, shot);
        }
    }

    fn attempt_count(&self, target_count: usize) -> Result<usize, String> {
        target_count
            .checked_mul(self.batch_size)
            .ok_or_else(|| "loss frame noise attempt count overflow".to_string())
    }

    fn ideal_pauli(&mut self, qubits: &[usize], pauli: u8) {
        for &q in qubits {
            for word in 0..self.x_table.words_per_row() {
                let present = !self.lost_table.row_words(q)[word];
                if matches!(pauli, 1 | 2) {
                    self.x_table.row_words_mut(q)[word] ^= present;
                }
                if matches!(pauli, 2 | 3) {
                    self.z_table.row_words_mut(q)[word] ^= present;
                }
            }
        }
    }
}

fn compile_ops(instrs: &[StimInstr]) -> Option<(Vec<LossOp>, bool)> {
    let mut ops = Vec::new();
    let mut has_loss = false;
    for instr in instrs {
        match instr {
            StimInstr::Repeat { count, body } => {
                let (body, body_has_loss) = compile_ops(body)?;
                has_loss |= body_has_loss;
                ops.push(LossOp::Repeat {
                    count: *count,
                    body,
                });
            }
            StimInstr::Op {
                name,
                args,
                targets,
                ..
            } => {
                let probability = || args.first().copied().unwrap_or(0.0);
                let op = match name.as_str() {
                    "H" => Some(LossOp::H(plain_qubits(targets)?)),
                    "CX" | "CNOT" | "ZCX" => Some(LossOp::Cx(plain_pairs(targets)?)),
                    "X" => Some(LossOp::X(plain_qubits(targets)?)),
                    "Y" => Some(LossOp::Y(plain_qubits(targets)?)),
                    "Z" => Some(LossOp::Z(plain_qubits(targets)?)),
                    "R" | "RZ" => Some(LossOp::ResetZ(plain_qubits(targets)?)),
                    "M" | "MZ" if args.is_empty() => {
                        Some(LossOp::MeasureZ(measurement_targets(targets)?))
                    }
                    "MR" | "MRZ" if args.is_empty() => {
                        Some(LossOp::MeasureResetZ(measurement_targets(targets)?))
                    }
                    "X_ERROR" => Some(LossOp::XError {
                        probability: probability(),
                        qubits: plain_qubits(targets)?,
                    }),
                    "Y_ERROR" => Some(LossOp::YError {
                        probability: probability(),
                        qubits: plain_qubits(targets)?,
                    }),
                    "Z_ERROR" => Some(LossOp::ZError {
                        probability: probability(),
                        qubits: plain_qubits(targets)?,
                    }),
                    "DEPOLARIZE1" => Some(LossOp::Depolarize1 {
                        probability: probability(),
                        qubits: plain_qubits(targets)?,
                    }),
                    "DEPOLARIZE2" => Some(LossOp::Depolarize2 {
                        probability: probability(),
                        pairs: plain_pairs(targets)?,
                    }),
                    "LOSS" => {
                        has_loss = true;
                        Some(LossOp::Loss {
                            probability: probability(),
                            qubits: plain_qubits(targets)?,
                        })
                    }
                    "QUBIT_COORDS" | "SHIFT_COORDS" | "TICK" | "DETECTOR"
                    | "OBSERVABLE_INCLUDE" => None,
                    _ => return None,
                };
                if let Some(op) = op {
                    ops.push(op);
                }
            }
        }
    }
    Some((ops, has_loss))
}

/// Remaps only operational qubits onto a dense internal range.
///
/// Stim qubit identifiers are labels and need not be contiguous. Stabilizer tableaux scale with
/// the largest allocated dimension, so preserving holes would charge every shot for qubits that
/// never participate in an operation. Measurement order is stored separately and is unchanged.
fn compact_qubit_indices(ops: &mut [LossOp]) -> usize {
    let mut used_qubits = Vec::new();
    collect_qubit_indices(ops, &mut used_qubits);
    used_qubits.sort_unstable();
    used_qubits.dedup();
    let mut measure_reset_qubits = Vec::new();
    collect_measure_reset_qubits(ops, &mut measure_reset_qubits);
    measure_reset_qubits.sort_unstable();
    measure_reset_qubits.dedup();
    used_qubits.sort_by_key(|q| (measure_reset_qubits.binary_search(q).is_ok(), *q));
    let mut physical_to_dense: Vec<(usize, usize)> = used_qubits
        .iter()
        .enumerate()
        .map(|(dense, &physical)| (physical, dense))
        .collect();
    physical_to_dense.sort_unstable_by_key(|&(physical, _)| physical);
    remap_qubit_indices(ops, &physical_to_dense);
    used_qubits.len()
}

fn collect_measure_reset_qubits(ops: &[LossOp], qubits: &mut Vec<usize>) {
    for op in ops {
        match op {
            LossOp::MeasureResetZ(targets) => {
                qubits.extend(targets.iter().map(|&(q, _)| q));
            }
            LossOp::Repeat { body, .. } => collect_measure_reset_qubits(body, qubits),
            _ => {}
        }
    }
}

fn collect_qubit_indices(ops: &[LossOp], used_qubits: &mut Vec<usize>) {
    for op in ops {
        match op {
            LossOp::H(qubits)
            | LossOp::X(qubits)
            | LossOp::Y(qubits)
            | LossOp::Z(qubits)
            | LossOp::ResetZ(qubits) => used_qubits.extend(qubits),
            LossOp::XError { qubits, .. }
            | LossOp::YError { qubits, .. }
            | LossOp::ZError { qubits, .. }
            | LossOp::Depolarize1 { qubits, .. }
            | LossOp::Loss { qubits, .. } => used_qubits.extend(qubits),
            LossOp::Cx(pairs) | LossOp::Depolarize2 { pairs, .. } => {
                used_qubits.extend(pairs.iter().flat_map(|&(a, b)| [a, b]));
            }
            LossOp::MeasureZ(targets) | LossOp::MeasureResetZ(targets) => {
                used_qubits.extend(targets.iter().map(|&(q, _)| q));
            }
            LossOp::Repeat { body, .. } => collect_qubit_indices(body, used_qubits),
        }
    }
}

fn remap_qubit_indices(ops: &mut [LossOp], physical_to_dense: &[(usize, usize)]) {
    let remap = |q: &mut usize| {
        let index = physical_to_dense
            .binary_search_by_key(q, |&(physical, _)| physical)
            .expect("compiled operation qubit was collected");
        *q = physical_to_dense[index].1;
    };
    for op in ops {
        match op {
            LossOp::H(qubits)
            | LossOp::X(qubits)
            | LossOp::Y(qubits)
            | LossOp::Z(qubits)
            | LossOp::ResetZ(qubits) => qubits.iter_mut().for_each(&remap),
            LossOp::XError { qubits, .. }
            | LossOp::YError { qubits, .. }
            | LossOp::ZError { qubits, .. }
            | LossOp::Depolarize1 { qubits, .. }
            | LossOp::Loss { qubits, .. } => qubits.iter_mut().for_each(&remap),
            LossOp::Cx(pairs) | LossOp::Depolarize2 { pairs, .. } => {
                for (a, b) in pairs {
                    remap(a);
                    remap(b);
                }
            }
            LossOp::MeasureZ(targets) | LossOp::MeasureResetZ(targets) => {
                for (q, _) in targets {
                    remap(q);
                }
            }
            LossOp::Repeat { body, .. } => remap_qubit_indices(body, physical_to_dense),
        }
    }
}

fn plain_qubits(targets: &[StimTarget]) -> Option<Vec<usize>> {
    targets
        .iter()
        .map(|target| match target {
            StimTarget::Qubit(q) => Some(*q as usize),
            _ => None,
        })
        .collect()
}

fn measurement_targets(targets: &[StimTarget]) -> Option<Vec<(usize, bool)>> {
    targets
        .iter()
        .map(|target| match target {
            StimTarget::Qubit(q) => Some((*q as usize, false)),
            StimTarget::QubitInv(q) => Some((*q as usize, true)),
            _ => None,
        })
        .collect()
}

fn plain_pairs(targets: &[StimTarget]) -> Option<Vec<(usize, usize)>> {
    let qubits = plain_qubits(targets)?;
    if qubits.len() % 2 != 0 {
        return None;
    }
    Some(
        qubits
            .chunks_exact(2)
            .map(|pair| (pair[0], pair[1]))
            .collect(),
    )
}

fn two_qubit_pauli(index: usize) -> (u8, u8) {
    let encoded = index + 1;
    ((encoded / 4) as u8, (encoded % 4) as u8)
}

#[cfg(test)]
mod tests {
    use rand::rngs::StdRng;
    use rand::SeedableRng;

    use super::LossSamplerPlan;
    use crate::executor::Executor;
    use crate::parser::parse_lines;

    #[test]
    fn packed_loss_plan_matches_legacy_executor_for_deterministic_loss_semantics() {
        let circuit = parse_lines(
            "R 0 1 2\n\
             X 0\n\
             LOSS(1) 0\n\
             CX 0 2\n\
             M 0 1 2\n\
             MR 0\n\
             M 0\n",
        )
        .unwrap();
        let plan = LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
        let reference = crate::data_path::build_reference_sample(
            &circuit,
            crate::data_path::ReferenceSampleMode::SimulateNoiseless,
        )
        .unwrap();

        for seed in 0..64 {
            let mut packed_rng = StdRng::seed_from_u64(seed);
            let mut legacy_rng = StdRng::seed_from_u64(seed);
            let batch = plan.run_batch(1, &reference, &mut packed_rng).unwrap();
            let packed: Vec<_> = (0..plan.num_measurements)
                .map(|measurement| batch.get(measurement, 0))
                .collect();
            let legacy = Executor::from_instrs(circuit.clone())
                .unwrap()
                .run(&mut legacy_rng)
                .unwrap()
                .measurements;
            assert_eq!(packed, legacy, "seed {seed}");
        }
    }

    #[test]
    fn unsupported_loss_circuit_declines_fast_plan() {
        let circuit = parse_lines("LOSS(0.1) 0\nMX 0\n").unwrap();
        assert!(LossSamplerPlan::try_compile(&circuit).is_none());
    }

    #[test]
    fn loss_before_later_cx_compiles_to_the_css_frame() {
        let circuit = parse_lines("X 0\nLOSS(0.5) 0\nCX 0 2\nM 0 1 2 3\n").unwrap();
        LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
    }

    #[test]
    fn loss_at_end_of_repeated_body_compiles_with_next_iteration_cx() {
        let circuit = parse_lines("REPEAT 2 {\nCX 0 1\nLOSS(0.5) 0\n}\nM 0 1\n").unwrap();
        LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
    }

    #[test]
    fn unrelated_cx_after_loss_compiles_to_the_css_frame() {
        let circuit = parse_lines("LOSS(0.5) 0\nCX 1 2\nM 0 1 2\n").unwrap();
        LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
    }

    #[test]
    fn reset_between_loss_and_later_cx_compiles_to_the_css_frame() {
        let circuit = parse_lines("LOSS(0.5) 0\nR 0\nCX 0 1\nM 0 1\n").unwrap();
        LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
    }

    #[test]
    fn repeat_reset_loss_compiles_to_the_css_frame() {
        let circuit = parse_lines("REPEAT 3 {\nCX 0 1\nLOSS(0.5) 0\nMR 0\n}\nM 0 1\n").unwrap();
        LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
    }

    #[test]
    fn bit_parallel_loss_batch_is_repeatable_for_a_fixed_seed() {
        let circuit =
            parse_lines("R 0 1\nH 0\nCX 0 1\nDEPOLARIZE2(0.2) 0 1\nLOSS(0.3) 0 1\nM 0 1\n")
                .unwrap();
        let plan = LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
        let reference = crate::data_path::build_reference_sample(
            &circuit,
            crate::data_path::ReferenceSampleMode::SimulateNoiseless,
        )
        .unwrap();
        let mut first_rng = StdRng::seed_from_u64(0x5eed);
        let mut second_rng = StdRng::seed_from_u64(0x5eed);

        let first = plan.run_batch(257, &reference, &mut first_rng).unwrap();
        let second = plan.run_batch(257, &reference, &mut second_rng).unwrap();
        for measurement in 0..first.num_major() {
            for shot in 0..first.num_minor() {
                assert_eq!(first.get(measurement, shot), second.get(measurement, shot));
            }
        }
    }

    #[test]
    fn gapped_qubit_labels_are_compacted_without_changing_loss_semantics() {
        let circuit = parse_lines("R 1 100\nX 1\nLOSS(1) 1\nCX 1 100\nX 100\nM 1 100\n").unwrap();
        let plan = LossSamplerPlan::try_compile(&circuit).expect("supported loss plan");
        assert_eq!(plan.num_simulated_qubits, 2);

        let mut packed_rng = StdRng::seed_from_u64(5);
        let mut legacy_rng = StdRng::seed_from_u64(5);
        let reference = crate::data_path::build_reference_sample(
            &circuit,
            crate::data_path::ReferenceSampleMode::SimulateNoiseless,
        )
        .unwrap();
        let batch = plan.run_batch(1, &reference, &mut packed_rng).unwrap();
        let packed: Vec<_> = (0..plan.num_measurements)
            .map(|measurement| batch.get(measurement, 0))
            .collect();
        let legacy = Executor::from_instrs(circuit)
            .unwrap()
            .run(&mut legacy_rng)
            .unwrap()
            .measurements;
        assert_eq!(packed, vec![true, true]);
        assert_eq!(packed, legacy);
    }

    #[test]
    fn public_atom_loss_fixture_compiles_to_the_css_frame() {
        let circuit = parse_lines(include_str!(
            "../../benchmarks/rstim_vs_stim_simulator/fixtures/stim_surface_code_rotated_memory_z_d11_r100_atom_loss.stim"
        ))
        .unwrap();
        let plan = LossSamplerPlan::try_compile(&circuit).expect("fixture uses supported loss ops");
        assert_eq!(plan.num_simulated_qubits, 241);
        assert_eq!(plan.num_measurements, 12_121);
    }
}