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
use rand::Rng;

use crate::compiled::{
    choose_sampler_path, compile_circuit, sample_compiled_batch_with_reference, CompiledCircuit,
    SamplerPathDecision, SamplingFallbackReason,
};
use crate::data_path::{
    build_reference_sample, build_reference_sample_with_decision,
    build_reference_sample_with_sweep_bits_and_decision, ReferenceSampleDecision,
    ReferenceSampleMode,
};
use crate::executor::max_qubit;
use crate::executor::Executor;
use crate::ir::StimInstr;
use crate::loss_sampler::LossSamplerPlan;
use crate::m2d::{
    measurements_to_detections_with_options, measurements_to_detections_with_reference, M2dOptions,
};
use crate::sim::bit_table::BitTable;
use crate::sim::frame::FrameSimulator;

pub struct BatchOutput {
    pub measurements: BitTable,
    pub detections: BitTable,
    pub observable_flips: BitTable,
    pub output_mode: SampleOutputMode,
    pub detector_materializations: usize,
    pub observable_materializations: usize,
}

impl BatchOutput {
    pub(crate) fn full(
        measurements: BitTable,
        detections: BitTable,
        observable_flips: BitTable,
        detector_materializations: usize,
        observable_materializations: usize,
    ) -> Self {
        Self {
            measurements,
            detections,
            observable_flips,
            output_mode: SampleOutputMode::Full,
            detector_materializations,
            observable_materializations,
        }
    }

    pub(crate) fn measurements_only(
        measurements: BitTable,
        n_shots: usize,
    ) -> Result<Self, String> {
        Self::measurements_only_with_materializations(measurements, n_shots, 0, 0)
    }

