stabilizer_qec 2.1.0

Quantum error-correction simulation and decoding: Stim-format circuits and error models, a bit-parallel sampler, sparse-blossom, correlated and belief matching, BP+OSD, window decoders, bivariate bicycle codes, lattice surgery, Clifford+T circuits with coherent noise, and sampling and BP on the GPU.
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
//! A detector error model as Stim writes one: errors, detector and observable declarations,
//! `shift_detectors`, and `repeat` blocks, kept as a program rather than unrolled.
//!
//! WHY THIS EXISTS
//! ---------------
//! A long memory's model repeats itself round after round. Stim folds the repetition into a
//! `repeat` block, so a d = 11 memory of 10,000 rounds is a few hundred lines; unrolled it is
//! millions of faults. This holds the folded form, reads and prints it, and unrolls it (into
//! `Dem`, the flat model the decoders take) only when a decoder needs it.

use std::fmt::Write as _;

use crate::circuit::{split_instruction, strip_comment};
use crate::dem::{xor_prob, Dem, Mechanism, Piece};

/// An observable's target: its index with the top bit set, so that within a sorted list of
/// targets the detectors come first, as in Stim.
pub const OBS: u64 = 1 << 63;

/// The largest detector index a flat model may name.
pub const MAX_DETECTOR: u64 = (1 << 24) - 1;

/// The most errors and declarations a model may unroll into.
pub const MAX_UNROLLED_LINES: u64 = 1 << 24;

#[derive(Clone, Debug, PartialEq)]
pub enum DemInstr {
    /// `error[tag](p) D0 D1 ^ D2 L0`: each piece's targets as written (order and repeats kept,
    /// as Stim keeps them; detectors relative to the current shift), pieces in order. A target
    /// written twice in a piece cancels when the error's meaning is taken (`mechanism`).
    Error { p: f64, pieces: Vec<Vec<u64>>, tag: String },
    /// `detector[tag](coords) D3`: detectors, relative.
    Detector { coords: Vec<f64>, targets: Vec<u64>, tag: String },
    /// `logical_observable[tag] L1`.
    Observable { targets: Vec<u32>, tag: String },
    /// `shift_detectors[tag](coords) n`.
    Shift { coords: Vec<f64>, by: u64, tag: String },
    /// `repeat[tag] count { body }`.
    Repeat { count: u64, body: Vec<DemInstr>, tag: String },
}

/// A model as a program.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DemProgram {
    pub instrs: Vec<DemInstr>,
}

/// What a program holds, counted through its loops without unrolling them.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Stats {
    /// One more than the largest detector index named, saturating.
    pub num_detectors: u64,
    /// One more than the largest observable index named.
    pub num_observables: usize,
    /// Error instructions, counted through loops, saturating.
    pub num_errors: u64,
}

/// A body's counts: its largest detector index plus one relative to its start (0 for none),
/// how far it shifts, its errors, and its observables.
#[derive(Clone, Copy, Default)]
struct BlockStats {
    end: u64,
    shift: u64,
    errors: u64,
    observables: usize,
}

fn block_stats(instrs: &[DemInstr]) -> BlockStats {
    let mut s = BlockStats::default();
    for ins in instrs {
        match ins {
            DemInstr::Error { pieces, .. } => {
                s.errors = s.errors.saturating_add(1);
                for t in pieces.iter().flatten() {
                    if t & OBS == 0 {
                        s.end = s.end.max(s.shift.saturating_add(t + 1));
                    } else {
                        s.observables = s.observables.max((t & !OBS) as usize + 1);
                    }
                }
            }
            DemInstr::Detector { targets, .. } => {
                for t in targets {
                    s.end = s.end.max(s.shift.saturating_add(t + 1));
                }
            }
            DemInstr::Observable { targets, .. } => {
                for &t in targets {
                    s.observables = s.observables.max(t as usize + 1);
                }
            }
            DemInstr::Shift { by, .. } => s.shift = s.shift.saturating_add(*by),
            DemInstr::Repeat { count, body, .. } => {
                if *count == 0 {
                    continue;
                }
                let b = block_stats(body);
                if b.end > 0 {
                    let last = s.shift.saturating_add(b.shift.saturating_mul(count - 1));
                    s.end = s.end.max(last.saturating_add(b.end));
                }
                s.shift = s.shift.saturating_add(b.shift.saturating_mul(*count));
                s.errors = s.errors.saturating_add(b.errors.saturating_mul(*count));
                s.observables = s.observables.max(b.observables);
            }
        }
    }
    s
}

