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::{Device, GenericDevice};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};
#[pyclass(from_py_object, name = "GenericDevice", module = "devices")]
#[derive(Clone, Debug, PartialEq)]
pub struct GenericDeviceWrapper {
pub internal: GenericDevice,
}
#[devicewrapper]
impl GenericDeviceWrapper {
#[new]
#[pyo3(text_signature = "(number_qubits)")]
pub fn new(number_qubits: usize) -> PyResult<Self> {
Ok(Self {
internal: GenericDevice::new(number_qubits),
})
}
#[cfg(feature = "json_schema")]
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(GenericDevice);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}
#[cfg(feature = "json_schema")]
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}
#[cfg(feature = "json_schema")]
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
GenericDevice::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}
impl GenericDeviceWrapper {
pub fn from_pyany(input: &Bound<PyAny>) -> PyResult<GenericDevice> {
if let Ok(try_downcast) = input.extract::<GenericDeviceWrapper>() {
Ok(try_downcast.internal)
} else {
let generic_device_candidate = input.call_method0("generic_device")?;
let get_bytes = generic_device_candidate.call_method0("to_bincode")?;
let bytes = get_bytes.extract::<Vec<u8>>()?;
bincode::serde::decode_from_slice(&bytes[..], bincode::config::legacy())
.map_err(|err| {
PyValueError::new_err(format!("Cannot treat input as GenericDevice: {err}"))
})
.map(|(deserialized, _)| deserialized)
}
}
}