use pyo3::prelude::*;
use qoqo_macros::noise_model_wrapper;
use roqoqo::noise_models::{ErrorOnGateModel, NoiseModel};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};
use struqture_py;
#[pyclass(frozen, name = "ErrorOnGateModel")]
#[derive(Debug, Clone, PartialEq)]
pub struct ErrorOnGateModelWrapper {
internal: ErrorOnGateModel,
}
#[noise_model_wrapper]
impl ErrorOnGateModelWrapper {
#[new]
pub fn new() -> ErrorOnGateModelWrapper {
ErrorOnGateModelWrapper {
internal: ErrorOnGateModel::new(),
}
}
pub fn set_single_qubit_gate_error(
&self,
gate: &str,
qubit: usize,
noise_operator: Py<PyAny>,
) -> PyResult<Self> {
let noise_operator =
struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany(noise_operator)?;
Ok(Self {
internal: self.internal.clone().set_single_qubit_gate_error(
gate,
qubit,
noise_operator,
),
})
}
pub fn get_single_qubit_gate_error(
&self,
gate: &str,
qubit: usize,
) -> Option<struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper> {
if let Some(noise_operator) = self.internal.get_single_qubit_gate_error(gate, qubit) {
Some(struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper {
internal: noise_operator.clone(),
})
} else {
None
}
}
pub fn set_two_qubit_gate_error(
&self,
gate: &str,
control: usize,
target: usize,
noise_operator: Py<PyAny>,
) -> PyResult<Self> {
let noise_operator =
struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany(noise_operator)?;
Ok(Self {
internal: self.internal.clone().set_two_qubit_gate_error(
gate,
control,
target,
noise_operator,
),
})
}
pub fn get_two_qubit_gate_error(
&self,
gate: &str,
control: usize,
target: usize,
) -> Option<struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper> {
if let Some(noise_operator) = self
.internal
.get_two_qubit_gate_error(gate, control, target)
{
Some(struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper {
internal: noise_operator.clone(),
})
} else {
None
}
}
pub fn set_three_qubit_gate_error(
&self,
gate: &str,
control0: usize,
control1: usize,
target: usize,
noise_operator: Py<PyAny>,
) -> PyResult<Self> {
let noise_operator =
struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany(noise_operator)?;
Ok(Self {
internal: self.internal.clone().set_three_qubit_gate_error(
gate,
control0,
control1,
target,
noise_operator,
),
})
}
pub fn get_three_qubit_gate_error(
&self,
gate: &str,
control0: usize,
control1: usize,
target: usize,
) -> Option<struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper> {
if let Some(noise_operator) = self
.internal
.get_three_qubit_gate_error(gate, control0, control1, target)
{
Some(struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper {
internal: noise_operator.clone(),
})
} else {
None
}
}
pub fn set_multi_qubit_gate_error(
&self,
gate: &str,
qubits: Vec<usize>,
noise_operator: Py<PyAny>,
) -> PyResult<Self> {
let noise_operator =
struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper::from_pyany(noise_operator)?;
Ok(Self {
internal: self.internal.clone().set_multi_qubit_gate_error(
gate,
qubits,
noise_operator,
),
})
}
pub fn get_multi_qubit_gate_error(
&self,
gate: &str,
qubits: Vec<usize>,
) -> Option<struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper> {
if let Some(noise_operator) = self.internal.get_multi_qubit_gate_error(gate, qubits) {
Some(struqture_py::spins::PlusMinusLindbladNoiseOperatorWrapper {
internal: noise_operator.clone(),
})
} else {
None
}
}
#[staticmethod]
#[pyo3(text_signature = "(input)")]
pub fn from_bincode(input: &PyAny) -> PyResult<ErrorOnGateModelWrapper> {
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::deserialize(&bytes[..]).map_err(|_| {
pyo3::exceptions::PyValueError::new_err("Input cannot be deserialized to Noise-Model.")
})?;
match noise_model {
NoiseModel::ErrorOnGateModel(internal) => Ok(ErrorOnGateModelWrapper { 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<ErrorOnGateModelWrapper> {
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::ErrorOnGateModel(internal) => Ok(ErrorOnGateModelWrapper { 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!(ErrorOnGateModel);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}
}