pub trait MeasureExpectationValues: PartialEq + Clone + Measure {
    // Required method
    fn evaluate(
        &self,
        bit_registers: HashMap<String, BitOutputRegister>,
        float_registers: HashMap<String, FloatOutputRegister>,
        complex_registers: HashMap<String, ComplexOutputRegister>
    ) -> Result<Option<HashMap<String, f64>>, RoqoqoError>;
}
Expand description

Allows generic interfacing with roqoqo measurements that evaluate expectation values.

Example

// We want to run a measurement for the following expectation value: 3 + 4.0 * < Z0 >.
use roqoqo::{measurements::{PauliZProduct, PauliZProductInput, MeasureExpectationValues}, registers::BitOutputRegister, Circuit};
use std::collections::HashMap;

// 1) Create and fill PauliZProductInput for the PauliZProduct measurement
let mut bri = PauliZProductInput::new(3, false);

let _a = bri.add_pauliz_product("ro".to_string(), vec![]);
let _b = bri.add_pauliz_product("ro".to_string(), vec![0]);

let mut linear_map_0: HashMap<usize, f64> = HashMap::new();
linear_map_0.insert(0, 3.0);
bri.add_linear_exp_val("constant".to_string(), linear_map_0).unwrap();
let mut linear_map_1: HashMap<usize, f64> = HashMap::new();
linear_map_1.insert(1, 4.0);
bri.add_linear_exp_val("single_qubit_exp_val".to_string(), linear_map_1).unwrap();

// 2) Create and fill PauliZProduct measurement
let mut circs: Vec<Circuit> = Vec::new();
circs.push(Circuit::new());

let br = PauliZProduct {
    constant_circuit: None,
    circuits: circs.clone(),
    input: bri,
};

// 3) Construct measured registers
let register = vec![
   vec![true, true, false],
   vec![true, true, false],
   vec![false, false, true],
   vec![false, false, true],
];
let mut measured_registers: HashMap<String, BitOutputRegister> = HashMap::new();
let new_output_register: BitOutputRegister = register;
let _ = measured_registers.insert("ro".to_string(), new_output_register);

// 4) Evaluate PauliZProduct measurement
let result = br
    .evaluate(measured_registers, HashMap::new(), HashMap::new())
    .unwrap()
    .unwrap();
    assert_eq!(result.get("constant").unwrap(), &3.0);
    assert_eq!(
    result.get("single_qubit_exp_val").unwrap(),
    &0.0
);

// 5) Check that all values are correct
assert_eq!(result.get("constant").unwrap(), &3.0);
assert_eq!(result.get("single_qubit_exp_val").unwrap(), &0.0);

Required Methods§

source

fn evaluate( &self, bit_registers: HashMap<String, BitOutputRegister>, float_registers: HashMap<String, FloatOutputRegister>, complex_registers: HashMap<String, ComplexOutputRegister> ) -> Result<Option<HashMap<String, f64>>, RoqoqoError>

Evaluates measurement results based on classical registers.

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) - The measurement evaluation failed.

Object Safety§

This trait is not object safe.

Implementors§