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
//! Deltakit's leakage dialect (Riverlane's fork of Stim): `LEAKAGE(p)`, `RELAX(p)` and
//! `HERALD_LEAKAGE_EVENT(p)`, read as tagged instructions that Stim runs, and the circuit
//! without any leakage.
//!
//! WHY THIS EXISTS
//! ---------------
//! Deltakit writes leakage into circuits with three instructions of its own. `LEAKAGE(p) q`
//! marks q leaked with probability p and fully depolarizes it; `RELAX(p) q` returns a leaked q
//! to the computational space with probability p; `HERALD_LEAKAGE_EVENT(p) q` writes whether
//! q is leaked into the measurement record (each result flipped with probability p, `!q`
//! inverted), where a `DETECTOR` can read it. Plain Stim reads none of them. Here each line is
//! lowered as it is read into a tagged line that Stim does read: `I_ERROR[LEAKAGE(p=…)] q`,
//! `I_ERROR[RELAX(p=…)] q` and `MPAD[HERALD_LEAKAGE_EVENT(qubits=q …)](p) 0 …`, a padded
//! record that reads 0 when nothing leaks, as Deltakit's herald does. Stim, and this package's
//! Clifford engines and error models, run the circuit as if nothing leaked; the leakage sampler
//! runs Deltakit's model; `to_deltakit` writes the dialect back.
//!
//! `without_leakage` is Deltakit's deinstrumentation (`Measurements._deinstrument_leakage_circuit`,
//! which turns its measurements into detection events): no leakage noise, no herald and no
//! detector reading one, every other lookback renumbered. Deltakit's works line by line on the
//! text, so it renumbers a loop's lookbacks as if the loop ran once; this one keeps loops,
//! peeling the first iterations off a loop whose lookbacks reach before it, and leaves an
//! observable its other records where Deltakit drops the line.
//!
//! `LeakageAware` decodes with the heralds: around any decoder that weighs faults by their
//! priors (the machinery argument reweighting uses), a shot's fired triggers raise the priors
//! of the faults their qubits' leakage makes likely, and the shot is decoded once.

use std::collections::VecDeque;

use crate::clifford::ir::{self, GateTarget, Instruction, Item};
use crate::nonpauli::NonPauli;

const NAMES: [&str; 4] = ["LEAKAGE", "RELAX", "HERALD_LEAKAGE_EVENT", "RL"];

/// The tag of a lowered herald.
pub const HERALD: &str = "HERALD_LEAKAGE_EVENT";

/// The tag a two-qubit gate's leakage transport is lowered to (Deltakit's `CX(p1, p2, p3, p4)`).
pub const TRANSPORT: &str = "LEAKAGE_TRANSPORT";

/// The keys of a transport tag, in the order of Deltakit's four arguments: leakage spreading
/// from the first qubit (control) to the second and back, then leakage moving (mobility) from
/// the first to the second and back.
const TRANSPORT_KEYS: [&str; 4] = ["spread", "spread_back", "mobility", "mobility_back"];

/// The two-qubit gates leakage passes through in Deltakit's Stim, by every name Stim reads.
pub const TRANSPORT_GATES: [&str; 9] = ["CX", "ZCX", "CNOT", "CY", "ZCY", "CZ", "ZCZ", "SQRT_XX", "SQRT_XX_DAG"];

/// A herald's tag, for the qubits its records read (`!` before an inverted target's).
pub fn herald_tag(targets: &[(u32, bool)]) -> String {
    format!("{HERALD}(qubits={})", targets.iter().map(|&(q, inv)| format!("{}{q}", if inv { "!" } else { "" })).collect::<Vec<_>>().join(" "))
}

/// The four probabilities of a transport tag (`LEAKAGE_TRANSPORT(spread=…, spread_back=…,
/// mobility=…, mobility_back=…)`), if `tag` is one.
pub fn transport_args(tag: &str) -> Option<Result<[f64; 4], String>> {
    let inner = tag.trim().strip_prefix(TRANSPORT)?.trim();
    let inner = inner.strip_prefix('(').and_then(|s| s.strip_suffix(')'))?;
    let parts: Vec<&str> = inner.split(',').map(str::trim).collect();
    let mut out = [0.0; 4];
    if parts.len() != 4 {
        return Some(Err(format!("{TRANSPORT}: four arguments ({}), not {}", TRANSPORT_KEYS.join(", "), parts.len())));
    }
    for (k, (part, key)) in parts.iter().zip(TRANSPORT_KEYS).enumerate() {
        let value = part.split_once('=').filter(|(k, _)| k.trim() == key).map(|(_, v)| v.trim());
        match value.and_then(|v| v.parse::<f64>().ok()) {
            Some(p) if (0.0..=1.0).contains(&p) => out[k] = p,
            _ => return Some(Err(format!("{TRANSPORT}: argument {} must be {key}=<probability>, not '{part}'", k + 1))),
        }
    }
    Some(Ok(out))
}

