Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization support for dataclasses #339

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

pavoljuhas
Copy link
Contributor

@pavoljuhas pavoljuhas commented Feb 6, 2024

TestClass._json_dict_() needs to be an instance method.

Fixes serialization of ReadoutScanTask in data_collection.ipynb notebook.

Fixes serialization of ReadoutScanTask in data_collection.ipynb notebook.
@@ -213,8 +213,9 @@ def wrap(cls):

cls._json_namespace_ = classmethod(lambda obj: namespace)

cls._json_dict_ = classmethod(lambda obj: cirq.obj_to_dict_helper(
obj, [f.name for f in dataclasses.fields(cls)]))
cls._json_dict_ = lambda obj: cirq.obj_to_dict_helper(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure, this is what I changed here:
#337
and it failed with the message "requiring positional argument obj"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am looking at https://quantumai.google/cirq/dev/serialization#adding_a_new_serializable_value and to my understanding _json_dict_ needs to be an instance method to return the instance data.

I left the _json_namespace_ a classmethod - as it was set in your PR. I have also updated the test_json_serializable_class unit test which fails at master, but passes in this PR. Finally, I did a quick test run of the failing notebooks data_collection.ipynb and data_analysis.ipynb which seem to work after this change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you are correct. I got json_dict and json_namespace confused.

@dstrain115 dstrain115 merged commit ccae62b into quantumlib:master Feb 6, 2024
5 checks passed
@pavoljuhas pavoljuhas deleted the fix-serialization branch February 6, 2024 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants