From b15e4292298f68fef694a90df80f8c42dad46647 Mon Sep 17 00:00:00 2001 From: Matthew Coolbeth Date: Tue, 30 Jan 2024 17:01:03 -0500 Subject: [PATCH] fix: Add deprecated property to DefFrame for CHANNEL-DELAY attribute (#1729) * Add deprecated property to DefFrame for CHANNEL-DELAY attribute * Minor formatting tweaks * Remove call to magic 'locals' function from unit tests --------- Co-authored-by: Matthew Coolbeth Co-authored-by: Marquess Valdez --- pyquil/quilbase.py | 16 ++++++ test/unit/__snapshots__/test_quilbase.ambr | 2 + test/unit/test_quilbase.py | 57 ++++++++++------------ 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/pyquil/quilbase.py b/pyquil/quilbase.py index d375e94c5..7da50373d 100644 --- a/pyquil/quilbase.py +++ b/pyquil/quilbase.py @@ -2858,6 +2858,22 @@ def center_frequency(self) -> Frame: def center_frequency(self, center_frequency: float) -> None: self.set_attribute("CENTER-FREQUENCY", center_frequency) + @property + @deprecated( + version="4.0", + reason="Quil now supports generic key/value pairs in DEFFRAMEs. Use get_attribute('CHANNEL-DELAY') instead.", + ) + def channel_delay(self) -> Frame: + return self.get_attribute("CHANNEL-DELAY") # type: ignore + + @channel_delay.setter + @deprecated( + version="4.0", + reason="Quil now supports generic key/value pairs in DEFFRAMEs. Use set_attribute('CHANNEL-DELAY') instead.", + ) + def channel_delay(self, channel_delay: float) -> None: + self.set_attribute("CHANNEL-DELAY", channel_delay) + def __copy__(self) -> Self: return self diff --git a/test/unit/__snapshots__/test_quilbase.ambr b/test/unit/__snapshots__/test_quilbase.ambr index 1074e677a..716994a56 100644 --- a/test/unit/__snapshots__/test_quilbase.ambr +++ b/test/unit/__snapshots__/test_quilbase.ambr @@ -196,6 +196,7 @@ # name: TestDefFrame.test_out[With-Optionals].1 set({ '\tCENTER-FREQUENCY: 440', + '\tCHANNEL-DELAY: 0', '\tDIRECTION: "direction"', '\tHARDWARE-OBJECT: "hardware_object"', '\tINITIAL-FREQUENCY: 1.39', @@ -215,6 +216,7 @@ # name: TestDefFrame.test_str[With-Optionals].1 set({ '\tCENTER-FREQUENCY: 440', + '\tCHANNEL-DELAY: 0', '\tDIRECTION: "direction"', '\tHARDWARE-OBJECT: "hardware_object"', '\tINITIAL-FREQUENCY: 1.39', diff --git a/test/unit/test_quilbase.py b/test/unit/test_quilbase.py index 0ef4eeb7d..b6064c82d 100644 --- a/test/unit/test_quilbase.py +++ b/test/unit/test_quilbase.py @@ -474,10 +474,10 @@ def test_copy(self, measurement: Measurement): @pytest.mark.parametrize( - ("frame", "direction", "initial_frequency", "hardware_object", "sample_rate", "center_frequency"), + ("frame", "direction", "initial_frequency", "hardware_object", "sample_rate", "center_frequency", "channel_delay"), [ - (Frame([Qubit(0)], "frame"), None, None, None, None, None), - (Frame([Qubit(1)], "frame"), "direction", 1.39, "hardware_object", 44.1, 440.0), + (Frame([Qubit(0)], "frame"), None, None, None, None, None, None), + (Frame([Qubit(1)], "frame"), "direction", 1.39, "hardware_object", 44.1, 440.0, 0.0), ], ids=("Frame-Only", "With-Optionals"), ) @@ -491,13 +491,17 @@ def def_frame( hardware_object: Optional[str], sample_rate: Optional[float], center_frequency: Optional[float], - ): - optional_args = [ - arg - for arg in [direction, initial_frequency, hardware_object, sample_rate, center_frequency] - if arg is not None - ] - return DefFrame(frame, *optional_args) + channel_delay: Optional[float], + ) -> DefFrame: + args = dict( + direction=direction, + initial_frequency=initial_frequency, + hardware_object=hardware_object, + sample_rate=sample_rate, + center_frequency=center_frequency, + channel_delay=channel_delay, + ) + return DefFrame(frame, **{k: v for k, v in args.items() if v is not None}) def test_out(self, def_frame: DefFrame, snapshot: SnapshotAssertion): # The ordering of attributes isn't stable and can be printed in different orders across calls. @@ -518,51 +522,40 @@ def test_frame(self, def_frame: DefFrame, frame: Frame): assert def_frame.frame == Frame([Qubit(123)], "new_frame") def test_direction(self, def_frame: DefFrame, direction: Optional[str]): - assert def_frame.direction == direction is None if not direction else def_frame["DIRECTION"] + assert def_frame.direction == direction def_frame.direction = "tx" assert def_frame.direction == "tx" def test_initial_frequency(self, def_frame: DefFrame, initial_frequency: Optional[float]): - assert ( - def_frame.initial_frequency == initial_frequency is None - if not initial_frequency - else def_frame["INITIAL-FREQUENCY"] - ) + assert def_frame.initial_frequency == initial_frequency def_frame.initial_frequency = 3.14 assert def_frame.initial_frequency == 3.14 def test_hardware_object(self, def_frame: DefFrame, hardware_object: Optional[str]): - assert ( - def_frame.hardware_object == hardware_object is None - if not hardware_object - else def_frame["HARDWARE-OBJECT"] - ) + assert def_frame.hardware_object == hardware_object def_frame.hardware_object = "bfg" assert def_frame.hardware_object == "bfg" def test_hardware_object_json(self, def_frame: DefFrame, hardware_object: Optional[str]): - assert ( - def_frame.hardware_object == hardware_object is None - if not hardware_object - else def_frame["HARDWARE-OBJECT"] - ) + assert def_frame.hardware_object == hardware_object def_frame.hardware_object = '{"string": "str", "int": 1, "float": 3.14}' assert def_frame.hardware_object == '{"string": "str", "int": 1, "float": 3.14}' def test_sample_rate(self, def_frame: DefFrame, sample_rate: Optional[float]): - assert def_frame.sample_rate == sample_rate is None if not sample_rate else def_frame["SAMPLE-RATE"] + assert def_frame.sample_rate == sample_rate def_frame.sample_rate = 96.0 assert def_frame.sample_rate == 96.0 def test_center_frequency(self, def_frame: DefFrame, center_frequency: Optional[float]): - assert ( - def_frame.center_frequency == center_frequency is None - if not center_frequency - else def_frame.center_frequency - ) + assert def_frame.center_frequency == center_frequency def_frame.center_frequency = 432.0 assert def_frame.center_frequency == 432.0 + def test_channel_delay(self, def_frame: DefFrame, channel_delay: Optional[float]): + assert def_frame.channel_delay == channel_delay + def_frame.channel_delay = 571.0 + assert def_frame.channel_delay == 571.0 + def test_copy(self, def_frame: DefFrame): assert isinstance(copy.copy(def_frame), DefFrame) assert isinstance(copy.deepcopy(def_frame), DefFrame)