Skip to content

Commit

Permalink
Restore backwards compatibility on the legacy serializer (#555)
Browse files Browse the repository at this point in the history
* Restore backwards compatibility on the legacy serializer

* Pin jsonschema

* Pin pydantic
  • Loading branch information
HGSilveri authored Jul 10, 2023
1 parent ee85197 commit 1cf1b6e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.13.2
0.13.3
12 changes: 12 additions & 0 deletions pulser-core/pulser/json/coders.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def object_hook(self, obj: dict[str, Any]) -> Any:
if not build:
return cls

if "Device" in obj_name:
# Handle the removal of _channels for backwards compatibility
_channels = obj["__kwargs__"].pop("_channels", None)
channel_objs = obj["__kwargs__"].get("channel_objects", None)
channel_ids = obj["__kwargs__"].get("channel_ids", None)
if _channels and not (channel_ids or channel_objs):
_channels_dict = dict(_channels)
obj["__kwargs__"]["channel_ids"] = tuple(_channels_dict.keys())
obj["__kwargs__"]["channel_objects"] = tuple(
_channels_dict.values()
)

if "Sequence" in obj_name:
seq = cls(*obj["__args__"], **obj["__kwargs__"])
for name, args, kwargs in obj["calls"]:
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jsonschema
jsonschema < 4.18
matplotlib
# Numpy 1.20 introduces type hints, 1.24.0 breaks matplotlib < 3.6.1
numpy >= 1.20, != 1.24.0
Expand Down
1 change: 1 addition & 0 deletions pulser-pasqal/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pasqal-cloud ~= 0.2.3
pydantic < 2.0
23 changes: 23 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,26 @@ def test_sequence_module():
# Check that it also works
s = json.dumps(obj_dict)
Sequence.deserialize(s)


def test_deprecation():
seq = Sequence(Register.square(1), MockDevice)

seq_dict = json.loads(seq.serialize())
dev_dict = seq_dict["__kwargs__"]["device"]

assert "_channels" not in dev_dict["__kwargs__"]
dev_dict["__kwargs__"]["_channels"] = []

s = json.dumps(seq_dict)
new_seq = Sequence.deserialize(s)
assert new_seq.device == MockDevice

ids = dev_dict["__kwargs__"].pop("channel_ids")
ch_objs = dev_dict["__kwargs__"].pop("channel_objects")
dev_dict["__kwargs__"]["_channels"] = tuple(zip(ids, ch_objs))

assert seq_dict["__kwargs__"]["device"] == dev_dict
s = json.dumps(seq_dict)
new_seq = Sequence.deserialize(s)
assert new_seq.device == MockDevice

0 comments on commit 1cf1b6e

Please sign in to comment.