prism-q 0.20.0

PRISM-Q: Performance Rust Interoperable Simulator for Quantum
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! T-gate strategy dispatch for native QEC programs.
//!
//! Public surface: one strategy enum and a single entry point
//! [`run_qec_program_with_strategy`]. [`QecTStrategy::Auto`] is the
//! production dispatcher: exact light-cone SPD, CAMPS, then the private exact
//! tensor-network scalar fallback. [`QecTStrategy::Reference`] is the
//! analytical correctness anchor. [`QecTStrategy::Spd`] and
//! [`QecTStrategy::Camps`] are direct analytical paths.

use super::observable_reroute::{min_cone_z_representative, xor_z_support};
use super::{run_qec_program_reference, QecObservableEstimate, QecOp, QecProgram, QecSampleResult};
#[cfg(test)]
use super::{QecBasis, QecOptions, QecRecordRef};
use crate::backend::mps::MpsBackend;
use crate::backend::Backend;
use crate::circuit::{Circuit, SmallVec};
use crate::error::{PrismError, Result};
use crate::gates::Gate;
use crate::sim::compiled::PackedShots;
use crate::sim::unified_pauli::{run_spd_observable_light_cone, PauliTerm};

/// Strategy used to sample a QEC program that may contain T gates.
///
/// Production path is [`QecTStrategy::Auto`], which tries exact light-cone SPD,
/// then CAMPS, then the private exact tensor-network scalar fallback when CAMPS
/// cannot run. [`QecTStrategy::Reference`] is the analytical correctness
/// anchor.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QecTStrategy {
    /// Production dispatcher. Exact light-cone SPD, CAMPS, then tensor network.
    Auto,
    /// One state-vector simulation per shot. Correctness oracle.
    Reference,
    /// Clifford-augmented matrix product state Pauli expectation path.
    Camps,
    /// Sparse Pauli decomposition after the joint-observable upgrade.
    Spd,
}

#[derive(Debug, Clone)]
pub struct QecObservableReroute {
    pub observable: usize,
    pub stabilizers: Vec<Vec<usize>>,
}

impl QecTStrategy {
    /// Short human label used in benchmark groups and decision tables.
    pub fn label(self) -> &'static str {
        match self {
            QecTStrategy::Auto => "auto",
            QecTStrategy::Reference => "reference",
            QecTStrategy::Camps => "camps",
            QecTStrategy::Spd => "spd",
        }
    }
}

/// Run a QEC program under the chosen T-gate sampling strategy.
///
/// Use [`QecTStrategy::Auto`] for production observable runs. The
/// [`QecTStrategy::Reference`] strategy is always available and matches
/// [`run_qec_program_reference`] exactly.
pub fn run_qec_program_with_strategy(
    program: &QecProgram,
    strategy: QecTStrategy,
) -> Result<QecSampleResult> {
    match strategy {
        QecTStrategy::Auto => run_qec_program_auto(program),
        QecTStrategy::Reference => run_qec_program_reference(program),
        QecTStrategy::Spd => run_qec_program_spd(program),
        QecTStrategy::Camps => run_qec_program_camps(program),
    }
}

#[cfg(test)]
mod auto_test_hooks {
    use std::cell::Cell;

    thread_local! {
        static FORCE_SPD_NONEXACT: Cell<bool> = const { Cell::new(false) };
        static FORCE_CAMPS_FAILURE: Cell<bool> = const { Cell::new(false) };
    }

    pub(super) fn force_spd_nonexact() -> bool {
        FORCE_SPD_NONEXACT.with(Cell::get)
    }

    pub(super) fn force_camps_failure() -> bool {
        FORCE_CAMPS_FAILURE.with(Cell::get)
    }

    pub(super) fn set_force_spd_nonexact(value: bool) {
        FORCE_SPD_NONEXACT.with(|flag| flag.set(value));
    }

    pub(super) fn set_force_camps_failure(value: bool) {
        FORCE_CAMPS_FAILURE.with(|flag| flag.set(value));
    }
}

#[cfg(test)]
fn run_qec_program_spd_for_auto(program: &QecProgram) -> Result<QecSampleResult> {
    let mut result = run_qec_program_spd(program)?;
    if auto_test_hooks::force_spd_nonexact() {
        if let Some(estimates) = result.observable_expectations.as_mut() {
            for estimate in estimates {
                estimate.variance = estimate.variance.max(1.0);
            }
        }
    }
    Ok(result)
}

#[cfg(not(test))]
fn run_qec_program_spd_for_auto(program: &QecProgram) -> Result<QecSampleResult> {
    run_qec_program_spd(program)
}

#[cfg(test)]
fn run_qec_program_camps_for_auto(program: &QecProgram) -> Result<QecSampleResult> {
    if auto_test_hooks::force_camps_failure() {
        return Err(PrismError::BackendUnsupported {
            backend: "QEC CAMPS".to_string(),
            operation: "forced CAMPS failure for auto-dispatch test".to_string(),
        });
    }
    run_qec_program_camps(program)
}

#[cfg(not(test))]
fn run_qec_program_camps_for_auto(program: &QecProgram) -> Result<QecSampleResult> {
    run_qec_program_camps(program)
}

