rstim 0.4.0

Stim-aligned stabilizer circuit simulator and detector error model toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! Bounded one-pass affine detector predicates for explicit eager postselection.
use super::super::*;

#[derive(Clone, Copy, Debug, Default)]
struct Input {
    first: usize,
    count: usize,
    active: bool,
    contributes: bool,
    // Immutable category-level support; at most 15 Noise choices per input.
    contributing_categories: u16,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct PacketInput {
    event: usize,
    ordinal: usize,
}

struct Quota {
    bytes: usize,
    limit: usize,
    work: usize,
}
impl Quota {
    // Monotone accounting includes all allocated vector payloads and headers,
    // including dropped scratch. Allocator metadata/reallocation transients are
    // not measured; every vector here has just one fallible exact reservation.
    fn zeros<T: Default + Clone>(&mut self, n: usize) -> Option<Vec<T>> {
        self.tick(n)?;
        let header = size_of::<Vec<T>>();
        if self
            .bytes
            .checked_add(header)?
            .checked_add(n.checked_mul(size_of::<T>())?)?
            > self.limit
        {
            return None;
        }
        let mut v = Vec::new();
        v.try_reserve_exact(n).ok()?;
        self.bytes = self
            .bytes
            .checked_add(header)?
            .checked_add(v.capacity().checked_mul(size_of::<T>())?)?;
        if self.bytes > self.limit {
            return None;
        }
        v.resize(n, T::default());
        Some(v)
    }
    fn tick(&mut self, n: usize) -> Option<()> {
        self.work = self.work.checked_sub(n)?;
        Some(())
    }
}

struct Matrix {
    data: Vec<u64>,
    words: usize,
    rows: usize,
}
impl Matrix {
    fn new(rows: usize, words: usize, q: &mut Quota) -> Option<Self> {
        Some(Self {
            data: q.zeros(rows.checked_mul(words)?)?,
            words,
            rows,
        })
    }
    fn range(&self, row: usize) -> Option<std::ops::Range<usize>> {
        if row >= self.rows {
            return None;
        }
        let start = row.checked_mul(self.words)?;
        Some(start..start.checked_add(self.words)?)
    }
    fn row(&self, row: usize) -> Option<&[u64]> {
        self.data.get(self.range(row)?)
    }
    fn row_mut(&mut self, row: usize) -> Option<&mut [u64]> {
        let range = self.range(row)?;
        self.data.get_mut(range)
    }
    fn toggle(&mut self, row: usize, variable: usize, q: &mut Quota) -> Option<()> {
        q.tick(1)?;
        let words = self.words;
        let r = self.row_mut(row)?;
        if variable / 64 >= words {
            return None;
        }
        r[variable / 64] ^= 1u64 << (variable % 64);
        Some(())
    }
    fn xor_rows(&mut self, to: usize, from: usize, q: &mut Quota) -> Option<()> {
        let a = self.range(to)?;
        let b = self.range(from)?;
        q.tick(self.words)?;
        for k in 0..self.words {
            let value = self.data[b.start + k];
            self.data[a.start + k] ^= value;
        }
        Some(())
    }
}
fn xor(to: &mut [u64], from: &[u64], q: &mut Quota) -> Option<()> {
    if to.len() != from.len() {
        return None;
    }
    q.tick(to.len())?;
    for (a, b) in to.iter_mut().zip(from) {
        *a ^= b;
    }
    Some(())
}
fn support(
    bits: &[u64],
    qubits: usize,
    q: &mut Quota,
    mut visit: impl FnMut(usize, &mut Quota) -> Option<()>,
) -> Option<()> {
    if bits.len() != qubits.checked_add(63)? / 64 {
        return None;
    }
    q.tick(bits.len())?;
    for (word, &bits) in bits.iter().enumerate() {
        let mut bits = bits;
        while bits != 0 {
            q.tick(1)?;
            let i = word
                .checked_mul(64)?
                .checked_add(bits.trailing_zeros() as usize)?;
            if i >= qubits {
                return None;
            }
            visit(i, q)?;
            bits &= bits - 1;
        }
    }
    Some(())
}
fn conjugate(g: BasisGate, x: &mut Matrix, z: &mut Matrix, q: &mut Quota) -> Option<()> {
    q.tick(1)?;
    match g {
        BasisGate::H(a) => {
            q.tick(x.words)?;
            for (a, b) in x.row_mut(a)?.iter_mut().zip(z.row_mut(a)?) {
                std::mem::swap(a, b);
            }
        }
        BasisGate::S(a) => xor(z.row_mut(a)?, x.row(a)?, q)?,
        BasisGate::CX(a, b) => {
            if a == b {
                return None;
            }
            x.xor_rows(b, a, q)?;
            z.xor_rows(a, b, q)?;
        }
        BasisGate::CZ(a, b) => {
            if a == b {
                return None;
            }
            xor(z.row_mut(a)?, x.row(b)?, q)?;
            xor(z.row_mut(b)?, x.row(a)?, q)?;
        }
    }
    Some(())
}
fn apply(
    p: &PackedPauli,
    value: &[u64],
    x: &mut Matrix,
    z: &mut Matrix,
    q: &mut Quota,
) -> Option<()> {
    if p.phase >= 4 {
        return None;
    }
    support(&p.x, x.rows, q, |i, q| xor(x.row_mut(i)?, value, q))?;
    support(&p.z, z.rows, q, |i, q| xor(z.row_mut(i)?, value, q))
}

#[derive(Debug)]
pub(in super::super) struct ActiveFreePredicate {
    eligible: Vec<usize>,
    inputs: Vec<Input>,
    packet_inputs: Vec<PacketInput>,
    sweeps: Vec<u32>,
    sweep_first: usize,
    variables: usize,
    words: usize,
    columns: Vec<u64>,
}
impl ActiveFreePredicate {
    pub(in super::super) fn build(
        plan: &CompiledNearCliffordExecutor,
        byte_limit: usize,
        work_limit: usize,
    ) -> Option<Self> {
        let mut quota = Quota {
            bytes: size_of::<Self>(),
            limit: byte_limit,
            work: work_limit,
        };
        quota.tick(plan.operations.len().checked_add(plan.random_kinds.len())?)?;
        let detector_count = plan
            .operations
            .iter()
            .filter(|o| {
                matches!(
                    o,
                    PlanOp::Annotation {
                        observable: None,
                        ..
                    }
                )
            })
            .count();
        if detector_count == 0 {
            return None;
        }
        let sweep_count = plan
            .operations
            .iter()
            .filter(|o| {
                matches!(
                    o,
                    PlanOp::Feedback {
                        condition: Condition::Sweep(_),
                        ..
                    }
                )
            })
            .count();
        let mut sweeps = quota.zeros::<u32>(sweep_count)?;
        let mut s = 0;
        for op in &plan.operations {
            if let PlanOp::Feedback {
                condition: Condition::Sweep(index),
                ..
            } = op
            {
                sweeps[s] = *index;
                s += 1;
            }
        }
        // Bound sort comparisons conservatively as n*(ceil(log2(n))+1).
        let sort_depth = usize::BITS as usize - sweep_count.leading_zeros() as usize + 1;
        quota.tick(sweep_count.checked_mul(sort_depth)?)?;
        sweeps.sort_unstable();
        sweeps.dedup();
        let mut inputs = quota.zeros::<Input>(plan.random_kinds.len())?;
        let mut variables = 1usize; // Variable zero is the affine constant one.
        for (input, kind) in inputs.iter_mut().zip(&plan.random_kinds) {
            let (count, active) = match *kind {
                RandomKind::Independent => (1, false),
                RandomKind::Active => (1, true),
                RandomKind::Noise {
                    probability,
                    choices,
                } => {
                    if !probability.is_finite()
                        || !(0. ..=1.).contains(&probability)
                        || choices == 0
                        || choices > 15
                    {
                        return None;
                    }
                    (choices, false)
                }
            };
            *input = Input {
                first: variables,
                count,
                active,
                contributes: false,
                contributing_categories: 0,
            };
            variables = variables.checked_add(count)?;
        }
        let sweep_first = variables;
        variables = variables.checked_add(sweeps.len())?;
        let words = variables.checked_add(63)? / 64;
        let mut active = quota.zeros::<u64>(words)?;
        for input in &inputs {
            if input.active {
                active[input.first / 64] |= 1 << (input.first % 64);
            }
        }
        let mut x = Matrix::new(plan.num_qubits, words, &mut quota)?;
        let mut z = Matrix::new(plan.num_qubits, words, &mut quota)?;
        let mut records = Matrix::new(plan.measurement_count, words, &mut quota)?;
        let mut expressions = Matrix::new(detector_count, words, &mut quota)?;
        let mut eligible = quota.zeros::<usize>(detector_count)?;
        let mut anti = quota.zeros::<u64>(words)?;
        let mut branch = quota.zeros::<u64>(words)?;
        let mut physical = quota.zeros::<u64>(words)?;
        let mut value = quota.zeros::<u64>(words)?;
        if let Some(signs) = &plan.noise_signs {
            quota.tick(signs.offsets.len())?;
            if signs.offsets.len() != plan.operations.len().checked_add(1)?
                || signs.offsets.first() != Some(&0)
                || signs.offsets.last() != Some(&signs.refs.len())
                || signs.offsets.windows(2).any(|r| r[0] > r[1])
            {
                return None;
            }
        }
        let (mut event, mut detector, mut selected) = (0, 0, 0);
        for (node, op) in plan.operations.iter().enumerate().skip(plan.prefix_len) {
            quota.tick(1)?;
            match op {
                PlanOp::Rotate { .. } => {} // Changes only the opaque branch probabilities.
                PlanOp::Basis(gates) => {
                    for &g in gates {
                        conjugate(g, &mut x, &mut z, &mut quota)?;
                    }
                }
                PlanOp::Noise {
                    probability,
                    choices,
                } => {
                    if !matches!(plan.random_kinds.get(event),Some(RandomKind::Noise { probability:p,choices:c }) if p.to_bits()==probability.to_bits() && *c==choices.len())
                    {
                        return None;
                    }
                    let input = inputs[event];
                    for (category, choice) in choices.iter().enumerate() {
                        let variable = input.first.checked_add(category)?;
                        support(&choice.pauli.x, plan.num_qubits, &mut quota, |i, q| {
                            x.toggle(i, variable, q)
                        })?;
                        support(&choice.pauli.z, plan.num_qubits, &mut quota, |i, q| {
                            z.toggle(i, variable, q)
                        })?;
                        for &r in &choice.record_flips {
                            records.toggle(r, variable, &mut quota)?;
                        }
                    }
                    event += 1;
                }
                PlanOp::Feedback { condition, pauli } => {
                    quota.tick(words)?;
                    value.fill(0);
                    match condition {
                        Condition::Record(r) => xor(&mut value, records.row(*r)?, &mut quota)?,
                        Condition::Sweep(index) => {
                            quota.tick(sort_depth)?;
                            let variable =
                                sweep_first.checked_add(sweeps.binary_search(index).ok()?)?;
                            value[variable / 64] = 1 << (variable % 64);
                        }
                    }
                    apply(pauli, &value, &mut x, &mut z, &mut quota)?;
                }
                PlanOp::Measure(m) => {
                    quota.tick(words.checked_mul(2)?)?;
                    anti.fill(0);
                    branch.fill(0);
                    support(&m.pauli.physical.z, plan.num_qubits, &mut quota, |i, q| {
                        xor(&mut anti, x.row(i)?, q)
                    })?;
                    support(&m.pauli.physical.x, plan.num_qubits, &mut quota, |i, q| {
                        xor(&mut anti, z.row(i)?, q)
                    })?;
                    if let Some(signs) = &plan.noise_signs {
                        for reference in &signs.refs[signs.offsets[node]..signs.offsets[node + 1]] {
                            quota.tick(1)?;
                            let input = *inputs.get(reference.event)?;
                            if !matches!(plan.random_kinds.get(reference.event),Some(RandomKind::Noise { choices,.. }) if *choices==usize::from(reference.choices))
                            {
                                return None;
                            }
                            for category in 0..input.count {
                                quota.tick(1)?;
                                if reference.mask >> category & 1 != 0 {
                                    let variable = input.first + category;
                                    anti[variable / 64] ^= 1 << (variable % 64);
                                }
                            }
                        }
                    }
                    match m.projection {
                        Projection::Constant(bit) => branch[0] = u64::from(bit),
                        Projection::Independent { .. } | Projection::Active { .. } => {
                            let input = *inputs.get(event)?;
                            if !matches!(
                                (&m.projection, plan.random_kinds.get(event)),
                                (
                                    Projection::Independent { .. },
                                    Some(RandomKind::Independent)
                                ) | (Projection::Active { .. }, Some(RandomKind::Active))
                            ) {
                                return None;
                            }
                            branch[input.first / 64] = 1 << (input.first % 64);
                            event += 1;
                        }
                    }
                    quota.tick(words)?;
                    physical.copy_from_slice(&anti);
                    xor(&mut physical, &branch, &mut quota)?;
                    for &g in &m.basis {
                        conjugate(g, &mut x, &mut z, &mut quota)?;
                    }
                    match m.projection {
                        Projection::Constant(_) => {}
                        Projection::Independent { pivot } => {
                            xor(x.row_mut(pivot)?, &branch, &mut quota)?
                        }
                        Projection::Active { pivot, offset, .. } => {
                            xor(x.row_mut(pivot)?, &branch, &mut quota)?;
                            if offset {
                                x.toggle(pivot, 0, &mut quota)?;
                            }
                        }
                    }
                    if let Some(reset) = &m.reset {
                        apply(reset, &physical, &mut x, &mut z, &mut quota)?;
                    }
                    if let Some(r) = m.record {
                        quota.tick(words)?;
                        let dest = records.row_mut(r)?;
                        dest.copy_from_slice(&physical);
                        if m.inverted {
                            dest[0] ^= 1;
                        }
                        if m.readout > 0. {
                            if !matches!(plan.random_kinds.get(event),Some(RandomKind::Noise{probability,choices:1}) if probability.to_bits()==m.readout.to_bits())
                            {
                                return None;
                            }
                            records.toggle(r, inputs.get(event)?.first, &mut quota)?;
                            event += 1;
                        }
                    }
                }
                PlanOp::Annotation {
                    offsets,
                    observable: None,
                } => {
                    quota.tick(words)?;
                    value.fill(0);
                    for &r in offsets {
                        xor(&mut value, records.row(r)?, &mut quota)?;
                    }
                    quota.tick(words)?;
                    if !value.iter().zip(&active).any(|(v, a)| v & a != 0) {
                        quota.tick(words)?;
                        expressions.row_mut(selected)?.copy_from_slice(&value);
                        eligible[selected] = detector;
                        selected += 1;
                    }
                    detector += 1;
                }
                PlanOp::Annotation {
                    observable: Some(_),
                    ..
                } => {}
            }
        }
        if event != inputs.len() || detector != detector_count || selected == 0 {
            return None;
        }
        eligible.truncate(selected);
        let output_words = selected.checked_add(63)? / 64;
        let mut columns = quota.zeros::<u64>(variables.checked_mul(output_words)?)?;
        for output in 0..selected {
            let row = expressions.row(output)?;
            quota.tick(words)?;
            for (word, &bits) in row.iter().enumerate() {
                let mut bits = bits;
                while bits != 0 {
                    quota.tick(1)?;
                    let variable = word * 64 + bits.trailing_zeros() as usize;
                    if variable >= variables {
                        return None;
                    }
                    columns[variable * output_words + output / 64] ^= 1 << (output % 64);
                    bits &= bits - 1;
                }
            }
        }
        Self::mark_contributing_inputs(&mut inputs, output_words, &columns, &mut quota)?;
        let packet_inputs = Self::index_packet_inputs(&inputs, &plan.random_kinds, &mut quota)?;
        Some(Self {
            eligible,
            inputs,
            packet_inputs,
            sweeps,
            sweep_first,
            variables,
            words: output_words,
            columns,
        })
    }
    fn mark_contributing_inputs(
        inputs: &mut [Input],
        words: usize,
        columns: &[u64],
        quota: &mut Quota,
    ) -> Option<()> {
        for input in inputs {
            let first = input.first.checked_mul(words)?;
            let end = input.first.checked_add(input.count)?.checked_mul(words)?;
            let effects = columns.get(first..end)?;
            quota.tick(effects.len().checked_add(1)?)?;
            if input.count > 15 {
                return None;
            }
            let mut categories = 0u16;
            for (offset, &bits) in effects.iter().enumerate() {
                if bits != 0 {
                    // A nonempty effects slice implies words > 0. Every
                    // category is within the checked input range above.
                    categories |= 1u16 << (offset / words);
                }
            }
            input.contributing_categories = categories;
            input.contributes = categories != 0;
        }
        Some(())
    }
    fn column(&self, variable: usize) -> Option<&[u64]> {
        if variable >= self.variables {
            return None;
        }
        self.columns
            .get(variable * self.words..(variable + 1) * self.words)
    }

