roqoqo 1.22.2

Rust Quantum Computing Toolkit by HQS
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
// Copyright © 2023-2024 HQS Quantum Simulations GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.

use super::SupportedVersion;
use std::collections::HashMap;
#[cfg(feature = "serialize")]
use struqture::spins::PlusMinusLindbladNoiseOperator;

/// Error model for noise that is only present on gate executions.
///
/// Adds additional noise when specific gates (identified by hqslang name and qubits acted on) are executed.
/// The noise is given in the form of a [struqture::spins::PlusMinusLindbladNoiseOperator] the same way it
/// is for the ContinuousDecoherence model.
/// Example:
///
/// ```
/// use roqoqo::noise_models::DecoherenceOnGateModel;
/// use struqture::spins::{PlusMinusLindbladNoiseOperator, PlusMinusProduct};
/// use struqture::prelude::*;
///
/// let mut noise_model = DecoherenceOnGateModel::new();
/// let mut lindblad_noise = PlusMinusLindbladNoiseOperator::new();
/// lindblad_noise.add_operator_product(
///    (PlusMinusProduct::new().z(0), PlusMinusProduct::new().z(0)),
///    0.9.into(),).unwrap();
/// lindblad_noise.add_operator_product(
///    (PlusMinusProduct::new().z(1), PlusMinusProduct::new().z(1)),
///    0.9.into(),).unwrap();
///
/// noise_model = noise_model.set_two_qubit_gate_error(
/// "CNOT", 0,1,
/// lindblad_noise
/// );
/// ```
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(from = "DecoherenceOnGateModelSerialize"))]
#[cfg_attr(feature = "serialize", serde(into = "DecoherenceOnGateModelSerialize"))]
pub struct DecoherenceOnGateModel {
    /// Extra noise for single qubit gates.
    single_qubit_gate_errors:
        HashMap<(String, usize), struqture::spins::PlusMinusLindbladNoiseOperator>,
    /// Extra noise for two qubit gates.
    two_qubit_gate_errors:
        HashMap<(String, (usize, usize)), struqture::spins::PlusMinusLindbladNoiseOperator>,
    /// Extra noise for three qubit gates.
    three_qubit_gate_errors:
        HashMap<(String, (usize, usize, usize)), struqture::spins::PlusMinusLindbladNoiseOperator>,
    /// Extra noise for multi qubit gates.
    multi_qubit_gate_errors:
        HashMap<(String, Vec<usize>), struqture::spins::PlusMinusLindbladNoiseOperator>,
}

#[cfg(feature = "json_schema")]
impl schemars::JsonSchema for DecoherenceOnGateModel {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "DecoherenceOnGateModel".into()
    }

    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        <DecoherenceOnGateModelSerialize>::json_schema(generator)
    }
}

#[cfg(feature = "serialize")]
type SingleQGateIndex = (String, usize);
#[cfg(feature = "serialize")]
type SingleQubitErrors = Vec<(
    SingleQGateIndex,
    struqture_1::spins::PlusMinusLindbladNoiseOperator,
)>;
#[cfg(feature = "serialize")]
type TwoQubitGateIndex = (String, (usize, usize));
#[cfg(feature = "serialize")]
type TwoQubitErrors = Vec<(
    TwoQubitGateIndex,
    struqture_1::spins::PlusMinusLindbladNoiseOperator,
)>;
#[cfg(feature = "serialize")]
type ThreeQubitGateIndex = (String, (usize, usize, usize));
#[cfg(feature = "serialize")]
type ThreeQubitErrors = Vec<(
    ThreeQubitGateIndex,
    struqture_1::spins::PlusMinusLindbladNoiseOperator,
)>;
#[cfg(feature = "serialize")]
type MultiQubitGateIndex = (String, Vec<usize>);
#[cfg(feature = "serialize")]
type MultiQubitErrors = Vec<(
    MultiQubitGateIndex,
    struqture_1::spins::PlusMinusLindbladNoiseOperator,
)>;
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "json_schema",
    derive(schemars::JsonSchema),
    schemars(deny_unknown_fields)
)]
#[cfg(feature = "serialize")]
struct DecoherenceOnGateModelSerialize {
    /// Extra noise for single qubit gates.
    single_qubit_gate_errors: SingleQubitErrors,
    /// Extra noise for two qubit gates.
    two_qubit_gate_errors: TwoQubitErrors,
    /// Extra noise for three qubit gates.
    three_qubit_gate_errors: ThreeQubitErrors,
    /// Extra noise for multi qubit gates.
    multi_qubit_gate_errors: MultiQubitErrors,
}