fn run_qec_program_auto(program: &QecProgram) -> Result<QecSampleResult> {
    match run_qec_program_spd_for_auto(program) {
        Ok(result) if analytical_result_has_no_truncation(&result) => Ok(result),
        Ok(_) => run_qec_program_auto_after_camps(
            program,
            "light-cone SPD exceeded its exact truncation budget".to_string(),
        ),
        Err(spd_error) => run_qec_program_auto_after_camps(
            program,
            format!("light-cone SPD failed ({spd_error})"),
        ),
    }
}

fn run_qec_program_auto_after_camps(
    program: &QecProgram,
    spd_context: String,
) -> Result<QecSampleResult> {
    match run_qec_program_camps_for_auto(program) {
        Ok(result) => Ok(result),
        Err(camps_error) => {
            run_qec_program_tensor_network_observable(program).map_err(|tn_error| {
                PrismError::IncompatibleBackend {
                    backend: "QEC auto T-strategy".to_string(),
                    reason: format!(
                        "{spd_context} and CAMPS fallback failed ({camps_error}); \
                         tensor-network scalar fallback failed: {tn_error}"
                    ),
                }
            })
        }
    }
}

/// Discarded-mass variance at or below this is treated as numerically exact.
/// SPD reports `variance = total_discarded²`, and a handful of legitimate
/// sub-`QEC_SPD_EPSILON` terms can leave a tiny nonzero residue that must not
/// force the costlier CAMPS / tensor-network fallback. A discarded amplitude of
/// `1e-6` (variance `1e-12`) is far below the statistical noise of any shot
/// count the QEC paths target.
const QEC_SPD_NEGLIGIBLE_VARIANCE: f64 = 1e-12;

fn analytical_result_has_no_truncation(result: &QecSampleResult) -> bool {
    result
        .observable_expectations
        .as_ref()
        .map(|estimates| {
            estimates
                .iter()
                .all(|estimate| estimate.variance <= QEC_SPD_NEGLIGIBLE_VARIANCE)
        })
        .unwrap_or(true)
}

/// Default truncation tolerance for SPD on QEC programs.
const QEC_SPD_EPSILON: f64 = 1e-10;
/// Default term-count cap for SPD on QEC programs. Sub-percent error is
/// expected for `t ≤ 20`; larger T-counts may exceed this and report a
/// non-zero `total_discarded`.
const QEC_SPD_MAX_TERMS: usize = 16_384;

/// Lower a QEC program into a unitary Circuit plus per-record qubit map
/// for the joint-Pauli SPD / CAMPS analytical paths.
///
/// Delegates to the deferred-lowering machinery in `qec/noise.rs`
/// (`lower_qec_program_to_deferred_circuit_allowing_non_clifford`),
/// which handles Reset (fresh qubit alias), MPP (scratch qubit + CNOT
/// chain), basis-rotated measurements (`H` / `S†H` prefix), and the
/// trailing measurement record bookkeeping. The trailing
/// `Instruction::Measure` ops appended by the deferred lowering are
/// stripped from the returned Circuit so the strategies can evaluate
/// `⟨0^n| U† P U |0^n⟩` against the pure unitary.
///
/// **Still rejected:**
/// - Active `Noise` channels: SPD / CAMPS do not have an
///   `apply_noise_to_measurements`-style hook.
/// - `ExpectationValue`: unmodified rejection.
/// - `Detector`: analytical strategies do not emit detector rows yet.
fn lower_qec_program_for_pauli_observable(program: &QecProgram) -> Result<(Circuit, Vec<usize>)> {
    for op in program.ops() {
        match op {
            QecOp::Noise { channel, .. } if channel.probability() > 0.0 => {
                return Err(PrismError::IncompatibleBackend {
                    backend: "QEC SPD / CAMPS".to_string(),
                    reason: format!(
                        "analytical strategies evaluate pure-state expectations and \
                         cannot absorb Pauli noise channels (got `{channel:?}`); \
                         route noisy QEC programs through `run_qec_program_reference` \
                         (statevector per shot with stochastic noise) or extend \
                         CAMPS to an `MPDO` density-matrix path"
                    ),
                });
            }
            QecOp::ExpectationValue { .. } => {
                return Err(PrismError::IncompatibleBackend {
                    backend: "QEC SPD / CAMPS".to_string(),
                    reason: "EXP_VAL op is reserved for the dedicated \
                             expectation runner"
                        .to_string(),
                });
            }
            QecOp::Detector { .. } => {
                return Err(PrismError::IncompatibleBackend {
                    backend: "QEC SPD / CAMPS".to_string(),
                    reason: "analytical strategies do not emit detector records yet".to_string(),
                });
            }
            _ => {}
        }
    }

    let deferred =
        super::noise::lower_qec_program_to_deferred_circuit_allowing_non_clifford(program)?;
    let mut circuit = Circuit::new(
        deferred.circuit.num_qubits,
        deferred.circuit.num_classical_bits,
    );
    for inst in &deferred.circuit.instructions {
        if matches!(inst, crate::circuit::Instruction::Gate { .. }) {
            circuit.instructions.push(inst.clone());
        }
    }
    Ok((circuit, deferred.measurement_qubits))
}