    pub(crate) fn measurements_only_with_materializations(
        measurements: BitTable,
        n_shots: usize,
        detector_materializations: usize,
        observable_materializations: usize,
    ) -> Result<Self, String> {
        Ok(Self {
            measurements,
            detections: alloc_bit_table(0, n_shots)?,
            observable_flips: alloc_bit_table(0, n_shots)?,
            output_mode: SampleOutputMode::MeasurementsOnly,
            detector_materializations,
            observable_materializations,
        })
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SampleOutputMode {
    #[default]
    Full,
    MeasurementsOnly,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SamplingBackend {
    #[default]
    Auto,
    Interpreted,
    Compiled,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct SampleOptions {
    pub reference_sample_mode: crate::data_path::ReferenceSampleMode,
    pub backend: SamplingBackend,
    pub output_mode: SampleOutputMode,
}

#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SampleBatchDecision {
    PackedInverse,
    Interpreted,
    InterpretedLegacy(SamplingFallbackReason),
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CompiledMeasurementSamplerDiagnostics {
    pub compiled_ir_builds: usize,
    pub reference_builds: usize,
    pub sample_calls: usize,
}

#[derive(Debug)]
pub struct CompiledMeasurementSampler {
    compiled: CompiledCircuit,
    reference_sample: Vec<bool>,
    diagnostics: CompiledMeasurementSamplerDiagnostics,
}

/// A reusable sampler for circuits whose `LOSS` instructions can conditionally suppress gates.
///
/// Compiling once keeps the loss operation plan and noiseless reference sample out of the timed
/// steady-state sampling path. When loss can affect a later entangling gate, sampling uses a
/// self-contained bit-parallel CSS frame so each shot can follow its own Clifford path.
#[derive(Debug)]
pub struct CompiledLossMeasurementSampler {
    instrs: Vec<StimInstr>,
    plan: LossSamplerPlan,
    reference_sample: Vec<bool>,
    diagnostics: CompiledMeasurementSamplerDiagnostics,
}

impl CompiledMeasurementSampler {
    pub fn compile(
        instrs: &[StimInstr],
        reference_mode: ReferenceSampleMode,
    ) -> Result<Self, String> {
        Self::compile_with_decision(instrs, reference_mode).map_err(|reason| reason.to_string())
    }

    #[doc(hidden)]
    pub fn compile_with_decision(
        instrs: &[StimInstr],
        reference_mode: ReferenceSampleMode,
    ) -> Result<Self, SamplingFallbackReason> {
        let compiled =
            compile_circuit(instrs).map_err(SamplingFallbackReason::UnsupportedOperation)?;
        match choose_sampler_path(&compiled) {
            SamplerPathDecision::FastPath => {}
            SamplerPathDecision::Fallback(reason) => return Err(reason),
        }

        let reference_sample = match reference_mode {
            ReferenceSampleMode::SimulateNoiseless => {
                let reference = build_reference_sample_with_decision(instrs)
                    .map_err(SamplingFallbackReason::UnsupportedOperation)?;
                match reference.decision {
                    ReferenceSampleDecision::PackedInverse => reference.bits,
                    ReferenceSampleDecision::LegacyFallback(reason) => return Err(reason),
                }
            }
            ReferenceSampleMode::AssumeAllZero => vec![false; compiled.num_measurements],
        };
        Ok(Self {
            compiled,
            reference_sample,
            diagnostics: CompiledMeasurementSamplerDiagnostics {
                compiled_ir_builds: 1,
                reference_builds: 1,
                sample_calls: 0,
            },
        })
    }

    pub fn sample(
        &mut self,
        shots: usize,
        rng: &mut impl Rng,
        output_mode: SampleOutputMode,
    ) -> Result<BatchOutput, String> {
        self.diagnostics.sample_calls += 1;
        sample_compiled_batch_with_reference(
            &self.compiled,
            &self.reference_sample,
            shots,
            rng,
            SampleOptions {
                output_mode,
                ..SampleOptions::default()
            },
        )
    }

    #[doc(hidden)]
    pub fn diagnostics(&self) -> CompiledMeasurementSamplerDiagnostics {
        self.diagnostics
    }
}

impl CompiledLossMeasurementSampler {
    pub fn compile(
        instrs: &[StimInstr],
        reference_mode: ReferenceSampleMode,
    ) -> Result<Self, String> {
        let plan = LossSamplerPlan::try_compile(instrs)
            .ok_or_else(|| "circuit does not support the compiled loss sampler".to_string())?;
        let reference_sample = match reference_mode {
            ReferenceSampleMode::SimulateNoiseless => {
                build_reference_sample(instrs, reference_mode)?
            }
            ReferenceSampleMode::AssumeAllZero => vec![false; plan.num_measurements()],
        };
        Ok(Self {
            instrs: instrs.to_vec(),
            plan,
            reference_sample,
            diagnostics: CompiledMeasurementSamplerDiagnostics {
                compiled_ir_builds: 1,
                reference_builds: 1,
                sample_calls: 0,
            },
        })
    }

    pub fn sample(
        &mut self,
        shots: usize,
        rng: &mut impl Rng,
        output_mode: SampleOutputMode,
    ) -> Result<BatchOutput, String> {
        self.diagnostics.sample_calls += 1;
        let measurements = self.plan.run_batch(shots, &self.reference_sample, rng)?;
        if output_mode == SampleOutputMode::MeasurementsOnly {
            return BatchOutput::measurements_only(measurements, shots);
        }

        let m2d = measurements_to_detections_with_reference(
            &self.instrs,
            &measurements,
            &self.reference_sample,
        )?;
        let (detector_materializations, observable_materializations) =
            count_output_materialization_ops(&self.instrs);
        Ok(BatchOutput::full(
            measurements,
            m2d.detections,
            m2d.observable_flips,
            detector_materializations,
            observable_materializations,
        ))
    }

    #[doc(hidden)]
    pub fn diagnostics(&self) -> CompiledMeasurementSamplerDiagnostics {
        self.diagnostics
    }
}

pub fn sample_batch_with_options(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
    options: SampleOptions,
) -> Result<BatchOutput, String> {
    Ok(sample_batch_with_options_and_decision(instrs, n_shots, rng, options)?.0)
}

#[doc(hidden)]
pub fn sample_batch_with_options_and_decision(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
    options: SampleOptions,
) -> Result<(BatchOutput, SampleBatchDecision), String> {
    sample_batch_with_options_sweep_bits_and_decision(instrs, n_shots, rng, options, None)
}

#[doc(hidden)]
pub fn sample_batch_with_options_sweep_bits_and_decision(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
    options: SampleOptions,
    sweep_bits: Option<&[bool]>,
) -> Result<(BatchOutput, SampleBatchDecision), String> {
    match options.backend {
        SamplingBackend::Interpreted => Ok((
            sample_batch_interpreted_with_sweep_bits(instrs, n_shots, rng, options, sweep_bits)?,
            SampleBatchDecision::Interpreted,
        )),
        SamplingBackend::Compiled => {
            let mut sampler = CompiledMeasurementSampler::compile_with_decision(
                instrs,
                options.reference_sample_mode,
            )
            .map_err(|reason| reason.to_string())?;
            let out = sampler.sample(n_shots, rng, options.output_mode)?;
            Ok((out, SampleBatchDecision::PackedInverse))
        }
        SamplingBackend::Auto => {
            match CompiledMeasurementSampler::compile_with_decision(
                instrs,
                options.reference_sample_mode,
            ) {
                Ok(mut sampler) => {
                    let out = sampler.sample(n_shots, rng, options.output_mode)?;
                    Ok((out, SampleBatchDecision::PackedInverse))
                }
                Err(reason) => {
                    let out = sample_batch_interpreted_with_sweep_bits(
                        instrs, n_shots, rng, options, sweep_bits,
                    )?;
                    Ok((out, SampleBatchDecision::InterpretedLegacy(reason)))
                }
            }
        }
    }
}

pub fn sample_batch(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
) -> Result<BatchOutput, String> {
    sample_batch_with_options(instrs, n_shots, rng, SampleOptions::default())
}

fn count_output_materialization_ops(instrs: &[StimInstr]) -> (usize, usize) {
    let mut detector_count = 0usize;
    let mut observable_count = 0usize;

    for instr in instrs {
        match instr {
            StimInstr::Op { name, .. } => match name.as_str() {
                "DETECTOR" => {
                    detector_count = detector_count.saturating_add(1);
                }
                "OBSERVABLE_INCLUDE" => {
                    observable_count = observable_count.saturating_add(1);
                }
                _ => {}
            },
            StimInstr::Repeat { count, body } => {
                let (body_detectors, body_observables) = count_output_materialization_ops(body);
                let repeat_count = usize::try_from(*count).unwrap_or(usize::MAX);
                detector_count =
                    detector_count.saturating_add(body_detectors.saturating_mul(repeat_count));
                observable_count =
                    observable_count.saturating_add(body_observables.saturating_mul(repeat_count));
            }
        }
    }

    (detector_count, observable_count)
}

fn sample_batch_interpreted_with_sweep_bits(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
    options: SampleOptions,
    sweep_bits: Option<&[bool]>,
) -> Result<BatchOutput, String> {
    if sweep_bits.is_some() || uses_executor_sampling_fallback(instrs) {
        return sample_batch_with_executor(instrs, n_shots, rng, options, sweep_bits);
    }

    let ref_sample = build_reference_sample(instrs, options.reference_sample_mode)?;
    let num_qubits = max_qubit(instrs)?;
    let mut frame = FrameSimulator::try_new(num_qubits, n_shots)?;
    frame.randomize_initial_z_frames(rng);
    frame
        .set_materialize_detector_observable_outputs(options.output_mode == SampleOutputMode::Full);
    frame.run(instrs, &ref_sample, rng)?;

    let measurements = frame.try_measurements(&ref_sample)?;
    match options.output_mode {
        SampleOutputMode::Full => Ok(BatchOutput::full(
            measurements,
            frame.try_detections()?,
            frame.try_observable_flips()?,
            frame.detector_materializations(),
            frame.observable_materializations(),
        )),
        SampleOutputMode::MeasurementsOnly => BatchOutput::measurements_only_with_materializations(
            measurements,
            n_shots,
            frame.detector_materializations(),
            frame.observable_materializations(),
        ),
    }
}

fn sample_batch_with_executor(
    instrs: &[StimInstr],
    n_shots: usize,
    rng: &mut impl Rng,
    options: SampleOptions,
    sweep_bits: Option<&[bool]>,
) -> Result<BatchOutput, String> {
    let ref_sample = match options.reference_sample_mode {
        ReferenceSampleMode::SimulateNoiseless => {
            build_reference_sample_with_sweep_bits_and_decision(instrs, sweep_bits)?.bits
        }
        ReferenceSampleMode::AssumeAllZero => vec![false; crate::stats::num_measurements(instrs)],
    };
    let n_meas = ref_sample.len();
    let loss_plan = if sweep_bits.is_none() {
        LossSamplerPlan::try_compile(instrs)
    } else {
        None
    };
    let mut executor = if loss_plan.is_none() {
        Some(Executor::from_instrs(instrs.to_vec())?)
    } else {
        None
    };

    let measurements = if let Some(plan) = &loss_plan {
        plan.run_batch(n_shots, &ref_sample, rng)?
    } else {
        let mut measurements = alloc_bit_table(n_meas, n_shots)?;
        for shot in 0..n_shots {
            let shot_measurements = executor
                .as_mut()
                .expect("legacy executor is available when the loss plan is absent")
                .run_with_sweep_bits(rng, sweep_bits)?
                .measurements;
            if shot_measurements.len() != n_meas {
                return Err(format!(
                    "executor produced {} measurements but reference sample expects {}",
                    shot_measurements.len(),
                    n_meas
                ));
            }
            for (m, &bit) in shot_measurements.iter().enumerate() {
                measurements.set(m, shot, bit);
            }
        }
        measurements
    };

    if options.output_mode == SampleOutputMode::MeasurementsOnly {
        return BatchOutput::measurements_only(measurements, n_shots);
    }

    let sweep_table = sweep_bits
        .map(|bits| repeated_sweep_table(instrs, bits, n_shots))
        .transpose()?;
    let m2d = measurements_to_detections_with_options(
        instrs,
        &measurements,
        sweep_table.as_ref(),
        M2dOptions {
            reference_sample_mode: options.reference_sample_mode,
            ran_without_feedback: false,
        },
    )?;
    let (detector_materializations, observable_materializations) =
        count_output_materialization_ops(instrs);

    Ok(BatchOutput::full(
        measurements,
        m2d.detections,
        m2d.observable_flips,
        detector_materializations,
        observable_materializations,
    ))
}

fn repeated_sweep_table(
    instrs: &[StimInstr],
    sweep_bits: &[bool],
    n_shots: usize,
) -> Result<BitTable, String> {
    let n_sweep = crate::stats::num_sweep_bits(instrs).max(sweep_bits.len());
    let mut table = alloc_bit_table(n_sweep, n_shots)?;
    for (sweep_index, bit) in sweep_bits.iter().copied().enumerate() {
        if bit {
            for shot in 0..n_shots {
                table.set(sweep_index, shot, true);
            }
        }
    }
    Ok(table)
}

fn alloc_bit_table(num_major: usize, num_minor: usize) -> Result<BitTable, String> {
    BitTable::try_new(num_major, num_minor)
        .map_err(|err| format!("BitTable allocation failed: {err:?}"))
}

fn uses_executor_sampling_fallback(instrs: &[StimInstr]) -> bool {
    for instr in instrs {
        match instr {
            StimInstr::Op { name, targets, .. } => {
                if matches!(
                    name.as_str(),
                    "LOSS"
                        | "ML"
                        | "MXL"
                        | "MYL"
                        | "MZL"
                        | "MRL"
                        | "MRXL"
                        | "MRYL"
                        | "MRZL"
                        | "MPP"
                ) {
                    return true;
                }
                if is_feedback_operation(name, targets)
                    || is_sweep_dependent_operation(name, targets)
                {
                    return true;
                }
            }
            StimInstr::Repeat { body, .. } => {
                if uses_executor_sampling_fallback(body) {
                    return true;
                }
            }
        }
    }
    false
}

fn is_feedback_operation(name: &str, targets: &[crate::ir::StimTarget]) -> bool {
    matches!(name, "CX" | "CNOT" | "ZCX" | "CY" | "ZCY" | "CZ" | "ZCZ")
        && targets.chunks_exact(2).any(|pair| {
            matches!(
                pair,
                [
                    crate::ir::StimTarget::Rec(_),
                    crate::ir::StimTarget::Qubit(_)
                ]
            )
        })
}

fn is_sweep_dependent_operation(name: &str, targets: &[crate::ir::StimTarget]) -> bool {
    targets
        .iter()
        .any(|target| matches!(target, crate::ir::StimTarget::Sweep(_)))
        && !matches!(
            name,
            "I" | "I_ERROR"
                | "II_ERROR"
                | "X_ERROR"
                | "Y_ERROR"
                | "Z_ERROR"
                | "DEPOLARIZE1"
                | "DEPOLARIZE2"
                | "TICK"
                | "QUBIT_COORDS"
                | "SHIFT_COORDS"
                | "DETECTOR"
                | "OBSERVABLE_INCLUDE"
        )
}

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

    use crate::data_path::ReferenceSampleMode;
    use crate::ir::StimTarget;

    #[test]
    fn sample_batch_with_executor_uses_shared_measurement_count_for_sweep_targets() {
        let instrs = vec![StimInstr::new("ML", vec![], vec![StimTarget::Sweep(0)])];
        let mut rng = StdRng::seed_from_u64(0);

        let result = sample_batch_with_executor(
            &instrs,
            1,
            &mut rng,
            SampleOptions {
                reference_sample_mode: ReferenceSampleMode::AssumeAllZero,
                ..SampleOptions::default()
            },
            None,
        );

        let output = result.expect("shared measurement count should match executor output");
        assert_eq!(output.measurements.num_major(), 0);
        assert_eq!(output.measurements.num_minor(), 1);
    }

    #[test]
    fn count_output_materialization_ops_expands_repeats_saturating() {
        let instrs = vec![
            StimInstr::new("DETECTOR", vec![], vec![StimTarget::Rec(-1)]),
            StimInstr::Repeat {
                count: 3,
                body: vec![
                    StimInstr::new("DETECTOR", vec![], vec![StimTarget::Rec(-1)]),
                    StimInstr::new("OBSERVABLE_INCLUDE", vec![0.0], vec![StimTarget::Rec(-1)]),
                ],
            },
        ];

        assert_eq!(count_output_materialization_ops(&instrs), (4, 3));
    }

    #[test]
    fn measurements_only_output_can_report_actual_materialization_counters() {
        let out =
            BatchOutput::measurements_only_with_materializations(BitTable::new(1, 4), 4, 2, 3)
                .unwrap();

        assert_eq!(out.output_mode, SampleOutputMode::MeasurementsOnly);
        assert_eq!(out.detections.num_major(), 0);
        assert_eq!(out.detections.num_minor(), 4);
        assert_eq!(out.observable_flips.num_major(), 0);
        assert_eq!(out.observable_flips.num_minor(), 4);
        assert_eq!(out.detector_materializations, 2);
        assert_eq!(out.observable_materializations, 3);
    }
}