paulistrings/channel/ptm/
noise.rs1use super::{assert_distinct, assert_probability, sparse_storage, PtmChannel, PtmKind, PtmStorage};
4use num_complex::Complex64;
5
6impl PtmChannel {
7 pub fn depolarizing(qubit: usize, p: f64) -> Self {
13 assert_probability("depolarizing", "p", p);
14 let scale = 1.0 - 4.0 * p / 3.0;
15 Self::diagonal(&[qubit], vec![1.0, scale, scale, scale], PtmKind::Depolarizing)
16 }
17
18 pub fn dephasing(qubit: usize, p: f64) -> Self {
24 assert_probability("dephasing", "p", p);
25 let scale = 1.0 - 2.0 * p;
26 Self::diagonal(&[qubit], vec![1.0, scale, 1.0, scale], PtmKind::Dephasing)
27 }
28
29 pub fn pauli_channel(qubit: usize, px: f64, py: f64, pz: f64) -> Self {
37 for (name, value) in [("px", px), ("py", py), ("pz", pz)] {
38 assert_probability("pauli_channel", name, value);
39 }
40 let total = px + py + pz;
41 assert!(
42 total <= 1.0,
43 "PtmChannel::pauli_channel: px + py + pz must be at most 1 (got {total})"
44 );
45 Self::diagonal(
46 &[qubit],
47 vec![1.0, 1.0 - 2.0 * (py + pz), 1.0 - 2.0 * (px + py), 1.0 - 2.0 * (px + pz)],
48 PtmKind::PauliChannel,
49 )
50 }
51
52 pub fn depolarizing_2q(q0: usize, q1: usize, p: f64) -> Self {
60 assert_distinct("depolarizing_2q", q0, q1);
61 assert_probability("depolarizing_2q", "p", p);
62 let mut scale = vec![1.0 - 16.0 * p / 15.0; 16];
63 scale[0] = 1.0;
64 Self::diagonal(&[q0, q1], scale, PtmKind::Depolarizing2q)
65 }
66
67 pub fn amplitude_damping(qubit: usize, gamma: f64) -> Self {
75 assert_probability("amplitude_damping", "gamma", gamma);
76 let transverse = Complex64::new((1.0 - gamma).sqrt(), 0.0);
77 let storage = sparse_storage(vec![
78 ((0, 0), Complex64::new(1.0, 0.0)),
79 ((0, 2), Complex64::new(gamma, 0.0)),
80 ((1, 1), transverse),
81 ((2, 2), Complex64::new(1.0 - gamma, 0.0)),
82 ((3, 3), transverse),
83 ]);
84 Self::new(&[qubit], storage, PtmKind::AmplitudeDamping)
85 }
86
87 fn diagonal(qubits: &[usize], scale: Vec<f64>, kind: PtmKind) -> Self {
88 Self::new(qubits, PtmStorage::Diagonal { scale: scale.into_boxed_slice() }, kind)
89 }
90}
91
92#[cfg(test)]
93mod tests;