deq-runtime 0.3.0

deq: Real-time Quantum Error Correction Decoding System
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
//! A sampler that drives `stim::TableauSimulator` directly with
//! retry-from-BEGIN semantics for preselect checks.
//!
//! Instead of sampling the full circuit and filtering, this sampler
//! splits the circuit at `#!preselect_begin` boundaries and replays
//! only the failing segment on preselect failure.

use crate::misc::bit_vector;
use crate::simulator::DeterministicRng;
use crate::simulator::common::{ErrorSet, Sampler};
use crate::util::BitVector;

/// A sampler that uses `stim::TableauSimulator` with retry-from-BEGIN.
///
/// A background thread parses the Stim text, splits it into segments
/// at `#!preselect_begin` markers, and for each shot drives the
/// `TableauSimulator` segment by segment.  When a `#!preselect_expect`
/// check fails, the simulator replays from the most recent BEGIN.
///
/// Since `stim::Circuit` and `stim::TableauSimulator` are `!Send`,
/// everything runs inside the background thread.
pub struct TableauPreselectSampler {
    receiver: std::sync::Mutex<std::sync::mpsc::Receiver<Vec<bool>>>,
    request: Option<std::sync::mpsc::SyncSender<()>>,
    total_retries: std::sync::Arc<std::sync::atomic::AtomicU64>,
}

/// A preselect check parsed from the Stim text.
struct SegmentCheck {
    /// Absolute measurement index in the full circuit.
    abs_meas_idx: usize,
    expected: bool,
}

/// A contiguous block between two `#!preselect_begin` markers.
///
/// Split into a *retry* portion (BEGIN → last EXPECT) and a *tail*
/// (last EXPECT → next BEGIN).  Only the retry portion is re-executed
/// on check failure; the tail runs once after all checks pass.
struct Segment {
    /// Circuit text for the retryable portion (up to the last check).
    /// Empty when the segment has no checks.
    retry_stim_text: String,
    /// Checks verified after executing the retry portion.
    checks: Vec<SegmentCheck>,
    /// Circuit text after the last check — entangles with data qubits
    /// and must execute exactly once.
    tail_stim_text: String,
}

/// Parse the full Stim circuit text into segments.
fn parse_segments(stim_text: &str) -> Vec<Segment> {
    let mut segments: Vec<Segment> = Vec::new();
    let mut current_lines: Vec<&str> = Vec::new();
    let mut current_checks: Vec<SegmentCheck> = Vec::new();
    let mut last_check_line_pos: Option<usize> = None;

    for line in stim_text.lines() {
        let trimmed = line.trim();

        if trimmed == "#!preselect_begin" {
            flush_segment(
                &mut segments,
                &mut current_lines,
                &mut current_checks,
                &mut last_check_line_pos,
            );
            continue;
        }

        if let Some(rest) = trimmed.strip_prefix("#!preselect_expect") {
            let rest = rest.trim();
            let mut parts = rest.split_whitespace();
            let abs_idx: usize = parts
                .next()
                .and_then(|s| s.parse().ok())
                .expect("invalid #!preselect_expect abs_meas_idx");
            let expected_int: u8 = parts
                .next()
                .and_then(|s| s.parse().ok())
                .expect("invalid #!preselect_expect expected value");
            current_checks.push(SegmentCheck {
                abs_meas_idx: abs_idx,
                expected: expected_int != 0,
            });
            last_check_line_pos = Some(current_lines.len());
            continue;
        }

        if trimmed.starts_with("#!") {
            continue;
        }

        current_lines.push(line);
    }

    flush_segment(
        &mut segments,
        &mut current_lines,
        &mut current_checks,
        &mut last_check_line_pos,
    );
    segments
}

