use crate::CircuitWrapper;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyType;
use roqoqo::measurements::ClassicalRegister;
use roqoqo::prelude::*;
use roqoqo::Circuit;
use std::collections::HashMap;
#[pyclass(name = "ClassicalRegister", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
pub struct ClassicalRegisterWrapper {
pub internal: ClassicalRegister,
}
#[pymethods]
impl ClassicalRegisterWrapper {
#[new]
pub fn new(constant_circuit: Option<CircuitWrapper>, circuits: Vec<CircuitWrapper>) -> Self {
let new_circuits: Vec<Circuit> = circuits.into_iter().map(|c| c.internal).collect();
let new_constant: Option<Circuit> = match constant_circuit {
None => None,
Some(c) => Some(c.internal),
};
Self {
internal: ClassicalRegister {
constant_circuit: new_constant,
circuits: new_circuits,
},
}
}
pub fn circuits(&self) -> Vec<CircuitWrapper> {
self.internal
.circuits()
.map(|c| CircuitWrapper {
internal: c.clone(),
})
.collect()
}
pub fn constant_circuit(&self) -> Option<CircuitWrapper> {
self.internal
.constant_circuit()
.clone()
.map(|c| CircuitWrapper { internal: c })
}
pub fn substitute_parameters(
&self,
substituted_parameters: HashMap<String, f64>,
) -> PyResult<Self> {
Ok(Self {
internal: self
.internal
.substitute_parameters(substituted_parameters)
.map_err(|x| {
PyRuntimeError::new_err(format!(
"Error substituting symbolic parameters {:?}",
x
))
})?,
})
}
pub fn to_json(&self) -> PyResult<String> {
serde_json::to_string(&self.internal)
.map_err(|_| PyRuntimeError::new_err("Unexpected error serializing ClassicalRegister"))
}
#[allow(unused_variables)]
#[classmethod]
pub fn from_json(cls: &PyType, json_string: &str) -> PyResult<Self> {
Ok(Self {
internal: serde_json::from_str(json_string).map_err(|_| {
PyValueError::new_err("Cannot deserialize string to ClassicalRegister")
})?,
})
}
}