/// A line naming a transport gate with four arguments (Deltakit's `CX(p1, p2, p3, p4) 0 1`),
/// lowered to the gate tagged with them; `None` for the gate with any other arguments, which
/// Stim reads (or refuses) itself.
fn lower_transport(indent: &str, name: &str, rest: &str) -> Result<Option<String>, String> {
    let tagged = rest.starts_with('[');
    let after_tag = if tagged {
        match rest.find(']') {
            Some(close) => &rest[close + 1..],
            None => return Ok(None),
        }
    } else {
        rest
    };
    let Some(inner) = after_tag.trim_start().strip_prefix('(') else {
        return Ok(None);
    };
    let Some(close) = inner.find(')') else {
        return Ok(None);
    };
    let args: Vec<&str> = inner[..close].split(',').map(str::trim).filter(|a| !a.is_empty()).collect();
    if args.len() != 4 {
        return Ok(None);
    }
    if tagged {
        return Err(format!("{name}: a gate with Deltakit's four leakage transport arguments takes no tag"));
    }
    for a in &args {
        match a.parse::<f64>() {
            Ok(p) if (0.0..=1.0).contains(&p) => {}
            _ => return Err(format!("{name}: '{a}' is not a probability")),
        }
    }
    // The probabilities written one way (exactly), so `0.10` and `0.1` are one tag.
    let kv: Vec<String> = TRANSPORT_KEYS.iter().zip(&args).map(|(k, v)| format!("{k}={}", ir::exact_arg(v.parse().expect("checked")))).collect();
    Ok(Some(format!("{indent}{name}[{TRANSPORT}({})]{}", kv.join(", "), &inner[close + 1..]).trim_end().to_string()))
}

/// A line of Deltakit's dialect (its comment stripped) as the tagged line it is read as; `None`
/// for any other line.
pub fn lower_line(line: &str) -> Result<Option<String>, String> {
    let trimmed = line.trim_start();
    let end = trimmed.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_')).unwrap_or(trimmed.len());
    let name = trimmed[..end].to_ascii_uppercase();
    let indent = &line[..line.len() - trimmed.len()];
    if TRANSPORT_GATES.contains(&name.as_str()) {
        return lower_transport(indent, &name, &trimmed[end..]);
    }
    if !NAMES.contains(&name.as_str()) {
        return Ok(None);
    }
    let mut rest = &trimmed[end..];
    if rest.starts_with('[') {
        return Err(format!("{name}: Deltakit's instruction takes no tag"));
    }
    let mut args: Vec<&str> = Vec::new();
    if let Some(inner) = rest.trim_start().strip_prefix('(') {
        let close = inner.find(')').ok_or_else(|| format!("{name}: unclosed '('"))?;
        args = inner[..close].split(',').map(str::trim).filter(|a| !a.is_empty()).collect();
        rest = &inner[close + 1..];
    } else if !rest.is_empty() && !rest.starts_with(char::is_whitespace) {
        return Err(format!("{name}: targets must be separated by spacing"));
    }
    for a in &args {
        match a.parse::<f64>() {
            Ok(p) if (0.0..=1.0).contains(&p) => {}
            _ => return Err(format!("{name}: '{a}' is not a probability")),
        }
    }
    let targets: Vec<&str> = rest.split_whitespace().collect();
    let qubit = |t: &str| -> Result<(u32, bool), String> {
        let (q, inverted) = match t.strip_prefix('!') {
            Some(q) => (q, true),
            None => (t, false),
        };
        match crate::circuit::uint::<u32>(q) {
            Some(q) if q < 1 << 24 => Ok((q, inverted)),
            _ => Err(format!("{name}: '{t}' is not a qubit")),
        }
    };
    let qubits = targets.iter().map(|t| qubit(t)).collect::<Result<Vec<_>, _>>()?;
    let list = |f: &dyn Fn(&(u32, bool)) -> String| qubits.iter().map(f).collect::<Vec<_>>().join(" ");
    if name == "HERALD_LEAKAGE_EVENT" {
        if args.len() > 1 {
            return Err(format!("{name} takes at most one argument, the probability of flipping its result"));
        }
        let flip = args.first().map(|a| format!("({a})")).unwrap_or_default();
        // Deltakit's herald reads 0 when its qubit is not leaked, an inverted target too (its
        // reference sample is 0 there): the inversion is kept in the tag for the text alone.
        let values = list(&|_| "0".into());
        return Ok(Some(format!("{indent}MPAD[{}]{flip} {values}", herald_tag(&qubits)).trim_end().to_string()));
    }
    if name == "RL" {
        if !args.is_empty() {
            return Err(format!("{name} takes no arguments"));
        }
        if qubits.iter().any(|q| q.1) {
            return Err(format!("{name}: a leakage reset's target is not inverted"));
        }
        return Ok(Some(format!("{indent}I[RL] {}", list(&|&(q, _)| q.to_string())).trim_end().to_string()));
    }
    if args.len() != 1 {
        return Err(format!("{name} takes one argument, its probability"));
    }
    if qubits.iter().any(|q| q.1) {
        return Err(format!("{name}: a noise channel's target is not inverted"));
    }
    let p = ir::exact_arg(args[0].parse().expect("checked"));
    Ok(Some(format!("{indent}I_ERROR[{name}(p={p})] {}", list(&|&(q, _)| q.to_string())).trim_end().to_string()))
}