#[cfg(feature = "serialize")]
impl From<DecoherenceOnGateModel> for DecoherenceOnGateModelSerialize {
    fn from(value: DecoherenceOnGateModel) -> Self {
        let single_qubit_gate_errors: SingleQubitErrors =
            value.single_qubit_gate_errors.into_iter().map(|(key, value)|(key, value.to_struqture_1().expect("Failed to convert PlusMinusLindbladNoiseOperator to struqture 1.x for serialization."))).collect();
        let two_qubit_gate_errors: TwoQubitErrors =
            value.two_qubit_gate_errors.into_iter().map(|(key, value)|(key, value.to_struqture_1().expect("Failed to convert PlusMinusLindbladNoiseOperator to struqture 1.x for serialization."))).collect();
        let three_qubit_gate_errors: ThreeQubitErrors =
            value.three_qubit_gate_errors.into_iter().map(|(key, value)|(key, value.to_struqture_1().expect("Failed to convert PlusMinusLindbladNoiseOperator to struqture 1.x for serialization."))).collect();
        let multi_qubit_gate_errors: MultiQubitErrors =
            value.multi_qubit_gate_errors.into_iter().map(|(key, value)|(key, value.to_struqture_1().expect("Failed to convert PlusMinusLindbladNoiseOperator to struqture 1.x for serialization."))).collect();
        DecoherenceOnGateModelSerialize {
            single_qubit_gate_errors,
            two_qubit_gate_errors,
            three_qubit_gate_errors,
            multi_qubit_gate_errors,
        }
    }
}

#[cfg(feature = "serialize")]
impl From<DecoherenceOnGateModelSerialize> for DecoherenceOnGateModel {
    fn from(value: DecoherenceOnGateModelSerialize) -> Self {
        let single_qubit_gate_errors: HashMap<
            (String, usize),
            struqture::spins::PlusMinusLindbladNoiseOperator,
        > = value.single_qubit_gate_errors.into_iter().map(|(key, value)|(key, PlusMinusLindbladNoiseOperator::from_struqture_1(&value).expect("Failed to convert PlusMinusLindbladNoiseOperator from struqture 1.x for serialization."))).collect();
        let two_qubit_gate_errors: HashMap<
            (String, (usize, usize)),
            struqture::spins::PlusMinusLindbladNoiseOperator,
        > = value.two_qubit_gate_errors.into_iter().map(|(key, value)|(key, PlusMinusLindbladNoiseOperator::from_struqture_1(&value).expect("Failed to convert PlusMinusLindbladNoiseOperator from struqture 1.x for serialization."))).collect();
        let three_qubit_gate_errors: HashMap<
            (String, (usize, usize, usize)),
            struqture::spins::PlusMinusLindbladNoiseOperator,
        > = value.three_qubit_gate_errors.into_iter().map(|(key, value)|(key, PlusMinusLindbladNoiseOperator::from_struqture_1(&value).expect("Failed to convert PlusMinusLindbladNoiseOperator from struqture 1.x for serialization."))).collect();
        let multi_qubit_gate_errors: HashMap<
            (String, Vec<usize>),
            struqture::spins::PlusMinusLindbladNoiseOperator,
        > = value.multi_qubit_gate_errors.into_iter().map(|(key, value)|(key, PlusMinusLindbladNoiseOperator::from_struqture_1(&value).expect("Failed to convert PlusMinusLindbladNoiseOperator from struqture 1.x for serialization."))).collect();
        DecoherenceOnGateModel {
            single_qubit_gate_errors,
            two_qubit_gate_errors,
            three_qubit_gate_errors,
            multi_qubit_gate_errors,
        }
    }
}

