diff --git a/changes.d/696.fix b/changes.d/696.fix new file mode 100644 index 000000000..9df69bd0b --- /dev/null +++ b/changes.d/696.fix @@ -0,0 +1 @@ +`ConstantPulseTemplate`s from all versions can now be deserialized. \ No newline at end of file diff --git a/qupulse/pulses/constant_pulse_template.py b/qupulse/pulses/constant_pulse_template.py index 9b712f0ea..24ae16847 100644 --- a/qupulse/pulses/constant_pulse_template.py +++ b/qupulse/pulses/constant_pulse_template.py @@ -66,6 +66,15 @@ def get_serialization_data(self, serializer=None) -> Any: }) return data + @classmethod + def deserialize(cls, serializer: Optional = None, **kwargs) -> 'ConstantPulseTemplate': + assert serializer is None, f"{cls} does not support legacy deserialization" + # this is for backwards compatible deserialization. + amplitudes = kwargs.pop('#amplitudes', None) + if amplitudes is not None: + kwargs['amplitude_dict'] = amplitudes + return cls(**kwargs) + @property def integral(self) -> Dict[ChannelID, ExpressionScalar]: """Returns an expression giving the integral over the pulse.""" diff --git a/tests/pulses/constant_pulse_template_tests.py b/tests/pulses/constant_pulse_template_tests.py index a3718eb7d..ead1ce597 100644 --- a/tests/pulses/constant_pulse_template_tests.py +++ b/tests/pulses/constant_pulse_template_tests.py @@ -10,6 +10,7 @@ from qupulse._program._loop import make_compatible from qupulse._program.waveforms import ConstantWaveform +from qupulse.serialization import DictBackend, PulseStorage from qupulse.pulses.constant_pulse_template import ConstantPulseTemplate, ExpressionScalar, TimeType from tests.serialization_tests import SerializableTests @@ -164,3 +165,24 @@ def assert_equal_instance_except_id(self, lhs: ConstantPulseTemplate, rhs: Const self.assertEqual(lhs.measurement_declarations, rhs.measurement_declarations) self.assertEqual(lhs._amplitude_dict, rhs._amplitude_dict) self.assertEqual(lhs.duration, rhs.duration) + + def test_legacy_deserialization(self): + serialized = """{ + "#amplitudes": { + "ZI0_A_MARKER_FRONT": 1 + }, + "#type": "qupulse.pulses.constant_pulse_template.ConstantPulseTemplate", + "duration": 62848.0, + "name": "constant_pulse" + }""" + backend = DictBackend() + backend.storage['my_pt'] = serialized + + ps = PulseStorage(backend) + + deserialized = ps['my_pt'] + expected = ConstantPulseTemplate( + amplitude_dict={"ZI0_A_MARKER_FRONT": 1}, + duration=62848, name="constant_pulse" + ) + self.assert_equal_instance(expected, deserialized)