/// The qubits a herald's tag names, each whether its target was inverted (`!q`), if `tag` is one.
pub fn herald_targets(tag: &str) -> Option<Result<Vec<(u32, bool)>, String>> {
    let inner = tag.trim().strip_prefix(HERALD)?.trim();
    let inner = inner.strip_prefix('(').and_then(|s| s.strip_suffix(')'))?;
    let qubits = inner.trim().strip_prefix("qubits")?.trim_start().strip_prefix('=')?;
    Some(
        qubits
            .split_whitespace()
            .map(|t| {
                let (q, inv) = t.strip_prefix('!').map_or((t, false), |q| (q, true));
                crate::circuit::uint::<u32>(q).filter(|&q| q < 1 << 24).map(|q| (q, inv)).ok_or_else(|| format!("{HERALD}: '{t}' is not a qubit"))
            })
            .collect(),
    )
}

/// The qubits a herald's tag names, if `tag` is one.
pub fn herald_qubits(tag: &str) -> Option<Result<Vec<u32>, String>> {
    Some(herald_targets(tag)?.map(|t| t.into_iter().map(|(q, _)| q).collect()))
}

/// A herald `MPAD`'s markers: one per padded record, the qubit it reads. `values` are the
/// pads (1 for an inverted target).
pub fn herald_ops(tag: &str, values: usize) -> Result<Option<Vec<NonPauli>>, String> {
    let Some(qubits) = herald_qubits(tag) else {
        return Ok(None);
    };
    let qubits = qubits?;
    if qubits.len() != values {
        return Err(format!("MPAD[{tag}]: {} qubits for {values} records", qubits.len()));
    }
    Ok(Some(qubits.into_iter().map(|qubit| NonPauli::Herald { qubit }).collect()))
}

/// Text with every lowered line written back in Deltakit's dialect.
pub fn to_deltakit(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for line in text.lines() {
        out.push_str(&raise_line(line).unwrap_or_else(|| line.to_string()));
        out.push('\n');
    }
    out
}

