use pyo3::{exceptions::PyValueError, prelude::*};
use qoqo_macros::noise_model_wrapper;
use roqoqo::noise_models::{ContinuousDecoherenceModel, NoiseModel};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};
use struqture;
#[pyclass(frozen, name = "ContinuousDecoherenceModel")]
#[derive(Clone, Debug, PartialEq)]
pub struct ContinuousDecoherenceModelWrapper {
internal: ContinuousDecoherenceModel,
}
#[noise_model_wrapper]
impl ContinuousDecoherenceModelWrapper {
#[new]
#[pyo3(signature = (noise_operator=None))]
pub fn new(noise_operator: Option<&Bound<PyAny>>) -> PyResult<Self> {
if let Some(lindblad_operator) = noise_operator {
let noise_operator: struqture::spins::PlusMinusLindbladNoiseOperator =
match struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany(
lindblad_operator,
) {
Ok(x) => x,
Err(_) => match struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany_struqture_1(lindblad_operator) {
Ok(x) => x,
Err(err) => return Err(PyValueError::new_err(format!("Could not convert input noise_operator from either struqture 1.x or struqture 2.x: {err:?}"))),
}
};
Ok(Self {
internal: ContinuousDecoherenceModel::from(noise_operator),
})
} else {
Ok(ContinuousDecoherenceModelWrapper {
internal: ContinuousDecoherenceModel::new(),
})
}
}
pub fn get_noise_operator<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
crate::get_operator(py, &self.internal.lindblad_noise)
}
#[staticmethod]
#[pyo3(text_signature = "(input)")]
pub fn from_bincode(input: &Bound<PyAny>) -> PyResult<ContinuousDecoherenceModelWrapper> {
let bytes = input.extract::<Vec<u8>>().map_err(|_| {
pyo3::exceptions::PyTypeError::new_err("Input cannot be converted to byte array")
})?;
let noise_model: NoiseModel =
bincode::serde::decode_from_slice(&bytes[..], bincode::config::legacy())
.map_err(|_| {
pyo3::exceptions::PyValueError::new_err(
"Input cannot be deserialized to Noise-Model.",
)
})?
.0;
match noise_model {
NoiseModel::ContinuousDecoherenceModel(internal) => {
Ok(ContinuousDecoherenceModelWrapper { internal })
}
_ => Err(pyo3::exceptions::PyValueError::new_err(
"Input cannot be deserialized to selected Noise-Model.",
)),
}
}
#[staticmethod]
#[pyo3(text_signature = "(input)")]
pub fn from_json(input: &str) -> PyResult<ContinuousDecoherenceModelWrapper> {
let noise_model: NoiseModel = serde_json::from_str(input).map_err(|_| {
pyo3::exceptions::PyValueError::new_err("Input cannot be deserialized to Noise-Model.")
})?;
match noise_model {
NoiseModel::ContinuousDecoherenceModel(internal) => {
Ok(ContinuousDecoherenceModelWrapper { internal })
}
_ => Err(pyo3::exceptions::PyValueError::new_err(
"Input cannot be deserialized to selected Noise-Model.",
)),
}
}
#[cfg(feature = "json_schema")]
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(ContinuousDecoherenceModel);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}
pub fn add_damping_rate(&self, qubits: Vec<usize>, rate: f64) -> Self {
Self {
internal: self.internal.clone().add_damping_rate(&qubits, rate),
}
}
pub fn add_dephasing_rate(&self, qubits: Vec<usize>, rate: f64) -> Self {
Self {
internal: self.internal.clone().add_dephasing_rate(&qubits, rate),
}
}
pub fn add_depolarising_rate(&self, qubits: Vec<usize>, rate: f64) -> Self {
Self {
internal: self.internal.clone().add_depolarising_rate(&qubits, rate),
}
}
pub fn add_excitation_rate(&self, qubits: Vec<usize>, rate: f64) -> Self {
Self {
internal: self.internal.clone().add_excitation_rate(&qubits, rate),
}
}
}