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
use itertools::Itertools;
use ndarray::{Array, Array2, Array3, ArrayView2, Axis};
use rand::{distributions::WeightedIndex, prelude::Distribution, thread_rng};
use crate::{
chp_decompositions::ChpOperation, linalg::ConjBy, log, log_as_err, Pauli, Process,
ProcessData::*, State, StateData::*, Tableau, C64,
};
use super::promote_pauli_channel;
impl Process {
pub fn apply(&self, state: &State) -> Result<State, String> {
if state.n_qubits != self.n_qubits {
return Err(format!(
"Channel acts on {} qubits, but was applied to {}-qubit state.",
self.n_qubits, state.n_qubits
));
}
match &self.data {
Unitary(u) => apply_unitary(u, state),
KrausDecomposition(ks) => apply_kraus_decomposition(ks, state),
MixedPauli(paulis) => apply_pauli_channel(paulis, state),
Sequence(processes) => {
let mut acc_state = state.clone();
for process in processes {
acc_state = process.apply(state)?;
}
Ok(acc_state)
}
ChpDecomposition(_operations) => todo!(),
Unsupported => Err("Unsupported quantum process.".to_string()),
}
}
pub fn apply_to(&self, idx_qubits: &[usize], state: &State) -> Result<State, String> {
if let Sequence(channels) = &self.data {
let mut acc_state = state.clone();
for channel in channels {
acc_state = channel.apply_to(idx_qubits, &acc_state)?;
}
return Ok(acc_state);
}
if state.n_qubits < self.n_qubits {
return log_as_err(format!(
"Channel acts on {} qubits, but a state on only {} qubits was given.\n\nChannel:\n{:?}\n\nState:\n{:?}",
self.n_qubits, state.n_qubits, self, state
));
}
if idx_qubits.iter().unique().count() < idx_qubits.len() {
return log_as_err(format!(
"List of qubit indices {:?} contained repeated elements.",
idx_qubits
));
}
if idx_qubits.len() != self.n_qubits {
return log_as_err(format!(
"Qubit indices were specified as {:?}, but this channel only acts on {} qubits.",
idx_qubits, self.n_qubits
));
}
if let ChpDecomposition(operations) = &self.data {
if let Stabilizer(tableau) = &state.data {
return apply_chp_decomposition_to(operations, state.n_qubits, idx_qubits, tableau);
}
}
match self.n_qubits {
1 => {
if state.n_qubits == 1 {
self.apply(state)
} else {
self.extend_one_to_n(idx_qubits[0], state.n_qubits)
.apply(state)
}
}
2 => self
.extend_two_to_n(idx_qubits[0], idx_qubits[1], state.n_qubits)
.apply(state),
_ => {
log(&format!(
"Expanding {}-qubit channels is not yet implemented.",
self.n_qubits
));
unimplemented!("");
}
}
}
}
fn apply_chp_decomposition_to(
operations: &[ChpOperation],
n_qubits: usize,
idx_qubits: &[usize],
tableau: &Tableau,
) -> Result<State, String> {
let mut new_tableau = tableau.clone();
for operation in operations {
match *operation {
ChpOperation::Phase(idx) => new_tableau.apply_s_mut(idx_qubits[idx]),
ChpOperation::AdjointPhase(idx) => new_tableau.apply_s_adj_mut(idx_qubits[idx]),
ChpOperation::Hadamard(idx) => new_tableau.apply_h_mut(idx_qubits[idx]),
ChpOperation::Cnot(idx_control, idx_target) => {
new_tableau.apply_cnot_mut(idx_qubits[idx_control], idx_qubits[idx_target])
}
};
}
Ok(State {
n_qubits,
data: Stabilizer(new_tableau),
})
}
pub(crate) fn apply_unitary(u: &Array2<C64>, state: &State) -> Result<State, String> {
Ok(State {
n_qubits: state.n_qubits,
data: match &state.data {
Pure(psi) => Pure(u.dot(psi)),
Mixed(rho) => Mixed(rho.conjugate_by(&u.into())),
Stabilizer(_tableau) => {
return Err(
"TODO: Promote stabilizer state to state vector and recurse.".to_string(),
)
}
},
})
}
pub(crate) fn apply_kraus_decomposition(ks: &Array3<C64>, state: &State) -> Result<State, String> {
Ok(State {
n_qubits: state.n_qubits,
data: match &state.data {
Pure(psi) => {
if ks.shape()[0] == 1 {
Pure({
let k: ArrayView2<C64> = ks.slice(s![0, .., ..]);
k.dot(psi)
})
} else {
apply_kraus_decomposition(ks, &state.to_mixed())?.data
}
}
Mixed(rho) => Mixed({
let mut sum: Array2<C64> = Array::zeros((rho.shape()[0], rho.shape()[1]));
for k in ks.axis_iter(Axis(0)) {
sum = sum + rho.conjugate_by(&k);
}
sum
}),
Stabilizer(_tableau) => {
return Err(
"TODO: Promote stabilizer state to state vector and recurse.".to_string(),
)
}
},
})
}
pub(crate) fn apply_pauli_channel(
paulis: &[(f64, Vec<Pauli>)],
state: &State,
) -> Result<State, String> {
Ok(State {
n_qubits: state.n_qubits,
data: match &state.data {
Pure(_) | Mixed(_) => {
let promoted = promote_pauli_channel(paulis);
return promoted.apply(state);
}
Stabilizer(tableau) => {
let mut new_tableau = tableau.clone();
let weighted = WeightedIndex::new(paulis.iter().map(|(pr, _)| pr)).unwrap();
let idx = weighted.sample(&mut thread_rng());
let pauli = &(&paulis)[idx].1;
for (idx_qubit, p) in pauli.iter().enumerate() {
match p {
Pauli::I => (),
Pauli::X => new_tableau.apply_x_mut(idx_qubit),
Pauli::Y => new_tableau.apply_y_mut(idx_qubit),
Pauli::Z => new_tableau.apply_z_mut(idx_qubit),
}
}
Stabilizer(new_tableau)
}
},
})
}