/// Convert an observable-row record list into a joint Pauli observable.
/// Each record contributes a single Z factor on the measured qubit; an
/// even number of factors on the same qubit cancels (Z² = I).
fn observable_row_to_pauli_terms(
    row: &[usize],
    record_to_qubit: &[usize],
) -> Result<Vec<PauliTerm>> {
    let mut parity: SmallVec<[(usize, bool); 8]> = SmallVec::new();
    for &record in row {
        let qubit = *record_to_qubit
            .get(record)
            .ok_or_else(|| PrismError::InvalidParameter {
                message: format!(
                    "observable references record {record} but only {} records exist",
                    record_to_qubit.len()
                ),
            })?;
        if let Some(entry) = parity.iter_mut().find(|(q, _)| *q == qubit) {
            entry.1 = !entry.1;
        } else {
            parity.push((qubit, true));
        }
    }
    let mut terms: Vec<PauliTerm> = parity
        .into_iter()
        .filter(|(_, odd)| *odd)
        .map(|(qubit, _)| PauliTerm::z(qubit))
        .collect();
    terms.sort_by_key(|t| t.qubit);
    Ok(terms)
}

/// Max number of postselection rows allowed for the analytical
/// conditional-expectation path. Each additional row doubles the
/// number of Pauli evaluations; 12 caps the inner sum at 4,096 terms
/// per observable.
const MAX_POSTSEL_FOR_CONDITIONAL_EXPECTATION: usize = 12;

/// Resolve postselection rows into (qubit-set, sign) pairs ready for
/// the conditional-expectation expansion. Returns
/// `(Vec<(Vec<usize>, bool)>, Vec<f64>)`: per-row qubit list and per-row
/// `ε_i ∈ {+1, -1}` based on the `expected` outcome bit.
fn lower_postselection_rows(
    rows: &[(Vec<usize>, bool)],
    record_to_qubit: &[usize],
) -> Result<Vec<(Vec<usize>, f64)>> {
    if rows.len() > MAX_POSTSEL_FOR_CONDITIONAL_EXPECTATION {
        return Err(PrismError::BackendUnsupported {
            backend: "QEC analytical conditional expectation".to_string(),
            operation: format!(
                "{} postselection rows exceeds the cap ({}); use the \
                 reference runner for large-postsel circuits",
                rows.len(),
                MAX_POSTSEL_FOR_CONDITIONAL_EXPECTATION
            ),
        });
    }
    let mut out = Vec::with_capacity(rows.len());
    for (records, expected) in rows {
        let mut qubits = Vec::with_capacity(records.len());
        for &record in records {
            let qubit =
                *record_to_qubit
                    .get(record)
                    .ok_or_else(|| PrismError::InvalidParameter {
                        message: format!(
                            "postselection references record {record} but only {} records exist",
                            record_to_qubit.len()
                        ),
                    })?;
            qubits.push(qubit);
        }
        let sign = if *expected { -1.0 } else { 1.0 };
        out.push((qubits, sign));
    }
    Ok(out)
}

/// XOR-merge a sequence of qubit lists into a sorted, deduplicated
/// `Vec<PauliTerm>` of `Z` factors (each qubit appears with parity 1).
fn merge_qubit_groups_into_z_terms<'a, I>(groups: I) -> Vec<PauliTerm>
where
    I: IntoIterator<Item = &'a [usize]>,
{
    use std::collections::BTreeMap;
    let mut counts: BTreeMap<usize, usize> = BTreeMap::new();
    for group in groups {
        for &q in group {
            *counts.entry(q).or_insert(0) += 1;
        }
    }
    counts
        .into_iter()
        .filter(|(_, c)| c % 2 == 1)
        .map(|(q, _)| PauliTerm::z(q))
        .collect()
}

fn validate_z_stabilizers(stabilizers: &[Vec<usize>], num_qubits: usize) -> Result<()> {
    for stabilizer in stabilizers {
        for &qubit in stabilizer {
            if qubit >= num_qubits {
                return Err(PrismError::InvalidQubit {
                    index: qubit,
                    register_size: num_qubits,
                });
            }
        }
    }
    Ok(())
}

/// Per-Pauli-string evaluation outcome. `discarded_sq` carries the
/// squared truncation weight (zero for exact strategies like CAMPS and
/// tensor-network); the analytical SPD strategy reports its light-cone
/// truncation error here so it can be attributed to the right observable.
struct ConditionalEval {
    mean: f64,
    discarded_sq: f64,
}

