-
Notifications
You must be signed in to change notification settings - Fork 1k
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 assert_decompose_ends_at_default_gateset consistency test #5079
Add assert_decompose_ends_at_default_gateset consistency test #5079
Conversation
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.
Code looks good, a few high level questions.
"""Ensures that all cirq gate decompositions end at the default cirq gateset.""" | ||
|
||
# pylint: disable=unused-variable | ||
__tracebackhide__ = True |
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.
What is this for ?
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 is useful to hide the traceback of assertion calls, in order to avoid clutter in user code if tests fail.
It's used as a general pattern across all cirq/testing/ methods. I've removed it for now as I think the assertion trace is very useful here, especially while this is still a WIP and further decompositions are being added in subsequent PRs.
if protocols.is_parameterized(val): | ||
return |
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.
Doesn't decompose work for gates that have symbols in them too ? Maybe it would be best here to assert that this is being called on a parameter free val
so one doesn't accidentally mistake accidentally putting in a symbol for success. WDYT ?
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.
We don't have general purpose analytical decompositions for parameterized gates which makes it much harder to ensure that the decompositions of parameterize gates ends up in the default gateset (eg: parameterized controlled gates and matrix gates).
However, I gave some more thought to it and instead of ignoring all parameterized gates or all gates without a decompose implemented (done below), I've added a dedicated method _known_gate_with_no_decomposition
, which should be used as an escape patch to ignore gates which are known to not have automatic decompositions into the target gateset.
All cases (eg: parameterized controlled gates) for which we do not support automatic decomposition, will be added to this method in subsequent PRs.
if dec_once is None: | ||
# _decompose_ is NotImplemented, so silently return. | ||
return |
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.
Same question as above, if not implemented wouldn't we want to fail loudly ?
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.
Addressed the feedback, see my comment above.
…rt_decompose_ends_at_default_gateset
…ignore gates without known decompositions
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.
I've restructured the code and added a dedicated method _known_gate_with_no_decomposition
, where we will add all known cases where default decompositions don't exist, in future PRs.
This is ready for another look.
if protocols.is_parameterized(val): | ||
return |
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.
We don't have general purpose analytical decompositions for parameterized gates which makes it much harder to ensure that the decompositions of parameterize gates ends up in the default gateset (eg: parameterized controlled gates and matrix gates).
However, I gave some more thought to it and instead of ignoring all parameterized gates or all gates without a decompose implemented (done below), I've added a dedicated method _known_gate_with_no_decomposition
, which should be used as an escape patch to ignore gates which are known to not have automatic decompositions into the target gateset.
All cases (eg: parameterized controlled gates) for which we do not support automatic decomposition, will be added to this method in subsequent PRs.
if dec_once is None: | ||
# _decompose_ is NotImplemented, so silently return. | ||
return |
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.
Addressed the feedback, see my comment above.
"""Ensures that all cirq gate decompositions end at the default cirq gateset.""" | ||
|
||
# pylint: disable=unused-variable | ||
__tracebackhide__ = True |
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 is useful to hide the traceback of assertion calls, in order to avoid clutter in user code if tests fail.
It's used as a general pattern across all cirq/testing/ methods. I've removed it for now as I think the assertion trace is very useful here, especially while this is still a WIP and further decompositions are being added in subsequent PRs.
…rt_decompose_ends_at_default_gateset
…rt_decompose_ends_at_default_gateset
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.
LGTM
…mlib#5079) * Add assert_decompose_ends_at_default_gateset consistency test * Refactor assert_decompose_ends_at_default_gateset to provide hook to ignore gates without known decompositions
…mlib#5079) * Add assert_decompose_ends_at_default_gateset consistency test * Refactor assert_decompose_ends_at_default_gateset to provide hook to ignore gates without known decompositions
cirq.testing.assert_decompose_ends_at_default_gateset
which checks that if the given object (gate / operation) has a decompose method defined, then it should always end up decomposing to the cirq default gateset.cirq.testing.assert_implements_consistent_protocols
to extend the coverage of this test across all cirq objects.cc @MichaelBroughton