fn flush_segment(
    segments: &mut Vec<Segment>,
    lines: &mut Vec<&str>,
    checks: &mut Vec<SegmentCheck>,
    last_check_pos: &mut Option<usize>,
) {
    let (retry, tail) = match *last_check_pos {
        Some(pos) => (lines[..pos].join("\n"), lines[pos..].join("\n")),
        None => (String::new(), lines.join("\n")),
    };
    segments.push(Segment {
        retry_stim_text: retry,
        checks: std::mem::take(checks),
        tail_stim_text: tail,
    });
    lines.clear();
    *last_check_pos = None;
}

impl TableauPreselectSampler {
    pub fn new(circuit_text: &str, seed: u64, skip_shots: usize, strict_timing: bool, max_attempts: u64) -> Self {
        let (tx, rx) = std::sync::mpsc::sync_channel::<Vec<bool>>(if strict_timing { 0 } else { 16 });
        let circuit_text = circuit_text.to_owned();

        let total_retries = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
        let retries_ref = total_retries.clone();

        let (request, request_rx) = if strict_timing {
            let (req_tx, req_rx) = std::sync::mpsc::sync_channel::<()>(0);
            (Some(req_tx), Some(req_rx))
        } else {
            (None, None)
        };

        std::thread::Builder::new()
            .name("tableau-preselect-sampler".into())
            .spawn(move || {
                let segments = parse_segments(&circuit_text);
                let parsed: Vec<(stim::Circuit, stim::Circuit)> = segments
                    .iter()
                    .map(|seg| {
                        let retry = seg
                            .retry_stim_text
                            .parse::<stim::Circuit>()
                            .expect("Failed to parse retry segment");
                        let tail = seg
                            .tail_stim_text
                            .parse::<stim::Circuit>()
                            .expect("Failed to parse tail segment");
                        (retry, tail)
                    })
                    .collect();

                let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(seed);

                for shot in 0u64.. {
                    let sim_seed = rand::Rng::next_u64(&mut rng);
                    if shot < skip_shots as u64 {
                        continue;
                    }

                    if let Some(ref req_rx) = request_rx
                        && req_rx.recv().is_err()
                    {
                        break;
                    }

                    let measurements = run_one_shot(&segments, &parsed, sim_seed, max_attempts, &retries_ref);

                    if tx.send(measurements).is_err() {
                        break;
                    }
                }
            })
            .expect("Failed to spawn tableau preselect sampler thread");

        Self {
            receiver: std::sync::Mutex::new(rx),
            request,
            total_retries,
        }
    }
}

/// Execute one shot with retry-from-BEGIN semantics.
///
/// A single `TableauSimulator` is kept for the entire shot.  We
/// maintain `accepted_indices` — a mapping from nominal measurement
/// index (as the circuit expects) to actual index in the simulator's
/// growing record (which includes junk from failed retries).  Every
/// `rec[-k]` target is remapped through this table so that references
/// across segments stay correct.
fn run_one_shot(
    segments: &[Segment],
    parsed: &[(stim::Circuit, stim::Circuit)],
    initial_seed: u64,
    max_attempts: u64,
    total_retries: &std::sync::atomic::AtomicU64,
) -> Vec<bool> {
    let mut sim = stim::TableauSimulator::with_seed(initial_seed);
    let mut accepted_indices: Vec<usize> = Vec::new();

    for (seg_idx, segment) in segments.iter().enumerate() {
        let (retry_circuit, tail_circuit) = &parsed[seg_idx];

        if segment.checks.is_empty() {
            let new = execute_with_mapping(&mut sim, tail_circuit, &accepted_indices);
            accepted_indices.extend(new);
        } else {
            let mut attempts = 0u64;
            loop {
                let new = execute_with_mapping(&mut sim, retry_circuit, &accepted_indices);

                let record = sim.current_measurement_record();
                let all_pass = segment.checks.iter().all(|check| {
                    let idx_in_seg = check.abs_meas_idx - accepted_indices.len();
                    idx_in_seg < new.len() && record[new[idx_in_seg]] == check.expected
                });

                if all_pass {
                    accepted_indices.extend(new);
                    break;
                }

                attempts += 1;
                total_retries.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

                if attempts >= max_attempts {
                    panic!(
                        "Preselect retry limit ({max_attempts}) exceeded at segment {seg_idx}. \
                         This usually means the retry region interacts with data qubits \
                         whose state is corrupted by failed attempts. Use --simulator static \
                         (resample mode) instead of --simulator preselect for this circuit."
                    );
                }
            }

            // Tail: execute once after checks pass.
            let new = execute_with_mapping(&mut sim, tail_circuit, &accepted_indices);
            accepted_indices.extend(new);
        }
    }

    let full_record = sim.current_measurement_record();
    accepted_indices.iter().map(|&i| full_record[i]).collect()
}

