use super::GenericDeviceWrapper;
use bincode::{deserialize, serialize};
use ndarray::Array2;
use numpy::{PyArray2, PyReadonlyArray2, ToPyArray};
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyByteArray;
use qoqo_macros::devicewrapper;
use roqoqo::devices::{AllToAllDevice, Device};
#[pyclass(name = "AllToAllDevice", module = "devices")]
#[derive(Clone, Debug, PartialEq)]
#[pyo3(text_signature = "(number_qubits, single_qubit_gates, two_qubit_gates, default_gate_time)")]
pub struct AllToAllDeviceWrapper {
pub internal: AllToAllDevice,
}
#[devicewrapper]
impl AllToAllDeviceWrapper {
#[new]
pub fn new(
number_qubits: usize,
single_qubit_gates: Vec<String>,
two_qubit_gates: Vec<String>,
default_gate_time: f64,
) -> PyResult<Self> {
Ok(Self {
internal: AllToAllDevice::new(
number_qubits,
&single_qubit_gates,
&two_qubit_gates,
default_gate_time,
),
})
}
#[pyo3(text_signature = "(gate, gate_time)")]
pub fn set_all_two_qubit_gate_times(&mut self, gate: &str, gate_time: f64) -> Self {
Self {
internal: self
.internal
.clone()
.set_all_two_qubit_gate_times(gate, gate_time),
}
}
#[pyo3(text_signature = "(gate, gate_time)")]
pub fn set_all_single_qubit_gate_times(&self, gate: &str, gate_time: f64) -> Self {
Self {
internal: self
.internal
.clone()
.set_all_single_qubit_gate_times(gate, gate_time),
}
}
#[pyo3(text_signature = "(rates)")]
pub fn set_all_qubit_decoherence_rates(&self, rates: PyReadonlyArray2<f64>) -> PyResult<Self> {
let rates_matrix = rates.as_array().to_owned();
Ok(Self {
internal: self
.internal
.clone()
.set_all_qubit_decoherence_rates(rates_matrix)
.map_err(|_| {
PyValueError::new_err("The input parameter `rates` needs to be a (3x3)-matrix.")
})?,
})
}
#[pyo3(text_signature = "(damping)")]
pub fn add_damping_all(&mut self, damping: f64) -> Self {
Self {
internal: self.internal.clone().add_damping_all(damping),
}
}
#[pyo3(text_signature = "(dephasing)")]
pub fn add_dephasing_all(&mut self, dephasing: f64) -> Self {
Self {
internal: self.internal.clone().add_dephasing_all(dephasing),
}
}
#[pyo3(text_signature = "(depolarising)")]
pub fn add_depolarising_all(&mut self, depolarising: f64) -> Self {
Self {
internal: self.internal.clone().add_depolarising_all(depolarising),
}
}
}
impl AllToAllDeviceWrapper {
pub fn from_pyany(input: Py<PyAny>) -> PyResult<AllToAllDevice> {
Python::with_gil(|py| -> PyResult<AllToAllDevice> {
let input = input.as_ref(py);
if let Ok(try_downcast) = input.extract::<AllToAllDeviceWrapper>() {
Ok(try_downcast.internal)
} else {
let get_bytes = input.call_method0("to_bincode")?;
let bytes = get_bytes.extract::<Vec<u8>>()?;
deserialize(&bytes[..]).map_err(|err| {
PyValueError::new_err(format!("Cannot treat input as AllToAllDevice: {}", err))
})
}
})
}
}