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

JSON serialization: namespaces for cirq_type #4377

Open
mpharrigan opened this issue Aug 2, 2021 · 8 comments
Open

JSON serialization: namespaces for cirq_type #4377

mpharrigan opened this issue Aug 2, 2021 · 8 comments
Assignees
Labels
area/serialization good part time project A meaty non-urgent issue with a substantial amount of work to be done. kind/health For CI/testing/release process/refactoring/technical debt items no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. priority/p2 Next release should contain it triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@mpharrigan
Copy link
Collaborator

Right now in cirq and all modules in the main cirq repo, the cirq_type field used in json serialization is the unqualified object name. This has not caused any naming collisions yet.

I had always imagined that different packages would have a namespaced cirq_type. For example: cirq_google.PhysicalZTag or cirq.google.PhysicalZTag instead of PhysicalZTag.

In fact, cirq.obj_to_dict_helper has an optional namespace argument. I tried to use this for a new cirq_google object, but the testing procedure uses the unqualified object name to assert that a data file should exist.

While playing around with #4375 I think it would be more robust to decouple the "object generation" from any semantic meaning. Instead, we generate all the example objects and keep track of the types we've successfully round-tripped. Afterwards we can check if there are public objects that weren't round-tripped.

@mpharrigan mpharrigan added the kind/bug-report Something doesn't seem to work. label Aug 2, 2021
@viathor viathor added area/serialization triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Aug 6, 2021
@mpharrigan
Copy link
Collaborator Author

#4527 adds a hook into the current testing infrastructure to make it possible to make namespaced objects pass the tests, although it requires manually adding a mapping from unqualified class name to cirq_type, which doesn't solve the disambiguation problem.

@vtomole vtomole added triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add priority/p2 Next release should contain it good part time project A meaty non-urgent issue with a substantial amount of work to be done. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque kind/bug-report Something doesn't seem to work. labels Oct 13, 2021
@viathor viathor added the kind/health For CI/testing/release process/refactoring/technical debt items label Oct 16, 2021
@dstrain115
Copy link
Collaborator

Tentatively putting pre-1.0 onto this issue. It sounds like this issue is mostly done. Tagging relevant people to update the issue and retriage.

@mpharrigan
Copy link
Collaborator Author

cc #4698

Namespacing has received some upgrades for users of Cirq.

The testing infrastructure is what isn't set up for it. There's a workaround merged into the testing spec.py, which you can see in action for all of the cirq_google.workflow dataclasses. I think we'd need a pretty big re-work of the JSON testing framework to truly fix it. The only quote-unquote real issue would come if we had to classes with the same name but different namespaces in the Cirq repository. I don't think there are currently any need for this, and is probably bad form anyway.

Moving to time/after-1.0 and noting that this should be part of a broader json testing rework.

@verult
Copy link
Collaborator

verult commented Jun 14, 2022

To add some data points, the discussion of namespaces came up in two places recently:

  1. In determining how to disambiguate cirq_google.GridDevice should the name GridDevice appear elsewhere in the future
  2. @tanujkhattar saw that MSGate is defined in both cirq-core and cirq-ionq.

I would advocate for requiring all newly created classes in vendor directories to have a JSON namespace in order to minimize future collisions. If the full enforcement mechanism will take time to build, we can start by documenting the policy, and cirq-maintainers could act as gatekeepers for now. If we fix this only after seeing lots of collisions, it would be too late because we can't change existing JSON serializations AFAIU.

@95-martin-orion
Copy link
Collaborator

I would advocate for requiring all newly created classes in vendor directories to have a JSON namespace in order to minimize future collisions. If the full enforcement mechanism will take time to build, we can start by documenting the policy, and cirq-maintainers could act as gatekeepers for now.

This is actually really easy to enforce. Currently all classes whose module starts with 'cirq' default to a blank namespace:

if hasattr(type_obj, '_json_namespace_'):
return type_obj._json_namespace_()
if type_obj.__module__.startswith('cirq'):
return ''
raise ValueError(f'{type_obj} is not a Cirq type, and does not define _json_namespace_.')

Changing line 595 to if type_obj.__module__.split('.')[0] == 'cirq': is likely sufficient. The hard part is converting existing types - a backwards-compatibility map will be required for all vendor-module types in violation of this rule, which notably includes several types in cirq-google.

@mpharrigan
Copy link
Collaborator Author

You could have an explicit list of grandfathered-in exceptions on line 595. Not the prettiest thing but effective

@mpharrigan
Copy link
Collaborator Author

@95-martin-orion didn't you implement a requirement for namespaces to be required?

@95-martin-orion
Copy link
Collaborator

@95-martin-orion didn't you implement a requirement for namespaces to be required?

It looks like #4693 does this (and more specifically this line, as seen in my previous comment), but we never went as far as my suggestion above. Notably, cirq_* modules all fulfill the startswith('cirq') condition, so they're technically allowed to not specify a namespace.

Probably the reason for this is that cirq_google has several namespace-less types to work around, and this wasn't a priority in the push to v1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/serialization good part time project A meaty non-urgent issue with a substantial amount of work to be done. kind/health For CI/testing/release process/refactoring/technical debt items no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. priority/p2 Next release should contain it triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
None yet
Development

No branches or pull requests

7 participants