/// The significant digits Stim prints a model's numbers to on this machine: its stream's
/// precision is `numeric_limits<long double>::digits10 + 1`, and `long double` is a double on
/// Windows and Apple silicon (16), x87's 80-bit extended on other x86-64 (19), and IEEE quad
/// on other ARM64 (34).
pub const STIM_PRECISION: usize = if cfg!(all(target_arch = "x86_64", not(target_os = "windows"))) {
    19
} else if cfg!(all(target_arch = "aarch64", not(any(target_os = "windows", target_vendor = "apple")))) {
    34
} else {
    16
};

/// A number as Stim prints one in a model on this machine (`fmt_g` at `STIM_PRECISION`).
pub fn fmt_g16(x: f64) -> String {
    // A model's numbers go through C++'s stream, which keeps a negative zero's sign (a
    // circuit's printer does not).
    if x == 0.0 && x.is_sign_negative() {
        return "-0".into();
    }
    fmt_g(x, STIM_PRECISION)
}

/// C++'s default stream format at precision `digits` (`%.{digits}g`): that many significant
/// digits, trailing zeros dropped, and an exponent below 10⁻⁴ or from 10^digits.
pub fn fmt_g(x: f64, digits: usize) -> String {
    if x == 0.0 || !x.is_finite() {
        return if x == 0.0 { "0".into() } else { format!("{x}") };
    }
    let sci = format!("{x:.*e}", digits - 1);
    let (mantissa, exp) = sci.split_once('e').expect("an exponent");
    let exp: i32 = exp.parse().expect("an integer exponent");
    let trim = |t: &str| -> String {
        if t.contains('.') {
            t.trim_end_matches('0').trim_end_matches('.').to_string()
        } else {
            t.to_string()
        }
    };
    if exp < -4 || exp >= digits as i32 {
        let sign = if exp < 0 { '-' } else { '+' };
        format!("{}e{sign}{:02}", trim(mantissa), exp.abs())
    } else {
        trim(&format!("{x:.*}", (digits as i32 - 1 - exp) as usize))
    }
}

/// A number exactly: whole numbers as integers, others as the shortest decimal that reads
/// back as the same double.
pub fn fmt_exact(x: f64) -> String {
    if x.fract() == 0.0 && x.abs() < 9.2e18 {
        format!("{}", x as i64)
    } else {
        format!("{x:?}")
    }
}

fn fmt_args(args: &[f64], fmt: fn(f64) -> String) -> String {
    args.iter().map(|&v| fmt(v)).collect::<Vec<_>>().join(", ")
}

fn tagged(tag: &str) -> String {
    if tag.is_empty() {
        String::new()
    } else {
        format!("[{tag}]")
    }
}

fn with_args(name: &str, tag: &str, args: &[f64], fmt: fn(f64) -> String) -> String {
    let mut s = format!("{name}{}", tagged(tag));
    if !args.is_empty() {
        let _ = write!(s, "({})", fmt_args(args, fmt));
    }
    s
}

fn push_target(s: &mut String, t: u64) {
    if t & OBS == 0 {
        let _ = write!(s, " D{t}");
    } else {
        let _ = write!(s, " L{}", t & !OBS);
    }
}

/// XOR `t` into a sorted target list.
/// A piece's targets sorted, a target written twice cancelled.
pub fn canonical(piece: &[u64]) -> Vec<u64> {
    let mut out = Vec::with_capacity(piece.len());
    for &t in piece {
        toggle(&mut out, t);
    }
    out
}

fn toggle(v: &mut Vec<u64>, t: u64) {
    match v.binary_search(&t) {
        Ok(i) => {
            v.remove(i);
        }
        Err(i) => v.insert(i, t),
    }
}

impl DemProgram {
    pub fn stats(&self) -> Stats {
        let b = block_stats(&self.instrs);
        Stats { num_detectors: b.end, num_observables: b.observables, num_errors: b.errors }
    }

    /// Stim's text.
    pub fn to_stim(&self) -> String {
        let mut s = String::new();
        emit(&self.instrs, "", &mut s, fmt_g16);
        s
    }

    /// The model's text with every number exactly (the shortest decimal that reads back as the
    /// same double), for copies that must not lose the bits Stim's printing drops.
    pub fn to_exact_text(&self) -> String {
        let mut s = String::new();
        emit(&self.instrs, "", &mut s, fmt_exact);
        s
    }

