Skip to content

Commit

Permalink
Fix rounding error in RampWaveform (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri authored Oct 9, 2024
1 parent c133817 commit cdedaeb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pulser-core/pulser/math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ def diff(a: AbstractArrayLike) -> AbstractArray:
return AbstractArray(np.diff(a.as_array()))


def clip(a: AbstractArrayLike, a_min: float, a_max: float) -> AbstractArray:
a = AbstractArray(a)
if a.is_tensor:
return AbstractArray(torch.clamp(a.as_tensor(), a_min, a_max))
return AbstractArray(np.clip(a.as_array(), a_min, a_max))


def dot(a: AbstractArrayLike, b: AbstractArrayLike) -> AbstractArray:
a, b = map(AbstractArray, (a, b))
if a.is_tensor or b.is_tensor:
Expand Down
5 changes: 3 additions & 2 deletions pulser-core/pulser/waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ def _samples(self) -> pm.AbstractArray:
Returns:
A numpy array with a value for each time step.
"""
return (
self._slope * np.arange(self._duration, dtype=float) + self._start
return pm.clip(
self._slope * np.arange(self._duration, dtype=float) + self._start,
*sorted(map(float, [self._start, self._stop])),
)

@property
Expand Down
5 changes: 5 additions & 0 deletions tests/test_waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ def test_custom():
def test_ramp():
assert np.isclose(ramp.slope, 7e-3, atol=1e-5)

ramp_samples = RampWaveform(
3000, top := 25.757450291031688, 0
).samples.as_array()
assert np.all(np.logical_and(ramp_samples <= top, ramp_samples >= 0))


def test_blackman():
with pytest.raises(TypeError):
Expand Down

0 comments on commit cdedaeb

Please sign in to comment.