hydr_structures/structures/gate/trajectory/
batch.rs1use numr::{dtype::DType, runtime::Runtime};
3
4use super::{
5 GateTrajectoryAssignment, GateTrajectoryEvaluation, GateTrajectoryEvent,
6 GateTrajectoryInterval, GateTrajectorySupport,
7};
8use super::{binding::GateTrajectoryData, evaluation::GateTrajectoryEvaluationData};
9use crate::structures::tensor::TensorStructureInput;
10use crate::structures::{
11 StructureBatch, StructureElement, StructureError, StructureHeads, StructureInputErrorKind,
12 StructureOperation, StructureProposal,
13};
14use std::sync::Arc;
15
16#[derive(Clone, Debug)]
18pub struct GateTrajectoryBatch<R: Runtime<DType = DType>> {
19 pub(crate) inner: Arc<GateTrajectoryData<R>>,
20}
21impl<R: Runtime<DType = DType>> StructureBatch<R> {
22 pub fn with_gate_trajectories(
24 &self,
25 assignments: Vec<GateTrajectoryAssignment>,
26 ) -> Result<GateTrajectoryBatch<R>, StructureError>
27 where
28 R::Client: numr::ops::BinaryOps<R>
29 + numr::ops::CompareOps<R>
30 + numr::ops::ConditionalOps<R>
31 + numr::ops::ReduceOps<R>
32 + numr::ops::ScalarOps<R>
33 + numr::ops::TwoSumOps<R>
34 + numr::ops::TypeConversionOps<R>
35 + numr::ops::UnaryOps<R>,
36 {
37 Ok(GateTrajectoryBatch {
38 inner: Arc::new(GateTrajectoryData::new(
39 Arc::clone(&self.context),
40 assignments,
41 )?),
42 })
43 }
44 pub fn with_gate_trajectory_subset(
49 &self,
50 assignments: Vec<GateTrajectoryAssignment>,
51 ) -> Result<GateTrajectoryBatch<R>, StructureError>
52 where
53 R::Client: numr::ops::BinaryOps<R>
54 + numr::ops::CompareOps<R>
55 + numr::ops::ConditionalOps<R>
56 + numr::ops::ReduceOps<R>
57 + numr::ops::ScalarOps<R>
58 + numr::ops::TwoSumOps<R>
59 + numr::ops::TypeConversionOps<R>
60 + numr::ops::UnaryOps<R>,
61 {
62 Ok(GateTrajectoryBatch {
63 inner: Arc::new(GateTrajectoryData::subset(
64 Arc::clone(&self.context),
65 assignments,
66 )?),
67 })
68 }
69}
70impl<R: Runtime<DType = DType>> GateTrajectoryBatch<R> {
71 pub fn elements(&self) -> &[StructureElement] {
73 &self.inner.context.elements
74 }
75 pub fn assignments(&self) -> &[GateTrajectoryAssignment] {
77 &self.inner.assignments
78 }
79 pub fn support(&self) -> GateTrajectorySupport {
81 self.inner.support
82 }
83 pub fn evaluate(&self, time_s: f64) -> Result<GateTrajectoryEvaluation<R>, StructureError>
85 where
86 R::Client: numr::ops::BinaryOps<R>
87 + numr::ops::CompareOps<R>
88 + numr::ops::ConditionalOps<R>
89 + numr::ops::IndexingOps<R>
90 + numr::ops::ReduceOps<R>
91 + numr::ops::ScalarOps<R>
92 + numr::ops::ShapeOps<R>
93 + numr::ops::TwoSumOps<R>
94 + numr::ops::TypeConversionOps<R>
95 + numr::ops::UnaryOps<R>
96 + solvr::interpolate::LinearInterp1dDiagnostics<R>,
97 {
98 super::events::check_query(self.inner.support, time_s)?;
99 let (opening, absolute_error, input) =
100 crate::structures::numerics::gate::trajectory::evaluate(
101 &self.inner.context,
102 &self.inner.tensors,
103 &self.inner.assignments,
104 &self.inner.slots,
105 time_s,
106 )?;
107 Ok(GateTrajectoryEvaluation {
108 inner: Arc::new(GateTrajectoryEvaluationData {
109 binding: Arc::clone(&self.inner),
110 time_s,
111 opening,
112 absolute_error,
113 metadata: input.metadata,
114 }),
115 })
116 }
117 pub fn propose_at(
122 &self,
123 time_s: f64,
124 heads: &[StructureHeads],
125 other_operations: &[StructureOperation],
126 ) -> Result<StructureProposal<R>, StructureError>
127 where
128 R::Client: numr::ops::CumulativeOps<R>
129 + numr::ops::BinaryOps<R>
130 + numr::ops::CompareOps<R>
131 + numr::ops::ConditionalOps<R>
132 + numr::ops::CorrectlyRoundedOps<R>
133 + numr::ops::IndexingOps<R>
134 + numr::ops::ReduceOps<R>
135 + numr::ops::ScalarOps<R>
136 + numr::ops::ShapeOps<R>
137 + numr::ops::TwoSumOps<R>
138 + numr::ops::TypeConversionOps<R>
139 + numr::ops::UnaryOps<R>
140 + solvr::interpolate::Interp1dAlgorithms<R>
141 + solvr::interpolate::LinearInterp1dDiagnostics<R>,
142 {
143 let evaluation = self.evaluate(time_s)?;
144 let input = TensorStructureInput::with_trajectory(
145 &self.inner.context,
146 heads,
147 &evaluation.inner,
148 other_operations,
149 )?;
150 let result = crate::structures::numerics::proposal(&self.inner.context, &input)?;
151 Ok(StructureProposal::from_trajectory(
152 Arc::clone(&self.inner.context),
153 heads.to_vec(),
154 evaluation.inner,
155 other_operations.to_vec(),
156 result,
157 ))
158 }
159 pub fn next_event_after(
161 &self,
162 time_s: f64,
163 ) -> Result<Option<GateTrajectoryEvent>, StructureError> {
164 super::events::check_query(self.inner.support, time_s)?;
165 let index = self
166 .inner
167 .events
168 .partition_point(|event| event.time_s <= time_s);
169 Ok(self.inner.events.get(index).cloned())
170 }
171 pub fn check_interval(
173 &self,
174 start_s: f64,
175 end_s: f64,
176 ) -> Result<GateTrajectoryInterval, StructureError> {
177 let duration = end_s - start_s;
178 if !start_s.is_finite() || !end_s.is_finite() || !duration.is_finite() || duration <= 0.0 {
179 return Err(StructureError::input(
180 StructureInputErrorKind::Definition,
181 format!(
182 "trajectory interval=[{start_s},{end_s}],duration={duration}: supply finite positive original duration"
183 ),
184 ));
185 }
186 super::events::check_query(self.inner.support, start_s)?;
187 super::events::check_query(self.inner.support, end_s)?;
188 let first = self
189 .inner
190 .events
191 .partition_point(|event| event.time_s <= start_s);
192 let end = self
193 .inner
194 .events
195 .partition_point(|event| event.time_s < end_s);
196 let end_event = self
197 .inner
198 .events
199 .get(end)
200 .filter(|event| event.time_s == end_s)
201 .cloned();
202 Ok(GateTrajectoryInterval {
203 start_s,
204 end_s,
205 interior_events: self.inner.events[first..end].to_vec(),
206 end_event,
207 })
208 }
209}