use numr::{dtype::DType, runtime::Runtime};
use super::{
GateTrajectoryAssignment, GateTrajectoryEvaluation, GateTrajectoryEvent,
GateTrajectoryInterval, GateTrajectorySupport,
};
use super::{binding::GateTrajectoryData, evaluation::GateTrajectoryEvaluationData};
use crate::structures::tensor::TensorStructureInput;
use crate::structures::{
StructureBatch, StructureElement, StructureError, StructureHeads, StructureInputErrorKind,
StructureOperation, StructureProposal,
};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct GateTrajectoryBatch<R: Runtime<DType = DType>> {
pub(crate) inner: Arc<GateTrajectoryData<R>>,
}
impl<R: Runtime<DType = DType>> StructureBatch<R> {
pub fn with_gate_trajectories(
&self,
assignments: Vec<GateTrajectoryAssignment>,
) -> Result<GateTrajectoryBatch<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>,
{
Ok(GateTrajectoryBatch {
inner: Arc::new(GateTrajectoryData::new(
Arc::clone(&self.context),
assignments,
)?),
})
}
pub fn with_gate_trajectory_subset(
&self,
assignments: Vec<GateTrajectoryAssignment>,
) -> Result<GateTrajectoryBatch<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>,
{
Ok(GateTrajectoryBatch {
inner: Arc::new(GateTrajectoryData::subset(
Arc::clone(&self.context),
assignments,
)?),
})
}
}
impl<R: Runtime<DType = DType>> GateTrajectoryBatch<R> {
pub fn elements(&self) -> &[StructureElement] {
&self.inner.context.elements
}
pub fn assignments(&self) -> &[GateTrajectoryAssignment] {
&self.inner.assignments
}
pub fn support(&self) -> GateTrajectorySupport {
self.inner.support
}
pub fn evaluate(&self, time_s: f64) -> Result<GateTrajectoryEvaluation<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::IndexingOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::ShapeOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>
+ solvr::interpolate::LinearInterp1dDiagnostics<R>,
{
super::events::check_query(self.inner.support, time_s)?;
let (opening, absolute_error, input) =
crate::structures::numerics::gate::trajectory::evaluate(
&self.inner.context,
&self.inner.tensors,
&self.inner.assignments,
&self.inner.slots,
time_s,
)?;
Ok(GateTrajectoryEvaluation {
inner: Arc::new(GateTrajectoryEvaluationData {
binding: Arc::clone(&self.inner),
time_s,
opening,
absolute_error,
metadata: input.metadata,
}),
})
}
pub fn propose_at(
&self,
time_s: f64,
heads: &[StructureHeads],
other_operations: &[StructureOperation],
) -> Result<StructureProposal<R>, StructureError>
where
R::Client: numr::ops::CumulativeOps<R>
+ numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::CorrectlyRoundedOps<R>
+ numr::ops::IndexingOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::ShapeOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>
+ solvr::interpolate::Interp1dAlgorithms<R>
+ solvr::interpolate::LinearInterp1dDiagnostics<R>,
{
let evaluation = self.evaluate(time_s)?;
let input = TensorStructureInput::with_trajectory(
&self.inner.context,
heads,
&evaluation.inner,
other_operations,
)?;
let result = crate::structures::numerics::proposal(&self.inner.context, &input)?;
Ok(StructureProposal::from_trajectory(
Arc::clone(&self.inner.context),
heads.to_vec(),
evaluation.inner,
other_operations.to_vec(),
result,
))
}
pub fn next_event_after(
&self,
time_s: f64,
) -> Result<Option<GateTrajectoryEvent>, StructureError> {
super::events::check_query(self.inner.support, time_s)?;
let index = self
.inner
.events
.partition_point(|event| event.time_s <= time_s);
Ok(self.inner.events.get(index).cloned())
}
pub fn check_interval(
&self,
start_s: f64,
end_s: f64,
) -> Result<GateTrajectoryInterval, StructureError> {
let duration = end_s - start_s;
if !start_s.is_finite() || !end_s.is_finite() || !duration.is_finite() || duration <= 0.0 {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"trajectory interval=[{start_s},{end_s}],duration={duration}: supply finite positive original duration"
),
));
}
super::events::check_query(self.inner.support, start_s)?;
super::events::check_query(self.inner.support, end_s)?;
let first = self
.inner
.events
.partition_point(|event| event.time_s <= start_s);
let end = self
.inner
.events
.partition_point(|event| event.time_s < end_s);
let end_event = self
.inner
.events
.get(end)
.filter(|event| event.time_s == end_s)
.cloned();
Ok(GateTrajectoryInterval {
start_s,
end_s,
interior_events: self.inner.events[first..end].to_vec(),
end_event,
})
}
}