impl SupportedVersion for DecoherenceOnGateModel {
    fn minimum_supported_roqoqo_version(&self) -> (u32, u32, u32) {
        (1, 6, 0)
    }
}

impl DecoherenceOnGateModel {
    /// Creates a new DecoherenceOnGateModel with default values.
    pub fn new() -> Self {
        Self {
            single_qubit_gate_errors: HashMap::new(),
            two_qubit_gate_errors: HashMap::new(),
            three_qubit_gate_errors: HashMap::new(),
            multi_qubit_gate_errors: HashMap::new(),
        }
    }

    /// Sets extra noise for a single qubit gate.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `qubit` - The qubit the gate acts on.
    /// * `noise_operator` - The noise affecting system when gate is applied.
    ///
    /// # Returns
    ///
    /// `Self` - The error model with the new noise on gate set.
    pub fn set_single_qubit_gate_error(
        mut self,
        gate: &str,
        qubit: usize,
        noise_operator: struqture::spins::PlusMinusLindbladNoiseOperator,
    ) -> Self {
        self.single_qubit_gate_errors
            .insert((gate.to_string(), qubit), noise_operator);
        self
    }

    /// Returns the extra noise for a single qubit gate, if it exists.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `qubit` - The qubit the gate acts on.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn get_single_qubit_gate_error(
        &self,
        gate: &str,
        qubit: usize,
    ) -> Option<&struqture::spins::PlusMinusLindbladNoiseOperator> {
        self.single_qubit_gate_errors
            .get(&(gate.to_string(), qubit))
    }

    /// Sets extra noise for a two qubit gate.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `control` - Controlling qubit.
    /// * `target` - Target qubit.
    /// * `noise_operator` - The noise affecting system when gate is applied.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn set_two_qubit_gate_error(
        mut self,
        gate: &str,
        control: usize,
        target: usize,
        noise_operator: struqture::spins::PlusMinusLindbladNoiseOperator,
    ) -> Self {
        self.two_qubit_gate_errors
            .insert((gate.to_string(), (control, target)), noise_operator);
        self
    }

    /// Returns the extra noise for a two qubit gate, if it exists.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `control` - Controlling qubit.
    /// * `target` - Target qubit.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn get_two_qubit_gate_error(
        &self,
        gate: &str,
        control: usize,
        target: usize,
    ) -> Option<&struqture::spins::PlusMinusLindbladNoiseOperator> {
        self.two_qubit_gate_errors
            .get(&(gate.to_string(), (control, target)))
    }

    /// Sets extra noise for a three qubit gate.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `control0` - First controlling qubit.
    /// * `control1` - Second controlling qubit.
    /// * `target` - Target qubit.
    /// * `noise_operator` - The noise affecting system when gate is applied.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn set_three_qubit_gate_error(
        mut self,
        gate: &str,
        control0: usize,
        control1: usize,
        target: usize,
        noise_operator: struqture::spins::PlusMinusLindbladNoiseOperator,
    ) -> Self {
        self.three_qubit_gate_errors.insert(
            (gate.to_string(), (control0, control1, target)),
            noise_operator,
        );
        self
    }

    /// Returns the extra noise for a two qubit gate, if it exists.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `control0` - First controlling qubit.
    /// * `control1` - Second controlling qubit.
    /// * `target` - Target qubit.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn get_three_qubit_gate_error(
        &self,
        gate: &str,
        control0: usize,
        control1: usize,
        target: usize,
    ) -> Option<&struqture::spins::PlusMinusLindbladNoiseOperator> {
        self.three_qubit_gate_errors
            .get(&(gate.to_string(), (control0, control1, target)))
    }

    /// Sets extra noise for a multi qubit gate.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `qubits` - A vector of qubit indices.
    /// * `noise_operator` - The noise affecting system when gate is applied.
    ///
    /// # Returns
    ///
    /// `Self` - The error model with the new noise on gate set.
    pub fn set_multi_qubit_gate_error(
        mut self,
        gate: &str,
        qubits: Vec<usize>,
        noise_operator: struqture::spins::PlusMinusLindbladNoiseOperator,
    ) -> Self {
        self.multi_qubit_gate_errors
            .insert((gate.to_string(), qubits), noise_operator);
        self
    }

    /// Returns the extra noise for a multi qubit gate, if it exists.
    ///
    /// # Arguments
    ///
    /// * `gate` - The name of the gate.
    /// * `qubits` - A vector of qubit indices.
    ///
    /// # Returns
    ///
    /// `Option<struqture::spins::PlusMinusLindbladNoiseOperator>` - The error model applied when gate is applied.
    pub fn get_multi_qubit_gate_error(
        &self,
        gate: &str,
        qubits: Vec<usize>,
    ) -> Option<&struqture::spins::PlusMinusLindbladNoiseOperator> {
        self.multi_qubit_gate_errors
            .get(&(gate.to_string(), qubits))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "json_schema")]
    use jsonschema::Validator;
    use struqture::spins::PlusMinusLindbladNoiseOperator;

    #[test]
    fn test_decoherence_on_gate_model_single() {
        let mut noise_model = DecoherenceOnGateModel::new();
        noise_model = noise_model.set_single_qubit_gate_error(
            "RotateX",
            0,
            PlusMinusLindbladNoiseOperator::new(),
        );
        assert_eq!(
            noise_model.get_single_qubit_gate_error("RotateX", 0),
            Some(&PlusMinusLindbladNoiseOperator::new())
        );
    }

    #[test]
    fn test_decoherence_on_gate_model_two() {
        let mut noise_model = DecoherenceOnGateModel::new();
        noise_model = noise_model.set_two_qubit_gate_error(
            "CNOT",
            0,
            1,
            PlusMinusLindbladNoiseOperator::new(),
        );
        assert_eq!(
            noise_model.get_two_qubit_gate_error("CNOT", 0, 1),
            Some(&PlusMinusLindbladNoiseOperator::new())
        );
    }

    #[test]
    fn test_decoherence_on_gate_model_three() {
        let mut noise_model = DecoherenceOnGateModel::new();
        noise_model = noise_model.set_three_qubit_gate_error(
            "ControlledControlledPauliZ",
            0,
            1,
            2,
            PlusMinusLindbladNoiseOperator::new(),
        );
        assert_eq!(
            noise_model.get_three_qubit_gate_error("ControlledControlledPauliZ", 0, 1, 2),
            Some(&PlusMinusLindbladNoiseOperator::new())
        );
    }

    #[test]
    fn test_decoherence_on_gate_model_mulit() {
        let mut noise_model = DecoherenceOnGateModel::new();
        noise_model = noise_model.set_multi_qubit_gate_error(
            "MultiQubitMS",
            vec![0, 1, 2, 3],
            PlusMinusLindbladNoiseOperator::new(),
        );
        assert_eq!(
            noise_model.get_multi_qubit_gate_error("MultiQubitMS", vec![0, 1, 2, 3]),
            Some(&PlusMinusLindbladNoiseOperator::new())
        );
    }

    #[cfg(feature = "serialize")]
    #[test]
    fn test_json_serialization() {
        let mut noise_model = DecoherenceOnGateModel::new();
        noise_model = noise_model.set_single_qubit_gate_error(
            "RotateX",
            0,
            PlusMinusLindbladNoiseOperator::new(),
        );
        let json_str = serde_json::to_string(&noise_model).unwrap();
        let deserialized_noise_model: DecoherenceOnGateModel =
            serde_json::from_str(&json_str).unwrap();
        assert_eq!(noise_model, deserialized_noise_model);
    }

    #[cfg(feature = "json_schema")]
    #[test]
    fn test_json_schema_feature() {
        let mut model = DecoherenceOnGateModel::new();
        model =
            model.set_single_qubit_gate_error("RotateX", 0, PlusMinusLindbladNoiseOperator::new());
        let schema = schemars::schema_for!(DecoherenceOnGateModel);
        let schema_checker =
            Validator::new(&serde_json::to_value(&schema).unwrap()).expect("schema is valid");
        let value = serde_json::to_value(model).unwrap();
        let val = match value {
            serde_json::Value::Object(ob) => ob,
            _ => panic!(),
        };
        let value: serde_json::Value = serde_json::to_value(val).unwrap();
        let validation = schema_checker.validate(&value);
        assert!(validation.is_ok());
    }
}