Skip to content

Pulse serialization fails for arithmetic pulse template #775

@maxbeer99

Description

@maxbeer99

Running:

import qupulse
import qupulse.serialization
import qupulse.pulses

DEFAULT_STORAGE_BACKEND_PATH = r"_tmp/pulse_storage/"
pulse_storage = qupulse.serialization.PulseStorage(qupulse.serialization.FilesystemBackend(DEFAULT_STORAGE_BACKEND_PATH, create_if_missing=True))
pulse_storage.set_to_default_registry()

pulse1 = qupulse.pulses.MappingPT(qupulse.pulses.PointPT([(0,0)], ["c"]),identifier="pulse1")
pulse2 = qupulse.pulses.MappingPT(2 * qupulse.pulses.PointPT([(0,0)], ["c"]),identifier="pulse2")

And then, after kernel restart:

import qupulse
import qupulse.serialization
import qupulse.pulses

DEFAULT_STORAGE_BACKEND_PATH = r"_tmp/pulse_storage/"
pulse_storage = qupulse.serialization.PulseStorage(qupulse.serialization.FilesystemBackend(DEFAULT_STORAGE_BACKEND_PATH, create_if_missing=True))
pulse_storage.set_to_default_registry()

print(pulse_storage["pulse1"].__repr__())
print(pulse_storage["pulse2"].__repr__())

Yields the following:

MappingPulseTemplate(template=PointPulseTemplate(time_point_tuple_list=[(0, 0, 'hold')],channel_names=('c',)),channel_mapping={'c': 'c'})

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 10
      7 pulse_storage.set_to_default_registry()
      9 print(pulse_storage["pulse1"].__repr__())
---> 10 print(pulse_storage["pulse2"].__repr__())

File ~\Documents\Userfiles\Beer\repos\qupulse\qupulse\serialization.py:861, in PulseStorage.__getitem__(self, identifier)
    852 """Fetch a Serializable.
    853 
    854 If the Serializable is not present in temporary storage, it will be loaded and deserialized from the storage
   (...)
    858     identifier: The identifier of the Serializable to load.
    859 """
    860 if identifier not in self._temporary_storage:
--> 861     self._load_and_deserialize(identifier)
    862 return self._temporary_storage[identifier].serializable

File ~\Documents\Userfiles\Beer\repos\qupulse\qupulse\serialization.py:836, in PulseStorage._load_and_deserialize(self, identifier)
    834 def _load_and_deserialize(self, identifier: str) -> StorageEntry:
    835     serialization = self._storage_backend[identifier]
--> 836     serializable = self._deserialize(serialization)
    837     self._temporary_storage[identifier] = PulseStorage.StorageEntry(serialization=serialization,
    838                                                                     serializable=serializable)
    839     return self._temporary_storage[identifier]

File ~\Documents\Userfiles\Beer\repos\qupulse\qupulse\serialization.py:831, in PulseStorage._deserialize(self, serialization)
    829 def _deserialize(self, serialization: str) -> Serializable:
    830     decoder = JSONSerializableDecoder(storage=self)
--> 831     serializable = decoder.decode(serialization)
    832     return serializable

File ~\Miniconda3\envs\beer_setup\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338     end = _w(s, end).end()
    339     if end != len(s):

File ~\Miniconda3\envs\beer_setup\lib\json\decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
    344 """Decode a JSON document from ``s`` (a ``str`` beginning with
    345 a JSON document) and return a 2-tuple of the Python
    346 representation and the index in ``s`` where the document ended.
   (...)
    350 
    351 """
    352 try:
--> 353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
    355     raise JSONDecodeError("Expecting value", s, err.value) from None

File ~\Documents\Userfiles\Beer\repos\qupulse\qupulse\serialization.py:1027, in JSONSerializableDecoder.filter_serializables(self, obj_dict)
   1024         if get_default_pulse_registry() is self.storage:
   1025             registry = dict()
-> 1027         return deserialization_callback(identifier=obj_identifier, registry=registry, **obj_dict)
   1028 return obj_dict

File ~\Documents\Userfiles\Beer\repos\qupulse\qupulse\serialization.py:582, in Serializable.deserialize(cls, serializer, **kwargs)
    578 if serializer:
    579     warnings.warn("{c}.deserialize(*) was called with a serializer argument, indicating deprecated behavior. Please switch to the new serialization routines.".format(c=cls.__name__),
    580                   DeprecationWarning, stacklevel=2)
--> 582 return cls(**kwargs)

TypeError: __init__() got an unexpected keyword argument 'registry'

From the first line we see everything is fine. Afterwards the de-serialization fails for the implicitly generated ArithmeticPT.

The generated pulse2.json looks like this:

{
    "#identifier": "pulse2",
    "#type": "qupulse.pulses.mapping_pulse_template.MappingPulseTemplate",
    "channel_mapping": {
        "c": "c"
    },
    "template": {
        "#type": "qupulse.pulses.arithmetic_pulse_template.ArithmeticPulseTemplate",
        "arithmetic_operator": "*",
        "lhs": 2,
        "rhs": {
            "#type": "qupulse.pulses.point_pulse_template.PointPulseTemplate",
            "channel_names": [
                "c"
            ],
            "time_point_tuple_list": [
                [
                    0,
                    0,
                    "hold"
                ]
            ]
        }
    }
}

Might this error be caused by ArithmeticPT not having the template field?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions