use super::{ControlledGateEvent, ControlledGateTrial};
use crate::structures::tensor::controlled_gate::TensorControlledGateContext;
use crate::structures::{
ControlEvent, ControlSample, GateActuatorBatch, GateActuatorEvent, OperatingControlBatch,
StructureError,
};
use numr::dtype::DType;
use numr::runtime::Runtime;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct ControlledGateBatch<R: Runtime<DType = DType>> {
pub(super) context: Arc<TensorControlledGateContext<R>>,
pub(crate) controls: OperatingControlBatch<R>,
pub(crate) actuators: GateActuatorBatch<R>,
}
impl<R: Runtime<DType = DType>> ControlledGateBatch<R> {
pub fn new(
controls: OperatingControlBatch<R>,
actuators: GateActuatorBatch<R>,
) -> Result<Self, StructureError> {
let context = Arc::new(TensorControlledGateContext::new(&controls, &actuators)?);
Ok(Self {
context,
controls,
actuators,
})
}
pub fn controls(&self) -> &OperatingControlBatch<R> {
&self.controls
}
pub fn actuators(&self) -> &GateActuatorBatch<R> {
&self.actuators
}
pub fn time(&self) -> f64 {
self.controls.time()
}
pub fn accepted_steps(&self) -> usize {
self.controls.accepted_steps()
}
pub fn next_event(&self) -> Result<Option<ControlledGateEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
let control = self.controls.next_event();
let actuator = self.actuators.next_event()?;
Ok(Self::common(control, actuator))
}
#[doc(hidden)]
pub fn next_coordinated_event(&self) -> Result<Option<ControlledGateEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
let control = self.controls.next_event();
let actuator = self.actuators.next_coordinated_event()?;
Ok(Self::common(control, actuator))
}
pub(crate) fn event_at(&self, end_s: f64) -> Result<Option<ControlledGateEvent>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>,
{
let control = self
.controls
.next_event()
.filter(|event| event.time_s == end_s);
let actuator = self.actuators.event_at(end_s)?;
Ok(Self::common(control, actuator))
}
fn common(
control: Option<ControlEvent>,
actuator: Option<GateActuatorEvent>,
) -> Option<ControlledGateEvent> {
let time_s = match (control, actuator) {
(Some(control), Some(actuator)) => control.time_s.min(actuator.time_s),
(Some(control), None) => control.time_s,
(None, Some(actuator)) => actuator.time_s,
(None, None) => return None,
};
let control = control.filter(|event| event.time_s == time_s);
let actuator = actuator.filter(|event| event.time_s == time_s);
Some(ControlledGateEvent {
time_s,
decision_index: control.and_then(|event| event.decision_index),
arrival_lower_boundary: actuator.is_some_and(|event| event.arrival_lower_boundary),
arrival_upper_boundary: actuator.is_some_and(|event| event.arrival_upper_boundary),
terminal: control.is_some_and(|event| event.terminal)
|| actuator.is_some_and(|event| event.terminal),
})
}
pub fn trial_to(
&self,
end_s: f64,
decision_samples: &[ControlSample],
) -> Result<ControlledGateTrial<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>,
{
self.complete(self.prepare_to(end_s)?, decision_samples)
}
}