/// Generic conditional-expectation evaluator for the analytical
/// strategies. Implements `⟨O⟩_post = ⟨O·Π⟩ / ⟨Π⟩` where
/// `Π = ∏_j (I + ε_j Z(S_j))/2`. Expands the product into a sum over
/// `2^J` subsets `T ⊆ {1..J}` and evaluates each Pauli-string
/// expectation via the supplied closure. Returns
/// `(post_means, per_observable_discarded_sq, accept_rate)`, where the
/// discarded-weight vector is aligned with `observable_terms_list` so each
/// observable's truncation error is tracked independently.
fn evaluate_conditional_expectations<F>(
    observable_terms_list: &[Vec<PauliTerm>],
    postsel: &[(Vec<usize>, f64)],
    mut eval: F,
) -> Result<(Vec<f64>, Vec<f64>, f64)>
where
    F: FnMut(&[PauliTerm]) -> Result<ConditionalEval>,
{
    let j = postsel.len();
    let subset_count = 1usize << j;
    let scale = 1.0 / (1usize << j) as f64;

    let mut pi_sum = 0.0;
    let mut obs_pi_sums = vec![0.0; observable_terms_list.len()];
    let mut obs_discarded = vec![0.0; observable_terms_list.len()];

    let obs_qubits: Vec<Vec<usize>> = observable_terms_list
        .iter()
        .map(|terms| terms.iter().map(|t| t.qubit).collect())
        .collect();

    for mask in 0..subset_count {
        let mut sign = 1.0;
        let mut qubit_groups: Vec<&[usize]> = Vec::with_capacity(j + 1);
        for (j_idx, (qubits, eps)) in postsel.iter().enumerate() {
            if (mask >> j_idx) & 1 == 1 {
                sign *= *eps;
                qubit_groups.push(qubits.as_slice());
            }
        }
        let pi_pauli = merge_qubit_groups_into_z_terms(qubit_groups.iter().copied());
        let pi_value = eval(&pi_pauli)?;
        pi_sum += sign * pi_value.mean;

        for (obs_idx, obs_q) in obs_qubits.iter().enumerate() {
            qubit_groups.push(obs_q.as_slice());
            let combined_pauli = merge_qubit_groups_into_z_terms(qubit_groups.iter().copied());
            qubit_groups.pop();
            let outcome = eval(&combined_pauli)?;
            obs_pi_sums[obs_idx] += sign * outcome.mean;
            obs_discarded[obs_idx] += outcome.discarded_sq;
        }
    }

    let accept_rate = (pi_sum * scale).clamp(0.0, 1.0);
    if accept_rate <= 1e-12 {
        return Err(PrismError::InvalidParameter {
            message: format!(
                "QEC postselection has acceptance rate {accept_rate:.2e}; cannot \
                 condition an expectation on a zero-measure event"
            ),
        });
    }
    let post_means: Vec<f64> = obs_pi_sums
        .iter()
        .map(|s| (s * scale) / accept_rate)
        .collect();
    Ok((post_means, obs_discarded, accept_rate))
}

fn build_qec_result_from_observable_means(
    program: &QecProgram,
    means: Vec<f64>,
    estimates: Vec<QecObservableEstimate>,
) -> Result<QecSampleResult> {
    build_qec_result_with_acceptance(program, means, estimates, 1.0)
}

fn accepted_shots_for_rate(total_shots: usize, accept_rate: f64) -> usize {
    (((total_shots as f64) * accept_rate).round() as usize).min(total_shots)
}

fn build_qec_result_with_acceptance(
    program: &QecProgram,
    means: Vec<f64>,
    estimates: Vec<QecObservableEstimate>,
    accept_rate: f64,
) -> Result<QecSampleResult> {
    if program.num_detectors() > 0 {
        return Err(PrismError::IncompatibleBackend {
            backend: "QEC analytical result".to_string(),
            reason: "analytical QEC T strategies do not emit detector records yet".to_string(),
        });
    }

    let total_shots = program.options().shots;
    let accepted_shots = accepted_shots_for_rate(total_shots, accept_rate);
    let discarded_shots = total_shots - accepted_shots;
    let num_observables = program.num_observables();
    let num_detectors = program.num_detectors();
    let num_measurements = program.num_measurements();
    let measurements = PackedShots::from_meas_major(Vec::new(), 0, num_measurements);
    // Detector records are empty because analytical strategies reject
    // detectors. Observable records are synthesized so their popcount
    // marginals equal `logical_errors`, but the analytical path has no
    // per-shot accept mask: the `count` one-bits occupy positions
    // `[0, accepted_shots)` and the rest is inert padding up to
    // `total_shots`. Consumers must therefore divide by `accepted_shots`
    // (e.g. via [`QecSampleResult::logical_error_rates`]), not
    // `total_shots`, and must not align observable rows shot-for-shot with
    // detector rows. Exact expectations live in `observable_expectations`.
    let detector_words = total_shots.div_ceil(64) * num_detectors;
    let detectors =
        PackedShots::from_meas_major(vec![0u64; detector_words], total_shots, num_detectors);

    let mut logical_errors = Vec::with_capacity(num_observables);
    for &mean in &means {
        let rate = ((1.0 - mean) * 0.5).clamp(0.0, 1.0);
        let count = if accepted_shots == 0 {
            0
        } else {
            (rate * accepted_shots as f64).round() as u64
        };
        logical_errors.push(count);
    }
    let observables = packed_observable_records_from_logical_errors(total_shots, &logical_errors);

    let result = QecSampleResult::new_with_total_shots(
        total_shots,
        measurements,
        detectors,
        observables,
        accepted_shots,
        discarded_shots,
        logical_errors,
    )?;
    result.with_observable_expectations(estimates)
}

