#[cfg(feature = "unstable_chain_with_environment")]
use std::collections::HashMap;
use pyo3::prelude::*;
mod square_lattice;
#[cfg(feature = "unstable_chain_with_environment")]
use roqoqo::{devices::ChainWithEnvironmentDevice, RoqoqoError};
pub use square_lattice::SquareLatticeDeviceWrapper;
mod generic_device;
pub use generic_device::GenericDeviceWrapper;
mod all_to_all;
pub use all_to_all::AllToAllDeviceWrapper;
#[cfg(feature = "unstable_chain_with_environment")]
#[derive(Debug)]
pub struct ChainWithEnvironmentCapsule<'py> {
internal: &'py Bound<'py, PyAny>,
}
#[cfg(feature = "unstable_chain_with_environment")]
impl<'py> ChainWithEnvironmentCapsule<'py> {
pub fn new(python_device: &'py Bound<PyAny>) -> Result<Self, RoqoqoError> {
let __implements_environment_with_chains =
python_device.call_method0("__implements_environment_chains");
let implements_protocol =
__implements_environment_with_chains.map(|implement| implement.extract::<bool>());
match implements_protocol {
Ok(Ok(true)) => Ok(Self {
internal: python_device,
}),
_ => Err(RoqoqoError::GenericError {
msg: "Python device does not implement `environment_chains` method.".to_string(),
}),
}
}
}
#[cfg(feature = "unstable_chain_with_environment")]
impl ChainWithEnvironmentDevice for ChainWithEnvironmentCapsule<'_> {
fn environment_chains(&self) -> Vec<roqoqo::devices::ChainAndEnvironment> {
let chains_with_environment = self
.internal
.call_method0("__environment_chains")
.expect("Internal error `environment_chains` on python device failed.");
chains_with_environment
.extract::<Vec<(Vec<usize>, HashMap<usize, Vec<usize>>)>>()
.expect("Internal error `environment_chains` on python device does not return valid description.")
}
}
#[pymodule]
pub fn devices(_py: Python, module: &Bound<PyModule>) -> PyResult<()> {
module.add_class::<AllToAllDeviceWrapper>()?;
module.add_class::<GenericDeviceWrapper>()?;
module.add_class::<SquareLatticeDeviceWrapper>()?;
Ok(())
}