Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adaptative rounding in InterpolatedWaveform #592

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: np.abs(samples).max() seems to be more concise and faster...
image
image
image

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