fn packed_observable_records_from_logical_errors(
    total_shots: usize,
    logical_errors: &[u64],
) -> PackedShots {
    let num_observables = logical_errors.len();
    let s_words = total_shots.div_ceil(64);
    let mut data = vec![0u64; num_observables * s_words];
    for (observable, &count) in logical_errors.iter().enumerate() {
        let mut remaining = (count as usize).min(total_shots);
        for word in 0..s_words {
            let bits = remaining.min(64);
            data[observable * s_words + word] = match bits {
                0 => 0,
                64 => u64::MAX,
                n => (1u64 << n) - 1,
            };
            remaining -= bits;
            if remaining == 0 {
                break;
            }
        }
    }
    PackedShots::from_meas_major(data, total_shots, num_observables)
}

/// Default MPS bond-dimension cap for CAMPS. Matches the
/// auto-dispatch ceiling used elsewhere in PRISM-Q (`MPS(256)` in
/// `sim/dispatch.rs`).
const QEC_CAMPS_MAX_BOND_DIM: usize = 256;

/// CAMPS analytical path for QEC observables.
///
/// Tracks the Clifford prefix separately from the MPS, applies T/Tdg through
/// the CAMPS disentangler update, and evaluates Z-string observables directly
/// from the MPS plus prefix. The path shares SPD's restricted lowering and
/// Z-only observable scope.
fn run_qec_program_camps(program: &QecProgram) -> Result<QecSampleResult> {
    use super::camps_prefix::{
        apply_t_via_camps, evaluate_z_observable_camps, SignedCliffordPrefix,
    };
    use crate::circuit::Instruction;

    let (circuit, record_to_qubit) = lower_qec_program_for_pauli_observable(program)?;
    let observable_rows = program.observable_rows()?;
    let postsel_rows = program.postselection_rows()?;

    let mut backend = MpsBackend::new(program.options().seed, QEC_CAMPS_MAX_BOND_DIM);
    backend.init(circuit.num_qubits, 0)?;

    let mut prefix = SignedCliffordPrefix::identity(circuit.num_qubits);
    for inst in &circuit.instructions {
        let Instruction::Gate { gate, targets } = inst else {
            continue;
        };
        match gate {
            Gate::T => {
                apply_t_via_camps(&mut prefix, &mut backend, targets[0], false, 1e-10)?;
            }
            Gate::Tdg => {
                apply_t_via_camps(&mut prefix, &mut backend, targets[0], true, 1e-10)?;
            }
            _ => {
                prefix.apply_state_gate(gate, targets).map_err(|_| {
                    PrismError::InvalidParameter {
                        message: format!(
                            "CAMPS dispatcher: gate {gate:?} is neither Clifford nor T/Tdg; \
                             extend `lower_qec_program_for_pauli_observable` or add a CAMPS \
                             fallback to handle this instruction"
                        ),
                    }
                })?;
            }
        }
    }

    let mut observable_terms_list = Vec::with_capacity(observable_rows.len());
    for row in observable_rows.iter() {
        observable_terms_list.push(observable_row_to_pauli_terms(row, &record_to_qubit)?);
    }

    // Clifford gates are absorbed into a [`SignedCliffordPrefix`]; T/Tdg
    // gates dispatch through [`apply_t_via_camps`] (Liu & Clark
    // Algorithm 1 OFD). The observable `⟨ψ|Π Z_q|ψ⟩` is evaluated as
    // `⟨ϕ| C†(Π Z_q) C |ϕ⟩` by composing twisted Pauli rows and calling
    // [`crate::backend::mps::MpsBackend::pauli_expectation`].
    let eval = |terms: &[PauliTerm]| -> Result<f64> {
        let qubits: Vec<usize> = terms.iter().map(|t| t.qubit).collect();
        evaluate_z_observable_camps(&prefix, &backend, &qubits)
    };

    if postsel_rows.is_empty() {
        let mut means = Vec::with_capacity(observable_terms_list.len());
        let mut estimates = Vec::with_capacity(observable_terms_list.len());
        for terms in &observable_terms_list {
            let mean = eval(terms)?;
            means.push(mean);
            estimates.push(QecObservableEstimate {
                mean,
                variance: 0.0,
                num_shots: program.options().shots,
            });
        }
        return build_qec_result_from_observable_means(program, means, estimates);
    }

    let postsel = lower_postselection_rows(&postsel_rows, &record_to_qubit)?;
    let (means, _obs_discarded, accept_rate) =
        evaluate_conditional_expectations(&observable_terms_list, &postsel, |terms| {
            Ok(ConditionalEval {
                mean: eval(terms)?,
                discarded_sq: 0.0,
            })
        })?;
    let estimate_shots = accepted_shots_for_rate(program.options().shots, accept_rate);
    let estimates: Vec<_> = means
        .iter()
        .map(|&m| QecObservableEstimate {
            mean: m,
            variance: 0.0,
            num_shots: estimate_shots,
        })
        .collect();
    build_qec_result_with_acceptance(program, means, estimates, accept_rate)
}

