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
// Copyright © 2021 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::*;
use ndarray::{Array1, Array2};
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
/// Collected information for executing a basis rotation measurement.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct BasisRotation {
/// Constant Circuit that is executed before each Circuit in circuits.
pub constant_circuit: Option<Circuit>,
/// Collection of quantum circuits for the separate basis rotations.
pub circuits: Vec<Circuit>,
/// Additional input information required for measurement.
pub input: BasisRotationInput,
}
impl Measure for BasisRotation {
/// Returns the constant Circuit that is executed before each Circuit in circuits.
///
/// # Returns
///
/// * `&Option<Circuit` - The constant Circuit (None if not defined).
fn constant_circuit(&self) -> &Option<Circuit> {
&self.constant_circuit
}
/// Returns iterator over circuits for measurement.
///
/// # Returns
///
/// * `Box<dyn Iterator<Item = &'a Circuit> + 'a>` - The quantum circuits.
fn circuits<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Circuit> + 'a> {
Box::new(self.circuits.iter())
}
/// Returns clone of Measurement with symbolic parameters replaced.
///
/// # Arguments
///
/// * `substituted_parameters` - The HashMap containing the substitutions to use in the Circuit.
///
/// # Returns
///
/// * `Ok(Self)` - The Circuits with the parameters substituted.
/// * `Err(RoqoqoError)` - The subsitution failed.
///
fn substitute_parameters(
&self,
substituted_parameters: HashMap<String, f64>,
) -> Result<Self, RoqoqoError> {
let mut calculator = qoqo_calculator::Calculator::new();
for (name, val) in substituted_parameters.iter() {
calculator.set_variable(name, *val)
}
let new_constant_circuit = match &self.constant_circuit {
None => None,
Some(c) => Some(c.substitute_parameters(&mut calculator)?),
};
let mut new_circuits = Vec::new();
for circ in self.circuits.iter() {
let mut calculator = qoqo_calculator::Calculator::new();
for (name, val) in substituted_parameters.iter() {
calculator.set_variable(name, *val)
}
new_circuits.push(circ.substitute_parameters(&mut calculator)?)
}
Ok(Self {
constant_circuit: new_constant_circuit,
circuits: new_circuits,
input: self.input.clone(),
})
}
}
impl MeasureExpectationValues for BasisRotation {
// TODO add optional device later for use with flipped measurement
#[allow(unused_variables)]
/// Executes the basis rotation measurement.
///
/// # Arguments
///
/// * `bit_registers` - The classical bit registers as a HashMap with the register name as key.
/// * `float_registers` - The classical float registers as a HashMap with the register name as key.
/// * `complex_registers` - The classical complex registers as a HashMap with the register name as key.
///
/// # Returns
///
/// * `Ok(Some(HashMap<String, f64>))` - The measurement has been evaluated successfully. The HashMap contains the measured expectation values.
/// * `Ok(None)` - The measurement did not fail but is incomplete. A new round of measurements is needed
/// * `Err([RoqoqoError::BasisRotationMeasurementError])` - An error occured in basis rotation measurement.
///
fn evaluate(
&self,
bit_registers: HashMap<String, BitOutputRegister>,
float_registers: HashMap<String, FloatOutputRegister>,
complex_registers: HashMap<String, ComplexOutputRegister>,
) -> Result<Option<HashMap<String, f64>>, RoqoqoError> {
// todo replace with actual input
let measurement_fidelities = vec![1.0; self.input.number_qubits];
// Setting up measurement correction factors for flipped measurement
let mut measurement_correction_factors: HashMap<String, Vec<f64>> = HashMap::new();
let flipped_and_extension: Vec<(bool, &'static str)>;
if self.input.use_flipped_measurement {
// helper vector to iterate over when evaluating the pauli products
flipped_and_extension = vec![(false, ""), (true, "_flipped")];
for (name, pauli_product_mask) in self.input.pauli_product_qubit_masks.iter() {
let mut measurement_correction_factor: Vec<f64> =
(0..self.input.number_pauli_products)
.into_iter()
.map(|_| 1.0)
.collect();
for (pp_index, indices) in pauli_product_mask.iter() {
if !indices.is_empty() {
for i in indices {
measurement_correction_factor[*pp_index] *= measurement_fidelities[*i]
}
}
}
measurement_correction_factors.insert(name.clone(), measurement_correction_factor);
}
} else {
flipped_and_extension = vec![(false, "")];
}
let mut pauli_product_dict: HashMap<String, Array1<f64>> = HashMap::new();
for (register_name, mask) in self.input.pauli_product_qubit_masks.iter() {
for (flip_measurement, extension) in flipped_and_extension.iter() {
let register = bit_registers
.get(&format!("{}{}", register_name.as_str(), extension))
.ok_or(RoqoqoError::BasisRotationMeasurementError {
msg: format!(
"bit register {}{} not found",
register_name.as_str(),
extension
),
})?;
let mut single_shot_pauli_products: Array2<f64> =
Array2::zeros((register.len(), self.input.number_pauli_products));
for (index, mask_val) in mask.iter() {
if mask_val.is_empty() {
single_shot_pauli_products.column_mut(*index).fill(1.0);
} else {
// Accessing column of single_shot_pauli_products that corresponds to pauli product designated by index
let mut column = single_shot_pauli_products.column_mut(*index);
// Iterate over all single shot readouts for all qubits and construct Pauli Product
for (row_index, values) in register.iter().enumerate() {
// Determine the value of the pauli product with the parity of the number of 0 and 1 measurements of the qubits
// false is even parity and true is odd parity
let mut parity = false;
for i in mask_val.iter() {
// For flipped readout a false (0) qubit measurement will flip the parity
// For a not-flipped measurement a true (1) qubit measurement will flip the parity
if values[*i] ^ flip_measurement {
parity = !parity
}
} // Map even parity measurement result to 1 and odd parity result to -1
column[row_index] = match parity {
false => 1.0,
true => -1.0,
};
}
}
}
let mut pauli_products_tmp: Array1<f64> =
Array1::zeros(self.input.number_pauli_products);
for i in 0..self.input.number_pauli_products {
pauli_products_tmp[i] = single_shot_pauli_products.column(i).mean().ok_or(
RoqoqoError::BasisRotationMeasurementError {
msg: format!(
"Column {} out of index for sinlge_shot_pauli_products",
i
),
},
)?;
}
pauli_product_dict.insert(
format!("{}{}", register_name.as_str(), extension),
pauli_products_tmp,
);
}
}
let mut pauli_products: Array1<f64> = Array1::zeros(self.input.number_pauli_products);
for (register_name, _) in self.input.pauli_product_qubit_masks.iter() {
if !register_name.ends_with("flipped") {
// Create temporary averaged vector of pauli_products
// Averaging between normal and flipped readout when flipped measurement is used
if self.input.use_flipped_measurement {
let tmp_pauli_products = (&pauli_product_dict
.get(register_name.as_str())
.ok_or(RoqoqoError::BasisRotationMeasurementError {
msg: format!("Register name {} not fount", register_name),
})?
.view()
+ &pauli_product_dict
.get(format!("{}_flipped", register_name).as_str())
.ok_or(RoqoqoError::BasisRotationMeasurementError {
msg: format!("Register name {}_flipped not fount", register_name),
})?
.view())
/ 2.0;
// reinserting in dict of pauli products
pauli_products += &tmp_pauli_products.view();
} else {
pauli_products += &pauli_product_dict
.get(register_name.as_str())
.ok_or(RoqoqoError::BasisRotationMeasurementError {
msg: format!("Register name {} not fount", register_name),
})?
.view()
}
}
}
// Evaluating expectation values
let mut results: HashMap<String, f64> = HashMap::new();
for (name, evaluation) in self.input.measured_exp_vals.iter() {
results.insert(
name.clone(),
match evaluation {
PauliProductsToExpVal::Linear(hm) => {
let mut value: f64 = 0.0;
for (index, coefficient) in hm {
value += pauli_products[*index] * coefficient;
}
value
}
PauliProductsToExpVal::Symbolic(x) => {
let mut calculator = qoqo_calculator::Calculator::new();
for (ind, p) in pauli_products.iter().enumerate() {
calculator.set_variable(format!("pauli_product_{}", ind).as_str(), *p);
}
calculator.parse_get(x.clone())?
}
},
);
}
Ok(Some(results))
}
}