From 2e1a8b8c5a1126e7516551c719289c2ed8fe35f1 Mon Sep 17 00:00:00 2001 From: Satya Bade <76647614+SatyaBade12@users.noreply.github.com> Date: Mon, 25 Jul 2022 15:20:41 +0200 Subject: [PATCH] Renamed arguments in Element and MeasureElement classes #124 (#127) * Renamed arguments in Element and MeasureElement classes #124 * Add test to check DeprecationWarning --- CHANGELOG.md | 2 + qualang_tools/config/components.py | 121 ++++++++++++++++++----------- qualang_tools/config/exceptions.py | 15 ++++ tests/test_config_builder.py | 23 ++++++ 4 files changed, 116 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1656ada1..764beff1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] +### Changed +- ConfigBuilder - renamed arguments (backward compatible) in Element and MeasureElement classes. ## [0.11.2] - 2022-07-25 ### Changed diff --git a/qualang_tools/config/components.py b/qualang_tools/config/components.py index feb0355c..fe3ad717 100644 --- a/qualang_tools/config/components.py +++ b/qualang_tools/config/components.py @@ -1,7 +1,7 @@ import abc from typing import Union, List, Tuple, Optional, Dict -from qualang_tools.config.exceptions import ConfigurationError +from qualang_tools.config.exceptions import ConfigurationError, _warn_if_not_none from qualang_tools.config.primitive_components import ( ConfigBuilderElement, AnalogInputPort, @@ -355,10 +355,9 @@ class Element(ConfigBuilderElement): def __init__( self, name: str, - analog_input_ports: Optional[List[AnalogOutputPort]] = None, - analog_output_ports: Optional[List[AnalogInputPort]] = None, - digital_input_ports: Optional[List[DigitalOutputPort]] = None, - digital_output_ports: Optional[List[DigitalInputPort]] = None, + element_analog_inputs: Optional[List[AnalogOutputPort]] = None, + element_analog_outputs: Optional[List[AnalogInputPort]] = None, + element_digital_inputs: Optional[List[DigitalOutputPort]] = None, intermediate_frequency: Optional[int] = None, lo_frequency: Optional[int] = None, **kwargs @@ -367,12 +366,12 @@ def __init__( :param name: element name :type name: int - :param analog_input_ports: AnalogOutputPort(s) associated with this element, defaults to [] - :type analog_input_ports: List[AnalogOutputPort], optional - :param analog_output_ports: AnalogInputPort(s) associated with this element, defaults to [] - :type analog_output_ports: List[AnalogInputPort], optional - :param digital_input_ports: DigitalOutputPort(s) associated with this element, defaults to [] - :type digital_input_ports: List[DigitalOutputPort], optional + :param element_analog_inputs: AnalogOutputPort(s) associated with this element, defaults to [] + :type element_analog_inputs: List[AnalogOutputPort], optional + :param element_analog_outputs: AnalogInputPort(s) associated with this element, defaults to [] + :type element_analog_outputs: List[AnalogInputPort], optional + :param element_digital_inputs: DigitalOutputPort(s) associated with this element, defaults to [] + :type element_digital_inputs: List[DigitalOutputPort], optional :param intermediate_frequency: intermediate frequency associated wit this element, defaults to None :type intermediate_frequency: int, optional :param lo_frequency: LO frequency associated wit this element, defaults to None @@ -384,50 +383,67 @@ def __init__( pulses: Optional[List[Pulse]] = kwargs.get("pulses", None) operations: Optional[Dict[str, str]] = kwargs.get("digital_marker", None) + analog_input_ports = _warn_if_not_none(kwargs, "analog_input_ports") + if analog_input_ports is not None: + element_analog_inputs = analog_input_ports + + analog_output_ports = _warn_if_not_none(kwargs, "analog_output_ports") + if analog_output_ports is not None: + element_analog_outputs = analog_output_ports + + digital_input_ports = _warn_if_not_none(kwargs, "digital_input_ports") + if digital_input_ports is not None: + element_digital_inputs = digital_input_ports + + element_digital_outputs = [] + digital_output_ports = _warn_if_not_none(kwargs, "digital_output_ports") + if digital_output_ports is not None: + element_digital_outputs = digital_output_ports + self.dict = dict() - assert len(analog_input_ports) <= 2 - self.type = "singleInput" if len(analog_input_ports) == 1 else "mixInputs" + assert len(element_analog_inputs) <= 2 + self.type = "singleInput" if len(element_analog_inputs) == 1 else "mixInputs" self.pulses = [] if pulses is not None: self.pulses = pulses - self.analog_input_ports = ( - [] if analog_input_ports is None else analog_input_ports + self.element_analog_inputs = ( + [] if element_analog_inputs is None else element_analog_inputs ) - self.analog_output_ports = ( - [] if analog_output_ports is None else analog_output_ports + self.element_analog_outputs = ( + [] if element_analog_outputs is None else element_analog_outputs ) - self.digital_output_ports = ( - [] if digital_output_ports is None else digital_output_ports + self.element_digital_outputs = ( + [] if element_digital_outputs is None else element_digital_outputs ) - self.digital_input_ports = ( - [] if digital_input_ports is None else digital_input_ports + self.element_digital_inputs = ( + [] if element_digital_inputs is None else element_digital_inputs ) self.mixer: Mixer = mixer - if len(self.analog_input_ports) > 0: + if len(self.element_analog_inputs) > 0: if self.type == "singleInput": - port = self.analog_input_ports[0] + port = self.element_analog_inputs[0] self.dict["singleInput"] = dict() self.dict["singleInput"]["port"] = port.info elif self.type == "mixInputs": - I = self.analog_input_ports[0] - Q = self.analog_input_ports[1] + I = self.element_analog_inputs[0] + Q = self.element_analog_inputs[1] self.dict["mixInputs"] = dict() self.dict["mixInputs"]["I"] = I.info self.dict["mixInputs"]["Q"] = Q.info self.dict["mixInputs"]["lo_frequency"] = lo_frequency - if len(self.analog_output_ports) > 0: + if len(self.element_analog_outputs) > 0: self.dict["outputs"] = dict() - for i, port in enumerate(self.analog_output_ports): + for i, port in enumerate(self.element_analog_outputs): self.dict["outputs"]["out" + str(i + 1)] = port.info self.dict["operations"] = dict() if operations is not None: self.dict["operations"] = operations self.dict["intermediate_frequency"] = intermediate_frequency # self.dict["outputPulseParameters"] = dict() - if len(self.digital_input_ports) > 0: + if len(self.element_digital_inputs) > 0: self.dict["digitalInputs"] = dict() - for i, port in enumerate(self.digital_input_ports): + for i, port in enumerate(self.element_digital_inputs): self.dict["digitalInputs"]["in" + str(i + 1)] = { "port": port.info, "delay": 0, @@ -621,9 +637,9 @@ class MeasureElement(Element): def __init__( self, name: str, - analog_input_ports: List[AnalogOutputPort], - analog_output_ports: List[AnalogInputPort], - digital_input_ports: Optional[List[DigitalOutputPort]] = None, + element_analog_inputs: List[AnalogOutputPort], + element_analog_outputs: List[AnalogInputPort], + element_digital_inputs: Optional[List[DigitalOutputPort]] = None, intermediate_frequency: Optional[int] = None, lo_frequency: Optional[int] = None, **kwargs @@ -632,12 +648,12 @@ def __init__( :param name: [description] :type name: int - :param analog_input_ports: [description] - :type analog_input_ports: List[AnalogOutputPort] - :param analog_output_ports: [description] - :type analog_output_ports: List[AnalogInputPort] - :param digital_input_ports: [description], defaults to [] - :type digital_input_ports: List[DigitalOutputPort], optional + :param element_analog_inputs: [description] + :type element_analog_inputs: List[AnalogOutputPort] + :param element_analog_outputs: [description] + :type element_analog_outputs: List[AnalogInputPort] + :param element_digital_inputs: [description], defaults to [] + :type element_digital_inputs: List[DigitalOutputPort], optional :param intermediate_frequency: intermediate frequency associated wit this element, defaults to None :type intermediate_frequency: int, optional :param lo_frequency: [description], defaults to None @@ -646,20 +662,35 @@ def __init__( time_of_flight: Optional[int] = kwargs.get("time_of_flight", 0) smearing: Optional[int] = kwargs.get("smearing", 0) - digital_output_ports: Optional[List[DigitalInputPort]] = kwargs.get( - "digital_output_ports", None - ) mixer: Optional[Mixer] = kwargs.get("mixer", None) pulses: Optional[List[Pulse]] = kwargs.get("pulses", None) operations: Optional[Dict[str, str]] = kwargs.get("operations", None) + + analog_input_ports = _warn_if_not_none(kwargs, "analog_input_ports") + if analog_input_ports is not None: + element_analog_inputs = analog_input_ports + + analog_output_ports = _warn_if_not_none(kwargs, "analog_output_ports") + if analog_output_ports is not None: + element_analog_outputs = analog_output_ports + + digital_input_ports = _warn_if_not_none(kwargs, "digital_input_ports") + if digital_input_ports is not None: + element_digital_inputs = digital_input_ports + + element_digital_outputs = [] + digital_output_ports = _warn_if_not_none(kwargs, "digital_output_ports") + if digital_output_ports is not None: + element_digital_outputs = digital_output_ports + super().__init__( name, - analog_input_ports, - analog_output_ports, - digital_input_ports, - digital_output_ports, + element_analog_inputs, + element_analog_outputs, + element_digital_inputs, intermediate_frequency, lo_frequency, + element_digital_outputs=element_digital_outputs, mixer=mixer, pulses=pulses, operations=operations, diff --git a/qualang_tools/config/exceptions.py b/qualang_tools/config/exceptions.py index 8021dafd..e4779353 100644 --- a/qualang_tools/config/exceptions.py +++ b/qualang_tools/config/exceptions.py @@ -1,4 +1,19 @@ +import warnings +import re + + class ConfigurationError(Exception): def __init__(self, message) -> None: self.message = message super().__init__(self.message) + + +def _warn_if_not_none(kwargs, deprecated_arg): + suggested_arg = "element_" + re.split("_ports", deprecated_arg)[0] + "s" + val = kwargs.get(deprecated_arg, None) + if val is not None: + warnings.warn( + "{0} is deprecated use {1}".format(deprecated_arg, suggested_arg), + DeprecationWarning, + ) + return val diff --git a/tests/test_config_builder.py b/tests/test_config_builder.py index 9423fa47..cfe3c18a 100644 --- a/tests/test_config_builder.py +++ b/tests/test_config_builder.py @@ -382,3 +382,26 @@ def test_parameter_algebra(): assert (c / 10)() == c() / 10 assert (d**c)() == 10000 assert (c**2)() == 16 + + +def test_deprecated_warning(): + with pytest.warns(None) as record: + cb = ConfigBuilder() + cont = Controller("con1") + cb.add(cont) + elm = Element("elm", analog_input_ports=[cont.analog_output(1)] + ) + + cb.add(elm) + cb.build() + assert len(record.list) == 1 + + with pytest.warns(None) as record: + cb = ConfigBuilder() + cont = Controller("con1") + cb.add(cont) + elm = Element("elm", element_analog_inputs=[cont.analog_output(1)] + ) + cb.add(elm) + cb.build() + assert len(record.list) == 0 \ No newline at end of file