    pub fn parse(text: &str) -> Result<DemProgram, String> {
        let lines: Vec<&str> = text.lines().collect();
        let mut pos = 0usize;
        let instrs = parse_block(&lines, &mut pos, false)?;
        Ok(DemProgram { instrs })
    }

    /// The program unrolled: no `repeat` and no `shift_detectors`, detectors absolute and
    /// coordinates shifted, in the program's order. Refuses past `MAX_UNROLLED_LINES` errors and
    /// declarations, or past detector `MAX_DETECTOR`.
    pub fn flattened(&self) -> Result<DemProgram, String> {
        let mut out = Vec::new();
        let mut st = Unroll { offset: 0, coords: Vec::new(), budget: MAX_UNROLLED_LINES };
        unroll(&self.instrs, &mut st, &mut |ins| out.push(ins))?;
        Ok(DemProgram { instrs: out })
    }

    /// Every detector's time (its last coordinate, shifts applied), by detector, walking the
    /// declarations alone: a model of millions of faults costs only its detectors. Refuses a
    /// detector without coordinates, or more than `MAX_DETECTOR` + 1 detectors.
    pub fn detector_times(&self) -> Result<Vec<f64>, String> {
        let n = self.stats().num_detectors;
        if n > MAX_DETECTOR + 1 {
            return Err(format!("the model has {n} detectors, past the largest index, {MAX_DETECTOR}"));
        }
        let mut times = vec![f64::NAN; n as usize];
        fn walk(instrs: &[DemInstr], offset: &mut u64, coords: &mut Vec<f64>, times: &mut [f64]) -> Result<(), String> {
            for ins in instrs {
                match ins {
                    DemInstr::Detector { coords: c, targets, .. } => {
                        let k = c.len().checked_sub(1).ok_or("a detector has no coordinates; windows need its time")?;
                        let t = c[k] + coords.get(k).copied().unwrap_or(0.0);
                        for &d in targets {
                            times[(d + *offset) as usize] = t;
                        }
                    }
                    DemInstr::Shift { coords: c, by, .. } => {
                        if coords.len() < c.len() {
                            coords.resize(c.len(), 0.0);
                        }
                        for (a, b) in coords.iter_mut().zip(c) {
                            *a += b;
                        }
                        *offset += by;
                    }
                    DemInstr::Repeat { count, body, .. } => {
                        // A body that declares nothing and shifts nothing changes nothing.
                        if body.iter().any(|i| !matches!(i, DemInstr::Error { .. } | DemInstr::Observable { .. })) {
                            for _ in 0..*count {
                                walk(body, offset, coords, times)?;
                            }
                        }
                    }
                    DemInstr::Error { .. } | DemInstr::Observable { .. } => {}
                }
            }
            Ok(())
        }
        walk(&self.instrs, &mut 0, &mut Vec::new(), &mut times)?;
        if let Some(d) = times.iter().position(|t| t.is_nan()) {
            return Err(format!("detector D{d} is not declared with coordinates; windows need its time"));
        }
        Ok(times)
    }

    /// The flat model the decoders take, in the program's order.
    pub fn to_dem(&self) -> Result<Dem, String> {
        let stats = self.stats();
        if stats.num_detectors > MAX_DETECTOR + 1 {
            return Err(format!("the model names detector {}, past the largest index, {MAX_DETECTOR}", stats.num_detectors - 1));
        }
        if stats.num_observables > 64 {
            return Err(format!(
                "the model has {} observables; this engine's decoders and model sampler take at most 64 (the text, counts, circuit sampling and measurement conversion take any number)",
                stats.num_observables
            ));
        }
        let mut dem = Dem { num_detectors: stats.num_detectors as usize, num_observables: stats.num_observables, ..Dem::default() };
        dem.detector_coords = vec![Vec::new(); dem.num_detectors];
        let mut st = Unroll { offset: 0, coords: Vec::new(), budget: MAX_UNROLLED_LINES };
        unroll(&self.instrs, &mut st, &mut |ins| match ins {
            DemInstr::Error { p, pieces, tag } => dem.mechanisms.push(mechanism(p, &pieces, tag)),
            DemInstr::Detector { coords, targets, tag } => {
                for d in targets {
                    let d = d as usize;
                    dem.detector_coords[d] = coords.clone();
                    if !tag.is_empty() {
                        if dem.detector_tags.len() <= d {
                            dem.detector_tags.resize(d + 1, String::new());
                        }
                        dem.detector_tags[d] = tag.clone();
                    }
                }
            }
            DemInstr::Observable { targets, tag } => {
                for l in targets {
                    let l = l as usize;
                    if !tag.is_empty() {
                        if dem.observable_tags.len() <= l {
                            dem.observable_tags.resize(l + 1, String::new());
                        }
                        dem.observable_tags[l] = tag.clone();
                    }
                }
            }
            DemInstr::Shift { .. } | DemInstr::Repeat { .. } => {}
        })?;
        Ok(dem)
    }