fn raise_line(line: &str) -> Option<String> {
    let trimmed = line.trim_start();
    let indent = &line[..line.len() - trimmed.len()];
    let code = crate::circuit::strip_comment(trimmed).trim_end();
    let (name, tag, args, targets) = crate::circuit::split_instruction(code).ok()?;
    let arg = ir::stim_arg;
    if TRANSPORT_GATES.contains(&name.as_str()) {
        let p = transport_args(tag)?.ok()?;
        let p: Vec<String> = p.iter().map(|&v| arg(v)).collect();
        return Some(format!("{indent}{name}({}) {}", p.join(", "), targets.join(" ")).trim_end().to_string());
    }
    match name.as_str() {
        "I" if tag.trim() == "RL" => Some(format!("{indent}RL {}", targets.join(" ")).trim_end().to_string()),
        "I_ERROR" => {
            // A line without targets is read on one, to name its channel.
            let qubits = if targets.is_empty() { vec![0] } else { targets.iter().map(|t| t.parse().ok()).collect::<Option<Vec<u32>>>()? };
            let ops = crate::nonpauli::parse("I_ERROR", tag, &qubits).ok()??;
            let (word, p) = match ops.first()? {
                NonPauli::Leakage { p, .. } => ("LEAKAGE", *p),
                NonPauli::Relax { p, .. } => ("RELAX", *p),
                _ => return None,
            };
            Some(format!("{indent}{word}({}) {}", arg(p), targets.join(" ")).trim_end().to_string())
        }
        "MPAD" => {
            let qubits = herald_targets(tag)?.ok()?;
            if qubits.len() != targets.len() {
                return None;
            }
            let flip = args.first().map(|&p| format!("({})", arg(p))).unwrap_or_default();
            // Inverted in the tag (or, as written before, by a pad of 1).
            let t: Vec<String> = qubits.iter().zip(&targets).map(|(&(q, inv), v)| format!("{}{q}", if inv || *v == "1" { "!" } else { "" })).collect();
            Some(format!("{indent}HERALD_LEAKAGE_EVENT{flip} {}", t.join(" ")).trim_end().to_string())
        }
        _ => None,
    }
}

fn targets_u32(op: &Instruction) -> Option<Vec<u32>> {
    op.targets.iter().map(|t| t.is_qubit().then(|| t.value())).collect()
}

/// Whether `op` is leakage noise (LEAK, SEEP, LEAK_TRANSPORT, LEAKAGE, RELAX).
fn is_leakage_noise(op: &Instruction) -> bool {
    if op.tag.is_empty() || !matches!(op.gate.name, "I" | "II" | "I_ERROR" | "II_ERROR") {
        return false;
    }
    let Some(t) = targets_u32(op) else {
        return false;
    };
    matches!(crate::nonpauli::parse(op.gate.name, &op.tag, &t), Ok(Some(ops)) if !ops.is_empty() && ops.iter().all(|o| o.is_leakage()))
}

fn is_herald(op: &Instruction) -> bool {
    op.gate.name == "MPAD" && herald_qubits(&op.tag).is_some()
}

/// The last records made, each whether it is a herald, as far back as any lookback reads.
struct Window {
    flags: VecDeque<bool>,
    cap: usize,
}

impl Window {
    fn push(&mut self, herald: bool, n: u64) {
        for _ in 0..n.min(self.cap as u64 + 1) {
            if self.flags.len() == self.cap {
                self.flags.pop_front();
            }
            self.flags.push_back(herald);
        }
    }

    /// Lookback `k` after removing heralds: `None` if it reads one.
    fn remap(&self, k: u32) -> Result<Option<u32>, String> {
        let k = k as usize;
        if k == 0 || k > self.flags.len() {
            return Err(format!("rec[-{k}] reaches before the first measurement"));
        }
        let at = self.flags.len() - k;
        if self.flags[at] {
            return Ok(None);
        }
        Ok(Some(self.flags.range(at..).filter(|&&h| !h).count() as u32))
    }
}

fn max_lookback(c: &ir::Circuit) -> u32 {
    c.items
        .iter()
        .map(|it| match it {
            Item::Op(op) => op.targets.iter().filter(|t| t.is_record()).map(|t| t.value()).max().unwrap_or(0),
            Item::Repeat { body, .. } => max_lookback(body),
        })
        .max()
        .unwrap_or(0)
}

fn records(c: &ir::Circuit) -> u64 {
    c.count_measurements()
}

