-
Notifications
You must be signed in to change notification settings - Fork 984
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
Add explicit JSON resolver function in contrib #2559
Conversation
cirq/testing/json.py
Outdated
import cirq | ||
|
||
|
||
def assert_roundtrip(obj, text_should_be=None, resolvers=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is too vague; there is not enough context to know what cirq.testing.assert_roundtrip
refers to.
Rename this to something more explicit like assert_json_roundtrip_works
.
We may want to include this into the standard test battery for gates (assert_implements_consistent_protocols
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the name, thanks!
I tried adding this to assert_implements_consistent_protocols, but that function is called with a ton of test gates (e.g. GateUsingWorkspaceForApplyUnitary
) :\
cirq/contrib/json.py
Outdated
classes = [ | ||
QuantumVolumeResult, | ||
] | ||
print(QuantumVolumeResult.__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover debug line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
D'oh I fixed that and forgot to commit it :P Done.
Friendly ping :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good to me
|
||
""" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could add
DEFAULT_RESOLVERS = [contrib_class_resolver] + protocols.json.DEFAULT_RESOLVERS
then
import cirq
import cirq.contrib
data = cirq.read_json(fn, resolvers=cirq.contrib.DEFAULT_RESOLVERS)
The above relies on python namespacing for disambiguation. You could make the name more specific: DEFAULT_CONTRIB_RESOLVERS
if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, done and used in the test.
@Strilanc you good? |
Automerge cancelled: Need a fresh 🍪. |
Fixes #2445
This PR adds an explicit function to return a JSON serializer containing Contrib classes. Right now, only QuantumVolumeResult is JSONable, but in the future other classes can be added here as well. Users who want to use the JSON family of functions will need to explicitly pass the contrib_class_resolver, as is done in the test.
Additionally, this pulls out a useful json_test function into the testing directory, for use elsewhere.