    /// The flat model of a model this engine built: `to_dem`, then faults that are the same in
    /// every way merged and all sorted, as the decoders have always been given it.
    pub fn to_dem_merged(&self) -> Result<Dem, String> {
        let mut dem = self.to_dem()?;
        let key = |m: &Mechanism| (m.detectors.clone(), m.observables, m.tag.clone(), m.pieces.clone());
        dem.mechanisms.sort_by(|a, b| {
            a.detectors
                .cmp(&b.detectors)
                .then(a.observables.cmp(&b.observables))
                .then(a.tag.cmp(&b.tag))
                .then(a.pieces.cmp(&b.pieces))
        });
        let mut merged: Vec<Mechanism> = Vec::with_capacity(dem.mechanisms.len());
        for m in dem.mechanisms {
            match merged.last_mut() {
                Some(last) if key(last) == key(&m) => last.p = xor_prob(last.p, m.p),
                _ => merged.push(m),
            }
        }
        dem.mechanisms = merged;
        Ok(dem)
    }
}

/// A flat model's mechanism from an error's absolute pieces, as the `.dem` reader has always
/// made one: the pieces' XOR, and the pieces kept when there are several or the one is
/// graph-like.
fn mechanism(p: f64, pieces: &[Vec<u64>], tag: String) -> Mechanism {
    let mut all = Vec::new();
    let mut kept = Vec::new();
    for piece in pieces {
        let piece = canonical(piece);
        let (dets, obs) = split(&piece);
        kept.push(Piece { detectors: dets, observables: obs });
        for &t in &piece {
            toggle(&mut all, t);
        }
    }
    let (detectors, observables) = split(&all);
    let pieces = if kept.len() > 1 {
        kept.into_iter().filter(|p| !p.detectors.is_empty() || p.observables != 0).collect()
    } else if (1..=2).contains(&detectors.len()) {
        vec![Piece { detectors: detectors.clone(), observables }]
    } else {
        Vec::new()
    };
    Mechanism { p, detectors, observables, pieces, tag }
}

/// A sorted target list as detectors (`u32`) and an observable mask.
pub fn split(targets: &[u64]) -> (Vec<u32>, u64) {
    let mut dets = Vec::new();
    let mut obs = 0u64;
    for &t in targets {
        if t & OBS == 0 {
            dets.push(t as u32);
        } else {
            obs ^= 1u64 << (t & !OBS);
        }
    }
    (dets, obs)
}

struct Unroll {
    offset: u64,
    coords: Vec<f64>,
    budget: u64,
}