fn strip(c: &ir::Circuit, w: &mut Window) -> Result<ir::Circuit, String> {
    let mut out = ir::Circuit::new();
    for it in &c.items {
        match it {
            Item::Op(op) if is_leakage_noise(op) => {}
            Item::Op(op) if is_herald(op) => w.push(true, op.count_measurement_results()),
            Item::Op(op) => {
                if let Some(op) = renumbered(op, w)? {
                    out.items.push(Item::Op(op));
                }
                w.push(false, op.count_measurement_results());
            }
            Item::Repeat { count, body, tag } => {
                let r = records(body);
                // From iteration `stable` on, every lookback stays inside the loop, so each
                // iteration is the same.
                let stable = if r == 0 { 1 } else { 1 + (w.cap as u64).div_ceil(r) };
                let runs = (*count).min(stable);
                let bodies = (0..runs).map(|_| strip(body, w)).collect::<Result<Vec<_>, _>>()?;
                let last = bodies.last().expect("a loop runs at least once");
                let first_same = bodies.iter().rposition(|b| b != last).map_or(0, |i| i + 1);
                for b in &bodies[..first_same] {
                    out.items.extend(b.items.iter().cloned());
                }
                let rest = count - first_same as u64;
                if !last.items.is_empty() {
                    if rest == 1 && tag.is_empty() {
                        out.items.extend(last.items.iter().cloned());
                    } else {
                        out.push_raw_repeat(rest, last.clone(), tag);
                    }
                }
            }
        }
    }
    Ok(out)
}

/// `op` with its lookbacks renumbered: a detector reading a herald is dropped, as are an
/// observable's herald records and a classically controlled pair on one.
fn renumbered(op: &Instruction, w: &Window) -> Result<Option<Instruction>, String> {
    if !op.targets.iter().any(|t| t.is_record()) {
        return Ok(Some(op.clone()));
    }
    let mut targets = Vec::with_capacity(op.targets.len());
    match op.gate.name {
        "DETECTOR" => {
            for t in &op.targets {
                match t.is_record() {
                    true => match w.remap(t.value())? {
                        Some(k) => targets.push(GateTarget::rec(k)),
                        None => return Ok(None),
                    },
                    false => targets.push(*t),
                }
            }
        }
        "OBSERVABLE_INCLUDE" => {
            for t in &op.targets {
                if !t.is_record() {
                    targets.push(*t);
                } else if let Some(k) = w.remap(t.value())? {
                    targets.push(GateTarget::rec(k));
                }
            }
        }
        _ => {
            // Classically controlled gates: pairs, one of them a record.
            for pair in op.targets.chunks(2) {
                let mut kept = Vec::with_capacity(2);
                let mut drop = false;
                for t in pair {
                    if t.is_record() {
                        match w.remap(t.value())? {
                            Some(k) => kept.push(GateTarget::rec(k)),
                            None => drop = true,
                        }
                    } else {
                        kept.push(*t);
                    }
                }
                if !drop {
                    targets.extend(kept);
                }
            }
            if targets.is_empty() {
                return Ok(None);
            }
        }
    }
    Ok(Some(Instruction { targets, ..op.clone() }))
}

/// The circuit with no leakage noise, no herald and no detector reading one (Deltakit's
/// deinstrumentation, loops kept).
pub fn without_leakage(c: &ir::Circuit) -> Result<ir::Circuit, String> {
    let mut w = Window { flags: VecDeque::new(), cap: max_lookback(c) as usize };
    strip(c, &mut w)
}

/// Where a circuit's heralds are, by absolute index.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct HeraldMap {
    /// The heralds' records, in order (Deltakit's leakage flags are these columns).
    pub herald_records: Vec<u64>,
    /// The detectors `without_leakage` keeps, in order.
    pub kept_detectors: Vec<u64>,
    /// Detectors that read exactly one herald and nothing else: (detector, its record).
    pub herald_detectors: Vec<(u64, u64)>,
}

/// The circuit's herald records and which of its detectors read them.
pub fn herald_map(c: &ir::Circuit) -> Result<HeraldMap, String> {
    let mut map = HeraldMap::default();
    let mut herald = Vec::<bool>::new();
    let mut detector = 0u64;
    let mut err = None;
    c.for_each_operation(&mut |op| {
        if err.is_some() {
            return;
        }
        let n = op.count_measurement_results();
        if is_herald(op) {
            for _ in 0..n {
                map.herald_records.push(herald.len() as u64);
                herald.push(true);
            }
            return;
        }
        if op.gate.name == "DETECTOR" {
            let mut recs = Vec::new();
            for t in op.targets.iter().filter(|t| t.is_record()) {
                let k = t.value() as usize;
                if k == 0 || k > herald.len() {
                    err = Some(format!("rec[-{k}] reaches before the first measurement"));
                    return;
                }
                recs.push(herald.len() - k);
            }
            let heralds: Vec<usize> = recs.iter().copied().filter(|&r| herald[r]).collect();
            if heralds.is_empty() {
                map.kept_detectors.push(detector);
            } else if recs.len() == 1 {
                map.herald_detectors.push((detector, heralds[0] as u64));
            }
            detector += 1;
        }
        herald.extend(std::iter::repeat_n(false, n as usize));
    });
    match err {
        Some(e) => Err(e),
        None => Ok(map),
    }
}

