Skip to main content

ppvm_tableau/gates/
reset.rs

1// SPDX-FileCopyrightText: 2026 The PPVM Authors
2// SPDX-License-Identifier: Apache-2.0
3
4use std::fmt::Debug;
5
6use crate::prelude::*;
7use bitvec::view::BitView;
8use num::complex::{Complex, Complex64, ComplexFloat};
9use num::traits::{One, PrimInt, ToPrimitive, Zero};
10
11impl<T: Config> Reset for Tableau<T>
12where
13    <<T as Config>::Storage as BitView>::Store: PrimInt,
14{
15    fn reset(&mut self, addr0: usize) {
16        let m = self.measure(addr0);
17        if m {
18            self.x(addr0);
19        }
20    }
21}
22
23impl<T, I, C> Reset for GeneralizedTableau<T, I, C>
24where
25    T: Config,
26    <<T as Config>::Storage as BitView>::Store: PrimInt,
27    I: TableauIndex + Debug,
28    C: SparseVector<Complex<T::Coeff>, I> + Debug,
29    T::Coeff: One
30        + Zero
31        + Clone
32        + num::Num
33        + ToPrimitive
34        + std::fmt::Debug
35        + std::ops::Mul<f64>
36        + PartialOrd<f64>,
37    Complex<T::Coeff>: std::ops::Mul<Output = Complex<T::Coeff>>
38        + From<Complex64>
39        + std::ops::MulAssign
40        + std::ops::AddAssign
41        + One
42        + ComplexFloat
43        + Copy,
44{
45    fn reset(&mut self, addr0: usize) {
46        // Skip qubits outside the computational subspace. Currently a no-op for
47        // loss (the `x` below is already skipped and `measure` returns `None`),
48        // but leaked qubits must not be re-zeroed, and this short-cuts both.
49        if self.is_inactive(addr0) {
50            return;
51        }
52
53        let m = self.measure(addr0);
54
55        // A reset is not a measurement in stim's model: drop the record
56        // entry that the internal `measure` just pushed so the reset is
57        // measurement-record-neutral.
58        self.measurement_record.pop();
59
60        if let Some(true) = m {
61            self.x(addr0);
62        }
63    }
64}