// Private exact fallback for Auto. Contracts each scalar observable directly
// as <0| U^dagger P U |0> and does not materialize a dense statevector.
fn run_qec_program_tensor_network_observable(program: &QecProgram) -> Result<QecSampleResult> {
    let (circuit, record_to_qubit) = lower_qec_program_for_pauli_observable(program)?;
    let observable_rows = program.observable_rows()?;
    let postsel_rows = program.postselection_rows()?;

    let mut observable_terms_list = Vec::with_capacity(observable_rows.len());
    for row in observable_rows.iter() {
        observable_terms_list.push(observable_row_to_pauli_terms(row, &record_to_qubit)?);
    }

    let eval = |terms: &[PauliTerm]| -> Result<f64> {
        crate::backend::tensornetwork::expectation_zero_state(&circuit, terms)
    };

    if postsel_rows.is_empty() {
        let mut means = Vec::with_capacity(observable_terms_list.len());
        let mut estimates = Vec::with_capacity(observable_terms_list.len());
        for terms in &observable_terms_list {
            let mean = eval(terms)?;
            means.push(mean);
            estimates.push(QecObservableEstimate {
                mean,
                variance: 0.0,
                num_shots: program.options().shots,
            });
        }
        return build_qec_result_from_observable_means(program, means, estimates);
    }

    let postsel = lower_postselection_rows(&postsel_rows, &record_to_qubit)?;
    let (means, _obs_discarded, accept_rate) =
        evaluate_conditional_expectations(&observable_terms_list, &postsel, |terms| {
            Ok(ConditionalEval {
                mean: eval(terms)?,
                discarded_sq: 0.0,
            })
        })?;
    let estimate_shots = accepted_shots_for_rate(program.options().shots, accept_rate);
    let estimates: Vec<_> = means
        .iter()
        .map(|&mean| QecObservableEstimate {
            mean,
            variance: 0.0,
            num_shots: estimate_shots,
        })
        .collect();
    build_qec_result_with_acceptance(program, means, estimates, accept_rate)
}

/// Tolerance on `⟨ψ|S|ψ⟩ = +1` for a rerouting stabilizer.
const REROUTE_STABILIZER_TOL: f64 = 1e-6;

/// Verify that the Z-string distinguishing the original observable support from
/// the rerouted support is a genuine `+1` stabilizer of the evaluated state.
///
/// Rerouting replaces observable `O` with `O' = O · S`, which preserves the
/// expectation only when `S |ψ⟩ = +|ψ⟩`. The reroute module documents this as a
/// caller obligation but cannot check it (it has no state). Here the state is
/// available cheaply via SPD on `S` alone, so validate it rather than silently
/// evaluate a different operator. `S = supp(original) ⊕ supp(rerouted)`; an
/// empty support means no reroute happened and is trivially valid.
fn verify_reroute_is_state_stabilizer(
    circuit: &Circuit,
    original_support: &[usize],
    rerouted_support: &[usize],
) -> Result<()> {
    let stab_support = xor_z_support(original_support, rerouted_support);
    if stab_support.is_empty() {
        return Ok(());
    }
    let stab_terms: Vec<PauliTerm> = stab_support.iter().copied().map(PauliTerm::z).collect();
    let stab =
        run_spd_observable_light_cone(circuit, &stab_terms, QEC_SPD_EPSILON, QEC_SPD_MAX_TERMS)?;
    if (stab.mean - 1.0).abs() > REROUTE_STABILIZER_TOL {
        return Err(PrismError::InvalidParameter {
            message: format!(
                "reroute stabilizer on qubits {stab_support:?} is not a +1 stabilizer of the \
                 state (⟨S⟩ = {:.6}); rerouting would evaluate a different operator",
                stab.mean
            ),
        });
    }
    Ok(())
}

pub fn run_qec_program_spd_rerouted(
    program: &QecProgram,
    reroutes: &[QecObservableReroute],
) -> Result<QecSampleResult> {
    let (circuit, record_to_qubit) = lower_qec_program_for_pauli_observable(program)?;
    let observable_rows = program.observable_rows()?;
    let postsel_rows = program.postselection_rows()?;
    if !postsel_rows.is_empty() {
        return Err(PrismError::IncompatibleBackend {
            backend: "QEC SPD rerouted".to_string(),
            reason: "rerouted analytical SPD does not yet combine stabilizer rerouting with postselection projectors".to_string(),
        });
    }

    // Reroute stabilizers and the observable support must live in the same
    // qubit space. Observable support comes from `record_to_qubit`, which
    // resolves to lowered circuit qubits; a `Reset` reassigns a logical
    // qubit to a fresh higher index, so a stabilizer expressed in original
    // logical-qubit indices would silently land on the wrong physical qubit.
    // Reject such programs rather than evaluate a different operator.
    if program
        .ops()
        .iter()
        .any(|op| matches!(op, QecOp::Reset { .. }))
    {
        return Err(PrismError::IncompatibleBackend {
            backend: "QEC SPD rerouted".to_string(),
            reason: "stabilizer rerouting is not supported for programs containing RESET; \
                     resets relabel logical qubits, making stabilizer qubit indices ambiguous"
                .to_string(),
        });
    }

    let mut reroute_by_observable: Vec<Option<&QecObservableReroute>> =
        vec![None; observable_rows.len()];
    for reroute in reroutes {
        let slot = reroute_by_observable
            .get_mut(reroute.observable)
            .ok_or_else(|| PrismError::InvalidParameter {
                message: format!(
                    "reroute references observable {} but program has {} observables",
                    reroute.observable,
                    observable_rows.len()
                ),
            })?;
        if slot.is_some() {
            return Err(PrismError::InvalidParameter {
                message: format!("duplicate reroute for observable {}", reroute.observable),
            });
        }
        validate_z_stabilizers(&reroute.stabilizers, program.num_qubits())?;
        *slot = Some(reroute);
    }

    let mut means = Vec::with_capacity(observable_rows.len());
    let mut estimates = Vec::with_capacity(observable_rows.len());
    for (observable, row) in observable_rows.iter().enumerate() {
        let mut terms = observable_row_to_pauli_terms(row, &record_to_qubit)?;
        if let Some(reroute) = reroute_by_observable[observable] {
            let support: Vec<usize> = terms.iter().map(|term| term.qubit).collect();
            let route = min_cone_z_representative(&circuit, &support, &reroute.stabilizers)?;
            verify_reroute_is_state_stabilizer(&circuit, &support, &route.rerouted_support)?;
            terms = route
                .rerouted_support
                .iter()
                .copied()
                .map(PauliTerm::z)
                .collect();
        }
        let result =
            run_spd_observable_light_cone(&circuit, &terms, QEC_SPD_EPSILON, QEC_SPD_MAX_TERMS)?;
        means.push(result.mean);
        estimates.push(QecObservableEstimate {
            mean: result.mean,
            variance: result.total_discarded * result.total_discarded,
            num_shots: program.options().shots,
        });
    }

    build_qec_result_from_observable_means(program, means, estimates)
}

