use num_complex::Complex64;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use roqoqo::measurements::{
BasisRotationInput, CheatedBasisRotationInput, CheatedInput, PauliProductMask,
};
use std::collections::HashMap;
#[pyclass(name = "BasisRotationInput", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct BasisRotationInputWrapper {
pub internal: BasisRotationInput,
}
#[pymethods]
impl BasisRotationInputWrapper {
#[new]
pub fn new(number_qubits: usize, use_flipped_measurement: bool) -> Self {
Self {
internal: BasisRotationInput::new(number_qubits, use_flipped_measurement),
}
}
pub fn add_pauli_product(
&mut self,
readout: String,
pauli_product_mask: PauliProductMask,
) -> PyResult<usize> {
self.internal
.add_pauli_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 = "CheatedBasisRotationInput", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct CheatedBasisRotationInputWrapper {
pub internal: CheatedBasisRotationInput,
}
impl Default for CheatedBasisRotationInputWrapper {
fn default() -> Self {
Self::new()
}
}
#[pymethods]
impl CheatedBasisRotationInputWrapper {
#[new]
pub fn new() -> Self {
Self {
internal: CheatedBasisRotationInput::new(),
}
}
pub fn add_pauli_product(&mut self, readout: String) -> usize {
self.internal.add_pauli_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
))
})
}
}