/// Execute `circuit` on `sim`, remapping every `rec[-k]` target through
/// the nominal→actual index mapping in `accepted`.  Returns the actual
/// record indices of the measurements produced by this execution.
fn execute_with_mapping(sim: &mut stim::TableauSimulator, circuit: &stim::Circuit, accepted: &[usize]) -> Vec<usize> {
    let mut new_actual: Vec<usize> = Vec::new();
    let nominal_base = accepted.len();

    for item in circuit {
        match item {
            stim::CircuitItem::Instruction(inst) => {
                let has_rec = inst.targets().iter().any(|t| t.is_measurement_record_target());

                let before = sim.current_measurement_record().len();

                if has_rec {
                    let nominal_now = nominal_base + new_actual.len();

                    let new_targets: Vec<stim::GateTarget> = inst
                        .targets()
                        .iter()
                        .map(|&t| {
                            if t.is_measurement_record_target() {
                                let k = (-t.value()) as usize;
                                let nominal_idx = nominal_now.checked_sub(k).expect("rec target underflow");
                                let actual_idx = if nominal_idx >= nominal_base {
                                    new_actual[nominal_idx - nominal_base]
                                } else {
                                    accepted[nominal_idx]
                                };
                                let new_k = before - actual_idx;
                                stim::GateTarget::rec(-(new_k as i32)).unwrap()
                            } else {
                                t
                            }
                        })
                        .collect();
                    let remapped = stim::CircuitInstruction::new(
                        inst.gate(),
                        new_targets,
                        inst.gate_args().iter().copied(),
                        inst.tag(),
                    )
                    .unwrap();
                    sim.do_circuit(&remapped.to_string().parse::<stim::Circuit>().unwrap());
                } else {
                    sim.do_circuit(&inst.to_string().parse::<stim::Circuit>().unwrap());
                }

                let after = sim.current_measurement_record().len();
                new_actual.extend(before..after);
            }
            stim::CircuitItem::RepeatBlock(_) => {
                panic!(
                    "The preselect simulator does not support REPEAT blocks. \
                     Use --simulator static (resample mode) for circuits with REPEAT."
                );
            }
        }
    }

    new_actual
}

impl Sampler for TableauPreselectSampler {
    fn sample(&self, _rng: &mut DeterministicRng) -> ErrorSet {
        if let Some(ref req) = self.request {
            req.send(()).expect("Tableau preselect sampling thread stopped unexpectedly");
        }
        let rx = self.receiver.lock().unwrap();
        let measurements_bool = rx.recv().expect("Tableau preselect sampling thread stopped unexpectedly");
        ErrorSet {
            errors: vec![],
            measurements: BitVector {
                size: measurements_bool.len() as u64,
                data: bit_vector::pack_bits(&measurements_bool),
            },
            expected_readouts: BitVector { size: 0, data: vec![] },
        }
    }

    fn sample_single_error(&self, _index: usize) -> ErrorSet {
        unimplemented!("single error iteration not supported for preselect sampler")
    }

    fn count_single_error(&self) -> usize {
        0
    }

    fn readouts_match(&self, actual: &BitVector, expected: &BitVector) -> bool {
        actual == expected
    }

    fn error_tag(&self, _marginal_index: usize, _error_index: usize) -> &str {
        ""
    }