/// SPD analytical path for QEC observables.
///
/// Backward-propagates the observable through its inverse light cone, branching each
/// T into two weighted Pauli terms, truncating terms below
/// `QEC_SPD_EPSILON` when the sum exceeds `QEC_SPD_MAX_TERMS`. Returns
/// the diagonal sum on `|0^n⟩` per observable. Truncation error is
/// reported via `QecObservableEstimate::variance = total_discarded²`.
fn run_qec_program_spd(program: &QecProgram) -> Result<QecSampleResult> {
    let (circuit, record_to_qubit) = lower_qec_program_for_pauli_observable(program)?;
    let observable_rows = program.observable_rows()?;
    let postsel_rows = program.postselection_rows()?;
    let mut observable_terms_list = Vec::with_capacity(observable_rows.len());
    for row in observable_rows.iter() {
        observable_terms_list.push(observable_row_to_pauli_terms(row, &record_to_qubit)?);
    }

    if postsel_rows.is_empty() {
        let mut means = Vec::with_capacity(observable_terms_list.len());
        let mut estimates = Vec::with_capacity(observable_terms_list.len());
        for terms in &observable_terms_list {
            let result =
                run_spd_observable_light_cone(&circuit, terms, QEC_SPD_EPSILON, QEC_SPD_MAX_TERMS)?;
            means.push(result.mean);
            estimates.push(QecObservableEstimate {
                mean: result.mean,
                variance: result.total_discarded * result.total_discarded,
                num_shots: program.options().shots,
            });
        }
        return build_qec_result_from_observable_means(program, means, estimates);
    }

    let postsel = lower_postselection_rows(&postsel_rows, &record_to_qubit)?;
    let (means, obs_discarded, accept_rate) =
        evaluate_conditional_expectations(&observable_terms_list, &postsel, |terms| {
            let result =
                run_spd_observable_light_cone(&circuit, terms, QEC_SPD_EPSILON, QEC_SPD_MAX_TERMS)?;
            Ok(ConditionalEval {
                mean: result.mean,
                discarded_sq: result.total_discarded * result.total_discarded,
            })
        })?;
    let estimate_shots = accepted_shots_for_rate(program.options().shots, accept_rate);
    let estimates: Vec<_> = means
        .iter()
        .zip(obs_discarded.iter())
        .map(|(&m, &discarded_sq)| QecObservableEstimate {
            mean: m,
            variance: discarded_sq,
            num_shots: estimate_shots,
        })
        .collect();
    build_qec_result_with_acceptance(program, means, estimates, accept_rate)
}

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

    const TEST_SEED: u64 = 0xDEAD_BEEF;

    struct AutoHookGuard;

    impl AutoHookGuard {
        fn force_spd_nonexact_and_camps_failure() -> Self {
            super::auto_test_hooks::set_force_spd_nonexact(true);
            super::auto_test_hooks::set_force_camps_failure(true);
            Self
        }
    }

    impl Drop for AutoHookGuard {
        fn drop(&mut self) {
            super::auto_test_hooks::set_force_spd_nonexact(false);
            super::auto_test_hooks::set_force_camps_failure(false);
        }
    }

    fn options(shots: usize) -> QecOptions {
        QecOptions {
            shots,
            seed: TEST_SEED,
            chunk_size: Some(128),
            keep_measurements: false,
        }
    }

    fn h_t_h_program(shots: usize) -> QecProgram {
        let mut program = QecProgram::with_options(1, options(shots));
        program.push_gate(Gate::H, &[0]).unwrap();
        program.push_gate(Gate::T, &[0]).unwrap();
        program.push_gate(Gate::H, &[0]).unwrap();
        let m0 = program.measure_z(0).unwrap();
        program
            .observable_include(0, &[QecRecordRef::absolute(m0)])
            .unwrap();
        program
    }

    fn entangled_xor_program(shots: usize) -> QecProgram {
        let mut program = QecProgram::with_options(3, options(shots));
        program.push_gate(Gate::H, &[0]).unwrap();
        program.push_gate(Gate::Cx, &[0, 1]).unwrap();
        program.push_gate(Gate::Cx, &[1, 2]).unwrap();
        program.push_gate(Gate::T, &[0]).unwrap();
        for qubit in 0..3 {
            program.push_gate(Gate::H, &[qubit]).unwrap();
        }
        let mut records = Vec::new();
        for qubit in 0..3 {
            records.push(QecRecordRef::absolute(program.measure_z(qubit).unwrap()));
        }
        program.observable_include(0, &records).unwrap();
        program
    }

    fn postselected_program(shots: usize) -> QecProgram {
        let mut program = QecProgram::with_options(1, options(shots));
        program.push_gate(Gate::H, &[0]).unwrap();
        program.push_gate(Gate::T, &[0]).unwrap();
        let m0 = program.measure_z(0).unwrap();
        program.reset(QecBasis::Z, 0).unwrap();
        program.push_gate(Gate::H, &[0]).unwrap();
        program.push_gate(Gate::T, &[0]).unwrap();
        let m1 = program.measure_z(0).unwrap();
        program
            .postselect(&[QecRecordRef::absolute(m0)], false)
            .unwrap();
        program
            .observable_include(0, &[QecRecordRef::absolute(m1)])
            .unwrap();
        program
    }

    fn deterministic_t_zero_program(shots: usize) -> QecProgram {
        let mut program = QecProgram::with_options(1, options(shots));
        program.push_gate(Gate::T, &[0]).unwrap();
        let m0 = program.measure_z(0).unwrap();
        program
            .observable_include(0, &[QecRecordRef::absolute(m0)])
            .unwrap();
        program
    }

    fn deterministic_t_one_program(shots: usize) -> QecProgram {
        let mut program = QecProgram::with_options(1, options(shots));
        program.push_gate(Gate::X, &[0]).unwrap();
        program.push_gate(Gate::T, &[0]).unwrap();
        let m0 = program.measure_z(0).unwrap();
        program
            .observable_include(0, &[QecRecordRef::absolute(m0)])
            .unwrap();
        program
    }

    fn estimates(result: &QecSampleResult) -> &[QecObservableEstimate] {
        result
            .observable_expectations
            .as_ref()
            .expect("analytical result must populate observable expectations")
    }

    fn assert_estimates_close(actual: &QecSampleResult, expected: &QecSampleResult) {
        let actual_estimates = estimates(actual);
        let expected_estimates = estimates(expected);
        assert_eq!(actual_estimates.len(), expected_estimates.len());
        for (idx, (actual, expected)) in actual_estimates
            .iter()
            .zip(expected_estimates.iter())
            .enumerate()
        {
            assert!(
                (actual.mean - expected.mean).abs() < 1e-10,
                "observable {idx}: tensor-network mean {} differs from expected {}",
                actual.mean,
                expected.mean
            );
            assert_eq!(actual.variance, 0.0);
        }
        assert_eq!(actual.accepted_shots, expected.accepted_shots);
        assert_eq!(actual.discarded_shots, expected.discarded_shots);
        assert_eq!(actual.logical_errors, expected.logical_errors);
    }

    #[test]
    fn tensor_network_observable_matches_spd_on_small_non_truncated_programs() {
        let fixtures = vec![
            h_t_h_program(512),
            entangled_xor_program(512),
            postselected_program(512),
        ];
        for program in fixtures {
            let spd = run_qec_program_spd(&program).unwrap();
            assert!(analytical_result_has_no_truncation(&spd));
            let tensor_network = run_qec_program_tensor_network_observable(&program).unwrap();
            assert_estimates_close(&tensor_network, &spd);
        }
    }

    #[test]
    fn tensor_network_observable_matches_reference_on_deterministic_t_fixtures() {
        let fixtures = vec![
            deterministic_t_zero_program(128),
            deterministic_t_one_program(128),
        ];
        for program in fixtures {
            let reference = run_qec_program_reference(&program).unwrap();
            let tensor_network = run_qec_program_tensor_network_observable(&program).unwrap();
            assert_eq!(tensor_network.accepted_shots, reference.accepted_shots);
            assert_eq!(tensor_network.discarded_shots, reference.discarded_shots);
            assert_eq!(tensor_network.logical_errors, reference.logical_errors);
        }
    }

    #[test]
    fn auto_uses_tensor_network_when_spd_nonexact_and_camps_fails() {
        let program = h_t_h_program(256);
        let expected = run_qec_program_tensor_network_observable(&program).unwrap();
        let _guard = AutoHookGuard::force_spd_nonexact_and_camps_failure();
        let actual = run_qec_program_auto(&program).unwrap();
        assert_estimates_close(&actual, &expected);
    }
}