/// Leakage-aware decoding from heralds, around any decoder that weighs faults by their priors
/// (matching, union-find, BP+OSD, BP+LSD, Relay-BP): each trigger (a measurement that found its
/// qubit leaked, or a herald) names the faults its qubit's leakage makes likely, with their
/// probability given it; a shot whose triggers fire has those faults' priors raised to that
/// (combined with their own as independent causes), and is decoded once, the priors put back
/// after.
pub struct LeakageAware {
    pub dec: crate::reweight::PriorDecoder,
    triggers: Vec<Vec<(u32, f64)>>,
}

impl LeakageAware {
    pub fn new(dec: crate::reweight::PriorDecoder, triggers: Vec<Vec<(u32, f64)>>) -> Result<LeakageAware, String> {
        let n = dec.num_faults();
        for (t, faults) in triggers.iter().enumerate() {
            for &(f, q) in faults {
                if f as usize >= n {
                    return Err(format!("trigger {t} names fault {f}; the decoder has {n}"));
                }
                if !(0.0..=1.0).contains(&q) {
                    return Err(format!("trigger {t}: probability {q} is outside [0, 1]"));
                }
            }
        }
        Ok(LeakageAware { dec, triggers })
    }

    pub fn num_triggers(&self) -> usize {
        self.triggers.len()
    }

    /// The faults of each trigger.
    pub fn trigger_sizes(&self) -> Vec<usize> {
        self.triggers.iter().map(Vec::len).collect()
    }

