microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_eval/src/noise.rs
40lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #[derive(Copy, Clone, Debug)] |
| 5 | pub struct PauliNoise { |
| 6 | /// Pauli noise distribution for sampling. |
| 7 | /// When p is randomly distributed on [0.0, 1.0) the following error is applied: |
| 8 | /// X, when p is from [0.0, distribution[0]) |
| 9 | /// Y, when p is from [distribution[0], distribution[1]) |
| 10 | /// Z, when p is from [distribution[1], distribution[2]) |
| 11 | /// I, when p is from [distribution[2], 1.0) |
| 12 | pub distribution: [f64; 3], |
| 13 | } |
| 14 | |
| 15 | impl Default for PauliNoise { |
| 16 | fn default() -> Self { |
| 17 | Self { |
| 18 | distribution: [0.0; 3], |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl PauliNoise { |
| 24 | pub fn from_probabilities(px: f64, py: f64, pz: f64) -> Result<Self, String> { |
| 25 | let px_py = px + py; |
| 26 | let px_py_pz = px_py + pz; |
| 27 | if px < 0.0 || py < 0.0 || pz < 0.0 || px_py_pz > 1.0 { |
| 28 | Err("Incorrect Pauli noise probabilities.".to_string()) |
| 29 | } else { |
| 30 | Ok(Self { |
| 31 | distribution: [px, px_py, px_py_pz], |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[must_use] |
| 37 | pub fn is_noiseless(&self) -> bool { |
| 38 | self.distribution[2] <= f64::EPSILON |
| 39 | } |
| 40 | } |
| 41 | |