fn unroll(instrs: &[DemInstr], st: &mut Unroll, out: &mut dyn FnMut(DemInstr)) -> Result<(), String> {
    let absolute = |t: u64, offset: u64| -> Result<u64, String> {
        if t & OBS != 0 {
            return Ok(t);
        }
        t.checked_add(offset)
            .filter(|&d| d <= MAX_DETECTOR)
            .ok_or_else(|| format!("the model names a detector past the largest index, {MAX_DETECTOR}"))
    };
    let spend = |st: &mut Unroll| -> Result<(), String> {
        if st.budget == 0 {
            return Err(format!("the model unrolls past {MAX_UNROLLED_LINES} errors and declarations"));
        }
        st.budget -= 1;
        Ok(())
    };
    for ins in instrs {
        match ins {
            DemInstr::Error { p, pieces, tag } => {
                spend(st)?;
                let mut abs = Vec::with_capacity(pieces.len());
                for piece in pieces {
                    abs.push(piece.iter().map(|&t| absolute(t, st.offset)).collect::<Result<Vec<_>, _>>()?);
                }
                out(DemInstr::Error { p: *p, pieces: abs, tag: tag.clone() });
            }
            DemInstr::Detector { coords, targets, tag } => {
                spend(st)?;
                let coords = coords.iter().enumerate().map(|(i, v)| v + st.coords.get(i).copied().unwrap_or(0.0)).collect();
                let targets = targets.iter().map(|&t| absolute(t, st.offset)).collect::<Result<Vec<_>, _>>()?;
                out(DemInstr::Detector { coords, targets, tag: tag.clone() });
            }
            DemInstr::Observable { .. } => {
                spend(st)?;
                out(ins.clone());
            }
            DemInstr::Shift { coords, by, .. } => {
                if st.coords.len() < coords.len() {
                    st.coords.resize(coords.len(), 0.0);
                }
                for (a, b) in st.coords.iter_mut().zip(coords) {
                    *a += b;
                }
                st.offset = st.offset.checked_add(*by).filter(|&o| o <= MAX_DETECTOR + 1).ok_or_else(|| {
                    format!("shift_detectors moves past detector {MAX_DETECTOR}, the largest index")
                })?;
            }
            DemInstr::Repeat { count, body, .. } => {
                for _ in 0..*count {
                    // Every pass costs at least one unit, so an empty body cannot spin forever.
                    spend(st)?;
                    unroll(body, st, out)?;
                }
            }
        }
    }
    Ok(())
}

fn emit(instrs: &[DemInstr], indent: &str, s: &mut String, fmt: fn(f64) -> String) {
    for ins in instrs {
        match ins {
            DemInstr::Error { p, pieces, tag } => {
                let _ = write!(s, "{indent}{}", with_args("error", tag, &[*p], fmt));
                for (k, piece) in pieces.iter().enumerate() {
                    if k > 0 {
                        s.push_str(" ^");
                    }
                    for &t in piece {
                        push_target(s, t);
                    }
                }
            }
            DemInstr::Detector { coords, targets, tag } => {
                let _ = write!(s, "{indent}{}", with_args("detector", tag, coords, fmt));
                for &t in targets {
                    push_target(s, t);
                }
            }
            DemInstr::Observable { targets, tag } => {
                let _ = write!(s, "{indent}logical_observable{}", tagged(tag));
                for &t in targets {
                    let _ = write!(s, " L{t}");
                }
            }
            DemInstr::Shift { coords, by, tag } => {
                let _ = write!(s, "{indent}{} {by}", with_args("shift_detectors", tag, coords, fmt));
            }
            DemInstr::Repeat { count, body, tag } => {
                let _ = writeln!(s, "{indent}repeat{} {count} {{", tagged(tag));
                emit(body, &format!("{indent}    "), s, fmt);
                if body.is_empty() {
                    // An empty block keeps a blank line, as Stim writes it.
                    s.push('\n');
                }
                let _ = write!(s, "{indent}}}");
            }
        }
        s.push('\n');
    }
}

