Skip to content

Commit

Permalink
Add adaptative rounding in InterpolatedWaveform (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri authored Oct 5, 2023
1 parent a5f98c0 commit fcd488a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pulser-core/pulser/waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,12 @@ def duration(self) -> int:
@cached_property
def _samples(self) -> np.ndarray:
"""The value at each time step that describes the waveform."""
return cast(
np.ndarray,
np.round(
self._interp_func(np.arange(self._duration)), decimals=9
), # Rounds to the order of Hz
)
samples = self._interp_func(np.arange(self._duration))
value_range = np.max(np.abs(samples))
decimals = int(
min(np.finfo(samples.dtype).precision - np.log10(value_range), 9)
) # Reduces decimal values below 9 for large ranges
return cast(np.ndarray, np.round(samples, decimals=decimals))

@property
def interp_function(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ def test_interpolated():
interp_wf2.samples, np.linspace(0, 1, num=dt) ** 2, atol=1e-3
)

# Test rounding when range of values is large
wf = InterpolatedWaveform(
1000, times=[0.0, 0.5, 1.0], values=[0, 2.6e7, 0]
)
assert np.all(wf.samples >= 0)


def test_kaiser():
duration: int = 40
Expand Down

0 comments on commit fcd488a

Please sign in to comment.