use num_complex::Complex64;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use roqoqo::measurements::{
CheatedInput, CheatedPauliZProductInput, PauliProductMask, PauliZProductInput,
};
use std::collections::HashMap;
#[pyclass(name = "PauliZProductInput", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct PauliZProductInputWrapper {
pub internal: PauliZProductInput,
}
#[pymethods]
impl PauliZProductInputWrapper {
#[new]
pub fn new(number_qubits: usize, use_flipped_measurement: bool) -> Self {
Self {
internal: PauliZProductInput::new(number_qubits, use_flipped_measurement),
}
}
pub fn add_pauliz_product(
&mut self,
readout: String,
pauli_product_mask: PauliProductMask,
) -> PyResult<usize> {
self.internal
.add_pauliz_product(readout, pauli_product_mask)
.map_err(|_| PyRuntimeError::new_err("Failed to add pauli product"))
}
pub fn add_linear_exp_val(
&mut self,
name: String,
linear: HashMap<usize, f64>,
) -> PyResult<()> {
self.internal.add_linear_exp_val(name, linear).map_err(|x| {
PyRuntimeError::new_err(format!("Failed to add linear expectation value {:?}", x))
})
}
pub fn add_symbolic_exp_val(&mut self, name: String, symbolic: String) -> PyResult<()> {
self.internal
.add_symbolic_exp_val(name, symbolic.into())
.map_err(|x| {
PyRuntimeError::new_err(format!("Failed to add symbolic expectation value {:?}", x))
})
}
}
#[pyclass(name = "CheatedPauliZProductInput", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct CheatedPauliZProductInputWrapper {
pub internal: CheatedPauliZProductInput,
}
impl Default for CheatedPauliZProductInputWrapper {
fn default() -> Self {
Self::new()
}
}
#[pymethods]
impl CheatedPauliZProductInputWrapper {
#[new]
pub fn new() -> Self {
Self {
internal: CheatedPauliZProductInput::new(),
}
}
pub fn add_pauliz_product(&mut self, readout: String) -> usize {
self.internal.add_pauliz_product(readout)
}
pub fn add_linear_exp_val(
&mut self,
name: String,
linear: HashMap<usize, f64>,
) -> PyResult<()> {
self.internal.add_linear_exp_val(name, linear).map_err(|x| {
PyRuntimeError::new_err(format!("Failed to add linear expectation value {:?}", x))
})
}
pub fn add_symbolic_exp_val(&mut self, name: String, symbolic: String) -> PyResult<()> {
self.internal
.add_symbolic_exp_val(name, symbolic.into())
.map_err(|x| {
PyRuntimeError::new_err(format!("Failed to add symbolic expectation value {:?}", x))
})
}
}
#[pyclass(name = "CheatedInput", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct CheatedInputWrapper {
pub internal: CheatedInput,
}
#[pymethods]
impl CheatedInputWrapper {
#[new]
pub fn new(number_qubits: usize) -> Self {
Self {
internal: CheatedInput::new(number_qubits),
}
}
pub fn add_operator_exp_val(
&mut self,
name: String,
operator: Vec<(usize, usize, Complex64)>,
readout: String,
) -> PyResult<()> {
self.internal
.add_operator_exp_val(name, operator, readout)
.map_err(|x| {
PyRuntimeError::new_err(format!(
"Failed to add operator based expectation value {:?}",
x
))
})
}
}