Skip to content

Commit 896b1ae

Browse files
committed
Adjust type hints to pad_to requirements and do piecewise evaluation based on the quanta
1 parent 0849652 commit 896b1ae

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

qupulse/utils/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def forced_hash(obj) -> int:
131131

132132

133133
def to_next_multiple(sample_rate: ExpressionLike, quantum: int,
134-
min_quanta: Optional[int] = None) -> Callable[[ExpressionLike],ExpressionScalar]:
134+
min_quanta: Optional[int] = None) -> Callable[[ExpressionScalar], ExpressionLike]:
135135
"""Construct a helper function to expand a duration to one corresponding to
136136
valid sample multiples according to the arguments given.
137137
Useful e.g. for PulseTemplate.pad_to's 'to_new_duration'-argument.
@@ -141,12 +141,13 @@ def to_next_multiple(sample_rate: ExpressionLike, quantum: int,
141141
quantum: number of samples to whose next integer multiple the duration shall be rounded up to.
142142
min_quanta: number of multiples of quantum not to fall short of.
143143
Returns:
144-
A function that takes a duration (ExpressionLike) as input, and returns
144+
A function that takes a duration as input, and returns
145145
a duration rounded up to the next valid samples count in given sample rate.
146146
The function returns 0 if duration==0, <0 is not checked if min_quanta is None.
147147
148148
"""
149149
sample_rate = ExpressionScalar(sample_rate)
150+
150151
#is it more efficient to omit the Max call if not necessary?
151152
if min_quanta is None:
152153
#double negative for ceil division.
@@ -157,14 +158,16 @@ def to_next_multiple(sample_rate: ExpressionLike, quantum: int,
157158
duration_per_quantum = sp.Integer(quantum) / sample_rate
158159
minimal_duration = duration_per_quantum * min_quanta
159160

160-
def build_next_multiple(duration: ExpressionLike) -> ExpressionScalar:
161+
def build_next_multiple(duration: ExpressionScalar) -> ExpressionLike:
161162
duration = sp.sympify(duration)
162-
rounded_up_duration = sp.ceiling(duration / duration_per_quantum) * duration_per_quantum
163+
n_quanta = sp.ceiling(duration / duration_per_quantum)
164+
rounded_up_duration = n_quanta * duration_per_quantum
163165

164166
next_multiple_sp = sp.Piecewise(
165-
(0, sp.Le(duration, 0)),
166-
(minimal_duration, sp.Le(duration, minimal_duration)),
167-
(rounded_up_duration, True)
167+
(0, sp.Le(n_quanta, 0)),
168+
(minimal_duration, sp.Le(n_quanta, min_quanta)),
169+
(rounded_up_duration, True),
170+
evaluate=False,
168171
)
169172
return ExpressionScalar(next_multiple_sp)
170173

0 commit comments

Comments
 (0)