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
use std::collections::HashMap;
use nalgebra::{Complex, DMatrix, DVector};
use ndarray::Array2;
use rand::seq::SliceRandom;
use rand::thread_rng;
use roqoqo::operations::{
DefinitionBit, GateOperation, PauliZ, PragmaRepeatedMeasurement, TwoQubitGateOperation,
};
use roqoqo::prelude::*;
use roqoqo::{
measurements::{BasisRotation, BasisRotationInput},
operations::SingleQubitGateOperation,
Circuit,
};
pub fn prepare_monte_carlo_gate_test(
gate: GateOperation,
preparation_gates: Vec<SingleQubitGateOperation>,
basis_rotations_gates: Vec<SingleQubitGateOperation>,
two_qubit_gate: Option<TwoQubitGateOperation>,
number_stochastic_tests: usize,
number_projective_measurement: usize,
) -> (BasisRotation, HashMap<String, f64>) {
if let Some(x) = two_qubit_gate {
if !(x.control() == &0 && x.target() == &1 || x.control() == &1 && x.target() == &0) {
panic!("Provided two_qubit_gate does not act on qubits 0 and 1")
}
}
let number_qubits = match gate.involved_qubits() {
InvolvedQubits::Set(x) => x.len(),
_ => panic!("Tested gate has no well defined number of qubits"),
};
let gate_matrix = ndarray_to_nalgebra(gate.unitary_matrix().unwrap());
let id_matrix: DMatrix<Complex<f64>> = DMatrix::identity(2, 2);
let mut starting_vec: DVector<Complex<f64>> = DVector::from_element(
2_usize.pow(number_qubits as u32),
Complex::<f64>::new(0.0, 0.0),
);
starting_vec[1] = Complex::<f64>::new(1.0, 0.0);
let mut expected_values: HashMap<String, f64> = HashMap::new();
let mut measurement_input = BasisRotationInput::new(number_qubits, false);
let mut measurement_circuits: Vec<Circuit> = Vec::new();
let mut rng = thread_rng();
for i in 0..number_stochastic_tests {
let mut init_circuit = Circuit::new();
let mut meas_circuit = Circuit::new();
meas_circuit += DefinitionBit::new(format!("ro_{}", i), number_qubits, true);
let mut pauli_product_mask: Vec<usize> = Vec::new();
let prep = preparation_gates.choose(&mut rng).unwrap();
let meas = basis_rotations_gates.choose(&mut rng).unwrap();
let involve_qubit: bool = rand::random();
let mut init_matrix: DMatrix<Complex<f64>> =
ndarray_to_nalgebra(prep.unitary_matrix().unwrap());
init_circuit += prep.clone();
let mut basis_rot_matrix: DMatrix<Complex<f64>> = if involve_qubit {
pauli_product_mask.push(0);
meas_circuit += meas.clone();
ndarray_to_nalgebra(meas.unitary_matrix().unwrap())
} else {
id_matrix.clone()
};
let mut measurement_matrix = if involve_qubit {
ndarray_to_nalgebra(PauliZ::new(0).unitary_matrix().unwrap())
} else {
id_matrix.clone()
};
for n in 1..number_qubits {
let prep = preparation_gates.choose(&mut rng).unwrap();
let meas = basis_rotations_gates.choose(&mut rng).unwrap();
let involve_qubit: bool = rand::random();
let mut mapping: HashMap<usize, usize> = HashMap::new();
let _ = mapping.insert(0, n);
init_matrix =
ndarray_to_nalgebra(prep.unitary_matrix().unwrap()).kronecker(&init_matrix);
init_circuit += prep.remap_qubits(&mapping).unwrap();
if involve_qubit {
pauli_product_mask.push(n);
basis_rot_matrix = ndarray_to_nalgebra(meas.unitary_matrix().unwrap())
.kronecker(&basis_rot_matrix);
meas_circuit += meas.remap_qubits(&mapping).unwrap();
measurement_matrix = ndarray_to_nalgebra(PauliZ::new(0).unitary_matrix().unwrap())
.kronecker(&measurement_matrix);
} else {
basis_rot_matrix = id_matrix.kronecker(&basis_rot_matrix);
measurement_matrix = id_matrix.kronecker(&measurement_matrix);
}
}
meas_circuit += PragmaRepeatedMeasurement::new(
format!("ro_{}", i),
number_projective_measurement,
None,
);
let j = measurement_input
.add_pauli_product(format!("ro_{}", i), pauli_product_mask)
.unwrap();
let mut linear_map: HashMap<usize, f64> = HashMap::new();
linear_map.insert(j, 1.0);
measurement_input
.add_linear_exp_val(format!("exp_val_{}", i), linear_map)
.unwrap();
let circuit = init_circuit + gate.clone() + meas_circuit;
measurement_circuits.push(circuit);
let expected_value = (init_matrix.conjugate().transpose()
* gate_matrix.clone().adjoint()
* basis_rot_matrix.adjoint()
* measurement_matrix
* basis_rot_matrix
* gate_matrix.clone()
* init_matrix)[(0, 0)];
let _ = expected_values.insert(format!("exp_val_{}", i), expected_value.re);
}
let measurement = BasisRotation {
circuits: measurement_circuits,
input: measurement_input,
constant_circuit: None,
};
(measurement, expected_values)
}
fn ndarray_to_nalgebra(input: Array2<Complex<f64>>) -> DMatrix<Complex<f64>> {
let shape = input.shape();
let matrix: DMatrix<Complex<f64>> =
DMatrix::from_iterator(shape[0], shape[1], input.iter().cloned());
matrix
}