Skip to content

Commit

Permalink
fix: Add deprecated property to DefFrame for CHANNEL-DELAY attribute (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
Co-authored-by: Marquess Valdez <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent e1572d8 commit b15e429
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
16 changes: 16 additions & 0 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/unit/__snapshots__/test_quilbase.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
57 changes: 25 additions & 32 deletions test/unit/test_quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand All @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit b15e429

Please sign in to comment.