    fn index_packet_inputs(
        inputs: &[Input],
        kinds: &[RandomKind],
        quota: &mut Quota,
    ) -> Option<Vec<PacketInput>> {
        if inputs.len() != kinds.len() {
            return None;
        }
        quota.tick(inputs.len())?;
        let count = inputs.iter().filter(|input| input.contributes).count();
        let mut indexed = quota.zeros::<PacketInput>(count)?;
        quota.tick(inputs.len())?;
        let (mut noise, mut independent, mut next) = (0usize, 0usize, 0usize);
        for (event, (input, kind)) in inputs.iter().zip(kinds).enumerate() {
            // Ordinals belong to the complete producer, including events with
            // zero detector effect. Retain their original index in each sidecar.
            let ordinal = match kind {
                RandomKind::Noise { .. } => {
                    let ordinal = noise;
                    noise = noise.checked_add(1)?;
                    ordinal
                }
                RandomKind::Independent => {
                    let ordinal = independent;
                    independent = independent.checked_add(1)?;
                    ordinal
                }
                RandomKind::Active => 0,
            };
            if input.contributes {
                indexed[next] = PacketInput { event, ordinal };
                next += 1;
            }
        }
        Some(indexed)
    }
}

fn packet_fail_mask<'a>(
    lanes: usize,
    detectors: usize,
    base: &[u64],
    columns: impl IntoIterator<Item = (&'a [u64], u64)>,
    scratch: &mut Vec<u64>,
    byte_budget: usize,
    work_budget: usize,
) -> Option<u64> {
    if !(1..=64).contains(&lanes) {
        return None;
    }
    let words = detectors.checked_add(63)? / 64;
    if base.len() != words {
        return None;
    }
    let valid_tail = if detectors % 64 == 0 {
        u64::MAX
    } else {
        (1u64 << (detectors % 64)) - 1
    };
    if base.last().is_some_and(|bits| bits & !valid_tail != 0) {
        return None;
    }
    let maximum_words = byte_budget.checked_sub(size_of::<Vec<u64>>())? / size_of::<u64>();
    if detectors > maximum_words || scratch.capacity() > maximum_words {
        return None;
    }
    let mut remaining = work_budget.checked_sub(detectors.checked_mul(2)?.checked_add(words)?)?;
    if scratch.capacity() < detectors {
        scratch.try_reserve_exact(detectors - scratch.len()).ok()?;
        if scratch.capacity() > maximum_words {
            *scratch = Vec::new();
            return None;
        }
    }
    scratch.resize(detectors, 0);
    scratch.fill(0);
    let all = if lanes == 64 {
        u64::MAX
    } else {
        (1u64 << lanes) - 1
    };
    for (word, &bits) in base.iter().enumerate() {
        let mut bits = bits;
        while bits != 0 {
            remaining = remaining.checked_sub(1)?;
            let bit = bits.trailing_zeros() as usize;
            scratch[word * 64 + bit] = all;
            bits &= bits - 1;
        }
    }
    // Caller supplies read-only category/Independent lane masks from the existing producer.
    // This kernel never consumes a random event and never changes a sampler/cache/output.
    // Scratch may be changed on a late refusal and must not be interpreted unless Some.
    for (effect, selected) in columns {
        remaining = remaining.checked_sub(words.checked_add(1)?)?;
        if effect.len() != words || effect.last().is_some_and(|bits| bits & !valid_tail != 0) {
            return None;
        }
        let selected = selected & all;
        if selected == 0 {
            continue;
        }
        for (word, &bits) in effect.iter().enumerate() {
            let mut bits = bits;
            while bits != 0 {
                remaining = remaining.checked_sub(1)?;
                let bit = bits.trailing_zeros() as usize;
                scratch[word * 64 + bit] ^= selected;
                bits &= bits - 1;
            }
        }
    }
    let _ = remaining;
    Some(scratch.iter().fold(0, |rejected, parity| rejected | parity) & all)
}

// A read-only view of the existing producer. Category masks are computed once
// per typed event; no row is redrawn, rewritten, or compacted by this iterator.
struct PacketColumns<'a> {
    model: &'a ActiveFreePredicate,
    plan: &'a CompiledNearCliffordExecutor,
    tape: &'a [u64],
    hits: &'a [u64],
    noise: Option<&'a NoisePacket>,
    independent: Option<&'a IndependentPacket>,
    lanes: usize,
    all: u64,
    // Cursor into the immutable contributing-event index, not the full tape.
    event: usize,
    first: usize,
    count: usize,
    category: usize,
    contributing_categories: u16,
    masks: [u64; 15],
}
impl<'a> Iterator for PacketColumns<'a> {
    type Item = (&'a [u64], u64);
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            while self.category < self.count {
                let category = self.category;
                self.category += 1;
                let mask = self.masks[category] & self.all;
                if mask == 0 || self.contributing_categories & (1u16 << category) == 0 {
                    continue;
                }
                let model = self.model;
                let column = model.column(self.first + category)?;
                return Some((column, mask));
            }
            let indexed = *self.model.packet_inputs.get(self.event)?;
            let event = indexed.event;
            let kind = *self.plan.random_kinds.get(event)?;
            self.event += 1;
            let input = self.model.inputs[event];
            self.first = input.first;
            self.contributing_categories = input.contributing_categories;
            self.count = 0;
            self.category = 0;
            if !input.contributes {
                continue;
            }
            self.count = input.count;
            self.masks.fill(0);
            match kind {
                RandomKind::Active => {
                    // These columns are zero by the compiler eligibility proof.
                    self.count = 0;
                }
                RandomKind::Independent => {
                    self.masks[0] = if let Some(packet) = self.independent {
                        packet.mask(indexed.ordinal)
                    } else {
                        let mut selected = 0;
                        for lane in 0..self.lanes {
                            if self.tape[lane * self.plan.random_kinds.len() + event] != 0 {
                                selected |= 1u64 << lane;
                            }
                        }
                        selected
                    };
                }
                RandomKind::Noise { choices, .. } => {
                    let noise_event = indexed.ordinal;
                    let selected = self.hits[noise_event] & self.all;
                    if selected == 0 {
                        // No valid lane selected this event. Skip its empty
                        // categories; later inputs retain independent ordinals.
                        self.count = 0;
                        continue;
                    }
                    self.masks = if let Some(packet) = self.noise {
                        packet.choice_masks(noise_event, selected, choices)
                    } else {
                        packet_choice_masks(
                            selected,
                            choices,
                            self.tape,
                            event,
                            self.plan.random_kinds.len(),
                        )
                    };
                }
            }
        }
    }
}

