From 4f18bc1c9162002b4ebde24501313762621b691c Mon Sep 17 00:00:00 2001 From: Tobias Hangleiter Date: Wed, 23 Oct 2024 19:24:40 +0200 Subject: [PATCH] Add caching in getitem for an edge case --- filter_functions/pulse_sequence.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/filter_functions/pulse_sequence.py b/filter_functions/pulse_sequence.py index db1a447..266ccd3 100644 --- a/filter_functions/pulse_sequence.py +++ b/filter_functions/pulse_sequence.py @@ -407,6 +407,21 @@ def __getitem__(self, key) -> 'PulseSequence': d=self.d, basis=self.basis ) + + # An edge use case: key is a slice of the form slice(n), e.g., + # pulse[:n], in which case the control matrix might already have + # been cached in the form of an intermediate + is_valid_slice = ( + isinstance(key, slice) + and key.start in (None, 0) + and key.step in (None, 1) + ) + if is_valid_slice and 'control_matrix_step_cumulative' in self._intermediates: + new.cache_control_matrix( + self.omega, + self._intermediates['control_matrix_step_cumulative'][key.stop] + ) + return new def __copy__(self) -> 'PulseSequence':