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
use crate::{states::StateData::Mixed, StateData};
use crate::{Process, ProcessData, C64};
use num_traits::{One, Zero};
use rand::Rng;
use std::iter::Iterator;
use crate::linalg::Trace;
use crate::State;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub enum Instrument {
Effects(Vec<Process>),
ZMeasurement {
pr_readout_error: f64,
},
}
impl Instrument {
pub fn sample(&self, idx_qubits: &[usize], state: &State) -> (usize, State) {
match self {
Instrument::Effects(ref effects) => sample_effects(effects, idx_qubits, state),
Instrument::ZMeasurement { pr_readout_error } => {
if idx_qubits.len() != 1 {
panic!("Z-basis measurement instruments only supported for single qubits.");
}
let idx_target = idx_qubits[0];
match state.data {
StateData::Pure(_) | StateData::Mixed(_) => {
let ideal_z_meas = Instrument::Effects(vec![
Process {
n_qubits: 1,
data: ProcessData::KrausDecomposition(array![[
[C64::one(), C64::zero()],
[C64::zero(), C64::zero()]
]]),
},
Process {
n_qubits: 1,
data: ProcessData::KrausDecomposition(array![[
[C64::zero(), C64::zero()],
[C64::zero(), C64::one()]
]]),
},
]);
let (result, new_state) = ideal_z_meas.sample(idx_qubits, state);
let result = (result == 1) ^ rand::thread_rng().gen_bool(*pr_readout_error);
(if result { 1 } else { 0 }, new_state)
}
StateData::Stabilizer(ref tableau) => {
let mut new_tableau = tableau.clone();
let result = new_tableau.meas_mut(idx_target)
^ rand::thread_rng().gen_bool(*pr_readout_error);
(
if result { 1 } else { 0 },
State {
n_qubits: state.n_qubits,
data: StateData::Stabilizer(new_tableau),
},
)
}
}
}
}
}
pub fn as_json(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}
fn sample_effects(effects: &[Process], idx_qubits: &[usize], state: &State) -> (usize, State) {
let mut possible_outcomes = effects
.iter()
.enumerate()
.map(|(idx, effect)| {
let output_state = effect.apply_to(idx_qubits, state).unwrap();
let tr = (&output_state).trace();
(idx, output_state, tr.norm())
})
.collect::<Vec<_>>();
assert!(
possible_outcomes.iter().any(|post_state| post_state.1.trace().norm() >= 1e-10),
"Expected output of applying instrument to be nonzero trace.\nInstrument effects:\n{:?}\n\nInput state:\n{}\n\nPostselected states:\n{:?}",
effects, state, possible_outcomes
);
let mut rng = rand::thread_rng();
let random_sample: f64 = rng.gen();
for (idx, cum_pr) in possible_outcomes
.iter()
.scan(0.0f64, |acc, (_idx, _, pr)| {
*acc += *pr;
Some(*acc)
})
.enumerate()
{
if random_sample < cum_pr {
possible_outcomes.retain(|(i, _, _)| idx == *i);
let (_, mut output_state, tr) = possible_outcomes.pop().unwrap();
if tr.abs() >= 1e-10 {
if let Mixed(ref rho) = output_state.data {
output_state.data = Mixed(rho * (1.0f64 / tr));
} else {
panic!("Couldn't renormalize, expected mixed output from instrument.");
}
}
assert!(
(output_state.trace() - 1.0).norm() <= 1e-10,
"Expected output of instrument to be trace 1."
);
return (idx, output_state);
}
}
let (idx, output_state, _) = possible_outcomes.pop().unwrap();
(idx, output_state)
}