#[cfg(test)]
#[path = "zero_effect_inputs_tests.rs"]
mod zero_effect_inputs_tests;

#[cfg(test)]
#[path = "category_effect_mask_tests.rs"]
mod category_effect_mask_tests;

#[cfg(test)]
#[path = "empty_noise_packet_tests.rs"]
mod empty_noise_packet_tests;

#[cfg(test)]
#[path = "contributing_event_index_tests.rs"]
mod contributing_event_index_tests;

impl ActiveFreePredicate {
    // Only a model built from this same immutable plan may be used. The caller
    // supplies completed producer storage and preserves original resource/RNG
    // ordering. A None result permits the unchanged physical path; no caller
    // sampler/cache/tape/output state has been modified by this method.
    pub(in super::super) fn packet_rejection_mask(
        &self,
        plan: &CompiledNearCliffordExecutor,
        lanes: usize,
        sweep: &[bool],
        tape: &[u64],
        hits: &[u64],
        noise: Option<&NoisePacket>,
        independent: Option<&IndependentPacket>,
        scratch: &mut Vec<u64>,
        byte_budget: usize,
        work_budget: usize,
    ) -> Option<u64> {
        if !(1..=64).contains(&lanes)
            || self.inputs.len() != plan.random_kinds.len()
            || tape.len() < lanes.checked_mul(plan.random_kinds.len())?
            || hits.len() != plan.noise_event_count
        {
            return None;
        }
        let mut noise_count = 0usize;
        let mut independent_count = 0usize;
        for (kind, input) in plan.random_kinds.iter().zip(&self.inputs) {
            match *kind {
                RandomKind::Noise { choices, .. } => {
                    if !(1..=15).contains(&choices) || choices != input.count || input.active {
                        return None;
                    }
                    noise_count = noise_count.checked_add(1)?;
                }
                RandomKind::Independent => {
                    if input.count != 1 || input.active {
                        return None;
                    }
                    independent_count = independent_count.checked_add(1)?;
                }
                RandomKind::Active => {
                    if input.count != 1 || !input.active {
                        return None;
                    }
                }
            }
        }
        if noise_count != plan.noise_event_count
            || independent_count != plan.independent_event_count
        {
            return None;
        }
        // Include the bounded iterator/decoder visits even when all columns are
        // skipped. The generic kernel's work counter alone cannot see those.
        // At most 64 lanes and four compact Noise bit planes are read per event.
        let iterator_work = self
            .inputs
            .len()
            .checked_mul(64 * 16)?
            .checked_add(self.variables.checked_mul(self.words)?)?
            .checked_add(self.sweeps.len().checked_mul(self.words.checked_add(1)?)?)?;
        let kernel_work = work_budget.checked_sub(iterator_work)?;
        let all = if lanes == 64 {
            u64::MAX
        } else {
            (1u64 << lanes) - 1
        };
        let columns = PacketColumns {
            model: self,
            plan,
            tape,
            hits,
            noise,
            independent,
            lanes,
            all,
            event: 0,
            first: 0,
            count: 0,
            category: 0,
            contributing_categories: 0,
            masks: [0; 15],
        };
        let sweeps = self.sweeps.iter().enumerate().filter_map(|(i, index)| {
            sweep
                .get(*index as usize)
                .copied()
                .unwrap_or(false)
                .then(|| {
                    (
                        self.column(self.sweep_first + i)
                            .expect("compiled sweep variable"),
                        all,
                    )
                })
        });
        packet_fail_mask(
            lanes,
            self.eligible.len(),
            self.column(0)?,
            columns.chain(sweeps),
            scratch,
            byte_budget,
            kernel_work,
        )
    }
}