fn parse_block(lines: &[&str], pos: &mut usize, nested: bool) -> Result<Vec<DemInstr>, String> {
    let mut out = Vec::new();
    while *pos < lines.len() {
        let lineno = *pos + 1;
        let line = strip_comment(lines[*pos]).trim();
        *pos += 1;
        if line.is_empty() {
            continue;
        }
        if line == "}" {
            if nested {
                return Ok(out);
            }
            return Err(format!("line {lineno}: unmatched '}}'"));
        }
        if let Some(rest) = line.strip_suffix('{') {
            let (name, tag, args, parts) = split_instruction(rest).map_err(|e| format!("line {lineno}: {e}"))?;
            if name != "REPEAT" {
                return Err(format!("line {lineno}: only repeat opens a block"));
            }
            let count: u64 = match (args.is_empty(), parts.as_slice()) {
                (true, [n]) => crate::circuit::uint(n),
                _ => None,
            }
            .ok_or_else(|| format!("line {lineno}: repeat needs a count"))?;
            let body = parse_block(lines, pos, true)?;
            out.push(DemInstr::Repeat { count, body, tag: tag.to_string() });
            continue;
        }
        let (name, tag, args, tokens) = split_instruction(line).map_err(|e| format!("line {lineno}: {e}"))?;
        let tag = tag.to_string();
        let bad = |t: &str| format!("line {lineno}: bad target '{t}'");
        let detector = |t: &str| -> Result<u64, String> {
            t.strip_prefix(['D', 'd']).and_then(crate::circuit::uint::<u64>).filter(|&d| d <= MAX_DETECTOR).ok_or_else(|| bad(t))
        };
        let observable = |t: &str| -> Result<u32, String> {
            let l = t.strip_prefix(['L', 'l']).and_then(crate::circuit::uint::<u32>).ok_or_else(|| bad(t))?;
            Ok(l)
        };
        out.push(match name.as_str() {
            "ERROR" => {
                if args.len() != 1 || !(0.0..=1.0).contains(&args[0]) {
                    return Err(format!("line {lineno}: error takes one probability"));
                }
                // Stim's rules for separators.
                if tokens.first() == Some(&"^") || tokens.last() == Some(&"^") {
                    return Err(format!("line {lineno}: First/last targets of 'error' instruction shouldn't be separators (^)."));
                }
                if tokens.windows(2).any(|w| w[0] == "^" && w[1] == "^") {
                    return Err(format!("line {lineno}: 'error' instruction has adjacent separators (^ ^)."));
                }
                let mut pieces = vec![Vec::new()];
                for t in &tokens {
                    if *t == "^" {
                        pieces.push(Vec::new());
                    } else if t.starts_with(['D', 'd']) {
                        pieces.last_mut().unwrap().push(detector(t)?);
                    } else if t.starts_with(['L', 'l']) {
                        pieces.last_mut().unwrap().push(OBS | u64::from(observable(t)?));
                    } else {
                        return Err(bad(t));
                    }
                }
                DemInstr::Error { p: args[0], pieces, tag }
            }
            "DETECTOR" | "LOGICAL_OBSERVABLE" if tokens.len() != 1 => {
                return Err(format!("line {lineno}: '{}' instruction takes 1 target but got {} arguments.", name.to_ascii_lowercase(), tokens.len()));
            }
            "DETECTOR" => {
                let targets = tokens.iter().map(|t| detector(t)).collect::<Result<Vec<_>, _>>()?;
                DemInstr::Detector { coords: args, targets, tag }
            }
            "LOGICAL_OBSERVABLE" => {
                let targets = tokens.iter().map(|t| observable(t)).collect::<Result<Vec<_>, _>>()?;
                DemInstr::Observable { targets, tag }
            }
            "SHIFT_DETECTORS" => {
                let by = match tokens.as_slice() {
                    [] => 0,
                    [n] => crate::circuit::uint::<u64>(n).filter(|&n| n <= MAX_DETECTOR + 1).ok_or_else(|| bad(n))?,
                    _ => return Err(format!("line {lineno}: shift_detectors takes one count")),
                };
                DemInstr::Shift { coords: args, by, tag }
            }
            _ => return Err(format!("line {lineno}: unsupported instruction '{name}'")),
        });
    }
    if nested {
        return Err("unterminated repeat block".into());
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::{fmt_g, DemProgram};

    #[test]
    fn lines_are_read_as_stim_reads_them() {
        let text = |t: &str| DemProgram::parse(t).map(|d| d.to_stim());
        assert_eq!(text("error(0.5) d0 l1\ndetector(1) d3\nlogical_observable l2").unwrap().trim_end(), "error(0.5) D0 L1\ndetector(1) D3\nlogical_observable L2");
        for bad in ["detector D0 D1", "detector(1)", "logical_observable L0 L1", "logical_observable", "error (0.1) D0", "error(0.1)D0"] {
            assert!(text(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn numbers_print_as_stims_do() {
        // At 19 digits, as Stim prints on x86-64 Linux, and 34, as on ARM64 Linux.
        assert_eq!(fmt_g(0.0025961611285238335, 19), "0.002596161128523833544");
        assert_eq!(fmt_g(0.5, 34), "0.5");
        assert_eq!(fmt_g(0.1, 19), "0.1000000000000000056");
        let fmt_g16 = |x| fmt_g(x, 16);
        for (x, want) in [
            (0.002596161128523834, "0.002596161128523834"),
            (6.669779853440971e-05, "6.669779853440971e-05"),
            (0.001, "0.001"),
            (0.5, "0.5"),
            (1.0, "1"),
            (2.0, "2"),
            (-1.5, "-1.5"),
            (0.0, "0"),
            (1e-300, "1e-300"),
            (123456789.0, "123456789"),
            (1e16, "1e+16"),
            (0.0001, "0.0001"),
            (0.00001, "1e-05"),
        ] {
            assert_eq!(fmt_g16(x), want, "{x}");
        }
    }
}