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
use crate::chp_decompositions::ChpOperation;
use crate::common_matrices;
use crate::instrument::Instrument;
use crate::linalg::HasDagger;
use crate::processes::Process;
use crate::processes::{
    ProcessData,
    ProcessData::{KrausDecomposition, Unitary},
};
use crate::states::State;
use crate::states::StateData::Mixed;
use crate::StateData;
use crate::Tableau;
use crate::C64;
use num_traits::{One, Zero};

use serde::{Deserialize, Serialize};

/// A description of the noise that applies to the state of a quantum system
/// as the result of applying operations.
#[derive(Serialize, Deserialize, Debug)]
pub struct NoiseModel {
    /// The initial state that freshly allocated qubits start off in.
    pub initial_state: State,

    /// The process that applies to the state of a simulator
    /// when the `I` operation is called.
    pub i: Process,

    /// The process that applies to the state of a simulator
    /// when the `X` operation is called.
    pub x: Process,

    /// The process that applies to the state of a simulator
    /// when the `Y` operation is called.
    pub y: Process,

    /// The process that applies to the state of a simulator
    /// when the `Z` operation is called.
    pub z: Process,

    /// The process that applies to the state of a simulator
    /// when the `H` operation is called.
    pub h: Process,

    /// The process that applies to the state of a simulator
    /// when the `S` operation is called.
    pub s: Process,

    /// The process that applies to the state of a simulator
    /// when the `Adjoint S` operation is called.
    pub s_adj: Process,

    /// The process that applies to the state of a simulator
    /// when the `T` operation is called.
    pub t: Process,

    /// The process that applies to the state of a simulator
    /// when the `Adjoint T` operation is called.
    pub t_adj: Process,

    /// The process that applies to the state of a simulator
    /// when the `CNOT` operation is called.
    pub cnot: Process,

    /// The instrument that is used to the measure the state of a simulator
    /// in the $Z$-basis.
    pub z_meas: Instrument,
}

impl NoiseModel {
    /// Returns a serialization of this noise model as a JSON object.
    pub fn as_json(&self) -> String {
        serde_json::to_string(&self).unwrap()
    }

    /// Given the name of a built-in noise model, returns either that noise
    /// model if it exists, or an [`Err`] variant if no such model exists.
    ///
    /// Currently accepted noise model names:
    ///
    /// - `"ideal"`: A full-state noise model in which all operations are
    ///   ideal (that is, without errors).
    /// - `"ideal_stabilizer"`: A noise model in which all operations except
    ///   [`NoiseModel::t`] and [`NoiseModel::t_adj`] are ideal, and
    ///   represented in a form compatible with stabilizer simulation.
    ///
    /// # Example
    /// ```
    /// # use qdk_sim::NoiseModel;
    /// let noise_model = NoiseModel::get_by_name("ideal");
    /// ```
    pub fn get_by_name(name: &str) -> Result<NoiseModel, String> {
        match name {
            "ideal" => Ok(NoiseModel::ideal()),
            "ideal_stabilizer" => Ok(NoiseModel::ideal_stabilizer()),
            _ => Err(format!("Unrecognized noise model name {}.", name)),
        }
    }

    /// Returns a copy of the ideal noise model; that is, a noise model
    /// describing the case in which no noise acts on the quantum system.
    pub fn ideal() -> NoiseModel {
        let i = Process {
            n_qubits: 1,
            data: Unitary(common_matrices::i()),
        };
        let z = Process {
            n_qubits: 1,
            data: Unitary(common_matrices::z()),
        };
        let z_meas = Instrument::Effects(vec![
            Process {
                n_qubits: 1,
                data: KrausDecomposition(array![[
                    [C64::one(), C64::zero()],
                    [C64::zero(), C64::zero()]
                ]]),
            },
            Process {
                n_qubits: 1,
                data: KrausDecomposition(array![[
                    [C64::zero(), C64::zero()],
                    [C64::zero(), C64::one()]
                ]]),
            },
        ]);
        NoiseModel {
            initial_state: State {
                n_qubits: 1,
                data: Mixed((common_matrices::i() + common_matrices::z()) / 2.0),
            },
            i,
            x: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::x()),
            },
            y: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::y()),
            },
            z,
            h: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::h()),
            },
            t: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::t()),
            },
            t_adj: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::t().dag()),
            },
            s: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::s()),
            },
            s_adj: Process {
                n_qubits: 1,
                data: Unitary(common_matrices::s().dag()),
            },
            cnot: Process {
                n_qubits: 2,
                data: Unitary(common_matrices::cnot()),
            },
            z_meas,
        }
    }

    /// Returns a copy of the ideal noise model suitable for use with
    /// stabilizer simulation; that is, a noise model
    /// describing the case in which no noise acts on the quantum system, and
    /// in which all channels can be represented by CHP decompositions.
    pub fn ideal_stabilizer() -> NoiseModel {
        NoiseModel {
            initial_state: State {
                n_qubits: 1,
                data: StateData::Stabilizer(Tableau::new(1)),
            },
            i: Process {
                n_qubits: 1,
                data: ProcessData::Sequence(vec![]),
            },
            x: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![
                    ChpOperation::Hadamard(0),
                    ChpOperation::Phase(0),
                    ChpOperation::Phase(0),
                    ChpOperation::Hadamard(0),
                ]),
            },
            y: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![
                    ChpOperation::AdjointPhase(0),
                    ChpOperation::Hadamard(0),
                    ChpOperation::Phase(0),
                    ChpOperation::Phase(0),
                    ChpOperation::Hadamard(0),
                    ChpOperation::Phase(0),
                ]),
            },
            z: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![
                    ChpOperation::Phase(0),
                    ChpOperation::Phase(0),
                ]),
            },
            h: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![ChpOperation::Hadamard(0)]),
            },
            s: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![ChpOperation::Phase(0)]),
            },
            s_adj: Process {
                n_qubits: 1,
                data: ProcessData::ChpDecomposition(vec![ChpOperation::AdjointPhase(0)]),
            },
            t: Process {
                n_qubits: 1,
                data: ProcessData::Unsupported,
            },
            t_adj: Process {
                n_qubits: 1,
                data: ProcessData::Unsupported,
            },
            cnot: Process {
                n_qubits: 2,
                data: ProcessData::ChpDecomposition(vec![ChpOperation::Cnot(0, 1)]),
            },
            z_meas: Instrument::ZMeasurement {
                pr_readout_error: 0.0,
            },
        }
    }
}

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

    #[test]
    fn can_serialize_noise_model() {
        let noise_model = NoiseModel::ideal();
        let _json = serde_json::to_string(&noise_model);
    }
}