    /// One shot: its detection events as sorted `defects` and one byte per detector, and the
    /// triggers that fired. The predicted observables (`None` where no correction explains the
    /// shot) and how many priors were raised.
    pub fn decode(&self, defects: &[u32], syndrome: &[u8], fired: &[u32], shot: u64, w: &mut crate::reweight::ArWork) -> (Option<u64>, u32) {
        let mut raised = 0u32;
        for &t in fired {
            for &(f, q) in &self.triggers[t as usize] {
                let p = self.dec.prior(w, f);
                self.dec.set(w, f, p * (1.0 - q) + q * (1.0 - p));
                raised += 1;
            }
        }
        let out = self.dec.round(w, defects, syndrome, self.dec.seed(shot)).map(|r| r.0);
        self.dec.restore(w);
        (out, raised)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ir(text: &str) -> ir::Circuit {
        ir::Circuit::parse(text).unwrap()
    }

    #[test]
    fn deltakit_lines_lower_and_raise() {
        assert_eq!(lower_line("LEAKAGE(0.01) 0 3").unwrap().unwrap(), "I_ERROR[LEAKAGE(p=0.01)] 0 3");
        assert_eq!(lower_line("  relax(0.5) 2").unwrap().unwrap(), "  I_ERROR[RELAX(p=0.5)] 2");
        assert_eq!(lower_line("HERALD_LEAKAGE_EVENT 0 !4").unwrap().unwrap(), "MPAD[HERALD_LEAKAGE_EVENT(qubits=0 !4)] 0 0");
        // Probabilities written one way, so equal circuits are equal.
        assert_eq!(lower_line("LEAKAGE(0.10) 0").unwrap(), lower_line("LEAKAGE(1e-1) 0").unwrap());
        assert_eq!(lower_line("CX(0.0, 0, 0.10, 0) 0 1").unwrap(), lower_line("CX(0, 0, 0.1, 0) 0 1").unwrap());
        assert_eq!(lower_line("HERALD_LEAKAGE_EVENT(0.02) 1").unwrap().unwrap(), "MPAD[HERALD_LEAKAGE_EVENT(qubits=1)](0.02) 0");
        assert_eq!(lower_line("M 0").unwrap(), None);
        assert_eq!(lower_line("LEAKAGES 0").unwrap(), None);
        assert!(lower_line("LEAKAGE 0").unwrap_err().contains("one argument"));
        assert!(lower_line("LEAKAGE(2) 0").unwrap_err().contains("probability"));
        assert!(lower_line("RELAX(0.1) !0").unwrap_err().contains("inverted"));
        assert!(lower_line("HERALD_LEAKAGE_EVENT[t] 0").unwrap_err().contains("no tag"));
        let text = "LEAKAGE(0.01) 0 3\nRELAX(0.5) 2\nHERALD_LEAKAGE_EVENT(0.02) 0 !4\nM 0\n";
        let lowered: String = text.lines().map(|l| lower_line(l).unwrap().unwrap_or(l.to_string()) + "\n").collect();
        assert_eq!(to_deltakit(&lowered), text);
    }

    #[test]
    fn heralds_fuse_by_joining_their_qubits() {
        // Two equal heralds are one of both: their tags are equal, but list one qubit each.
        let c = ir("MPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\nMPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\nMPAD[HERALD_LEAKAGE_EVENT(qubits=!1)] 0");
        assert_eq!(c.to_string(), "MPAD[HERALD_LEAKAGE_EVENT(qubits=0 0 !1)] 0 0 0");
        assert_eq!(to_deltakit(&c.to_string()), "HERALD_LEAKAGE_EVENT 0 0 !1\n");
        // A different flip probability does not fuse.
        assert_eq!(ir("MPAD[HERALD_LEAKAGE_EVENT(qubits=0)](0.1) 0\nMPAD[HERALD_LEAKAGE_EVENT(qubits=1)] 0").items.len(), 2);
    }

    #[test]
    fn without_leakage_renumbers_and_drops_heralds() {
        let c = ir("R 0 1\nI_ERROR[LEAKAGE(p=0.1)] 0\nM 0\nMPAD[HERALD_LEAKAGE_EVENT(qubits=1)] 0\nDETECTOR rec[-1]\nM 1\nDETECTOR rec[-1] rec[-3]\nOBSERVABLE_INCLUDE(0) rec[-1] rec[-2]\nCX rec[-2] 0 rec[-1] 1");
        let out = without_leakage(&c).unwrap();
        assert_eq!(out.to_string(), "R 0 1\nM 0\nM 1\nDETECTOR rec[-1] rec[-2]\nOBSERVABLE_INCLUDE(0) rec[-1]\nCX rec[-1] 1");
        let map = herald_map(&c).unwrap();
        assert_eq!(map, HeraldMap { herald_records: vec![1], kept_detectors: vec![1], herald_detectors: vec![(0, 1)] });
    }

    #[test]
    fn loops_are_kept_and_peeled_where_lookbacks_leave_them() {
        // Each round: a measurement, a herald, a detector against the last round's measurement.
        // The first round's reaches two measurements back before the loop, where no herald
        // sits between: rec[-4] is rec[-3] there, rec[-2] after; then one loop of the rest.
        let c = ir("M 0 0\nREPEAT 100 {\n    M 0\n    MPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\n    DETECTOR rec[-2] rec[-4]\n}");
        let out = without_leakage(&c).unwrap();
        assert_eq!(out.to_string(), "M 0 0\nM 0\nDETECTOR rec[-1] rec[-3]\nREPEAT 99 {\n    M 0\n    DETECTOR rec[-1] rec[-2]\n}");
        // With a herald before the loop too, nothing is peeled.
        let c = ir("M 0\nMPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\nREPEAT 100 {\n    M 0\n    MPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\n    DETECTOR rec[-2] rec[-4]\n}");
        assert_eq!(without_leakage(&c).unwrap().to_string(), "M 0\nREPEAT 100 {\n    M 0\n    DETECTOR rec[-1] rec[-2]\n}");
        // Loops with nothing to renumber stay loops.
        let c = ir("REPEAT 5 {\n    M 0\n    DETECTOR rec[-1]\n    I_ERROR[RELAX(p=0.1)] 0\n}");
        assert_eq!(without_leakage(&c).unwrap().to_string(), "REPEAT 5 {\n    M 0\n    DETECTOR rec[-1]\n}");
        assert_eq!(herald_map(&ir("REPEAT 3 {\n    MPAD[HERALD_LEAKAGE_EVENT(qubits=0)] 0\n    M 0\n    DETECTOR rec[-2]\n}")).unwrap().herald_detectors, vec![(0, 0), (1, 2), (2, 4)]);
    }
}