    fn filtered_count(&self) -> u64 {
        self.total_retries.load(std::sync::atomic::Ordering::Relaxed)
    }
}

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

    #[test]
    fn parse_segments_no_directives() {
        let segments = parse_segments("H 0\nM 0\n");
        assert_eq!(segments.len(), 1);
        assert!(segments[0].checks.is_empty());
        assert!(segments[0].retry_stim_text.is_empty());
        assert!(segments[0].tail_stim_text.contains("H 0"));
    }

    #[test]
    fn parse_segments_with_begin_and_checks() {
        let text = "\
R 0
H 0
M 0
#!preselect_begin
H 0
M 0
#!preselect_expect 1 0
CZ 0 1
";
        let segments = parse_segments(text);
        assert_eq!(segments.len(), 2);
        assert!(segments[0].checks.is_empty());
        assert_eq!(segments[1].checks.len(), 1);
        assert_eq!(segments[1].checks[0].abs_meas_idx, 1);
        assert!(segments[1].retry_stim_text.contains("M 0"));
        assert!(segments[1].tail_stim_text.contains("CZ 0 1"));
    }

    #[test]
    fn basic_preselect_sampling() {
        let text = "\
R 0
M 0
#!preselect_begin
H 0
M 0
#!preselect_expect 1 0
";
        let sampler = TableauPreselectSampler::new(text, 42, 0, false, 1_000_000);
        let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(123);
        let sample = sampler.sample(&mut rng);
        let bits = bit_vector::unpack_bits(&sample.measurements.data, sample.measurements.size);
        assert!(!bits[0]);
        assert!(!bits[1]);
    }

    #[test]
    fn preselect_reports_retries() {
        let text = "\
#!preselect_begin
H 0
M 0
#!preselect_expect 0 0
";
        let sampler = TableauPreselectSampler::new(text, 77, 0, false, 1_000_000);
        let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(0);
        for _ in 0..10 {
            let sample = sampler.sample(&mut rng);
            let bits = bit_vector::unpack_bits(&sample.measurements.data, sample.measurements.size);
            assert!(!bits[0], "preselect should enforce measurement == 0");
        }
        let retries = sampler.filtered_count();
        assert!(retries > 0, "10 shots of 50/50 should produce some retries, got 0");
    }

    #[test]
    fn no_preselect_passthrough() {
        let text = "X 0\nM 0\n";
        let sampler = TableauPreselectSampler::new(text, 42, 0, false, 1_000_000);
        let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(0);
        let sample = sampler.sample(&mut rng);
        let bits = bit_vector::unpack_bits(&sample.measurements.data, sample.measurements.size);
        assert!(bits[0], "X|0> then M should give 1");
        assert_eq!(sampler.filtered_count(), 0);
    }

    #[test]
    fn deterministic_measurement_no_retry() {
        let text = "\
#!preselect_begin
R 0
M 0
#!preselect_expect 0 0
";
        let sampler = TableauPreselectSampler::new(text, 42, 0, false, 1_000_000);
        let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(0);
        for _ in 0..10 {
            sampler.sample(&mut rng);
        }
        assert_eq!(sampler.filtered_count(), 0, "deterministic circuit should never retry");
    }

    #[test]
    fn many_shots_consistent() {
        let text = "\
R 0
#!preselect_begin
H 0
M 0
#!preselect_expect 0 0
H 0
M 0
";
        let sampler = TableauPreselectSampler::new(text, 123, 0, false, 1_000_000);
        let mut rng = <crate::simulator::DeterministicRng as rand::SeedableRng>::seed_from_u64(0);
        for shot in 0..50 {
            let sample = sampler.sample(&mut rng);
            let bits = bit_vector::unpack_bits(&sample.measurements.data, sample.measurements.size);
            assert_eq!(bits.len(), 2, "should have 2 measurements");
            assert!(!bits[0], "shot {shot}: preselected meas 0 should be 0");
        }
    }
}