-
Notifications
You must be signed in to change notification settings - Fork 1.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
Make Instance GateFamily check for equality ignoring global phase #4542
Make Instance GateFamily check for equality ignoring global phase #4542
Conversation
It looks like there is a significant diffbase in here for neutral atoms and the PR is marked as Draft, so I will hold off reviewing for now. Can you ping reviewers when it is ready? |
7889a0f
to
7043beb
Compare
@dstrain115 This is ready for review now -- I had marked it as draft after realizing the git branching issue which included diffbase from the other neutral atoms PR. |
@dstrain115 Gentle reminder. This is ready for review. |
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.
Drive-by review comment: Would this changeover let people do things like ?
my_gateset = cirq.GateSet(X**0.5, ignore_global_phase=True)
cirq.X ** 0.5 in my_gateset # True
cirq.PhasedXPowGate(phase_exponent=0) in my gateset # !True!
my_gateset2 = cirq.GateSet(X**0.5, ignore_global_phase=False)
cirq.X ** 0.5 in my_gateset2 # True
cirq.PhasedXPowGate(phase_exponent=0) in my gateset2 # False
This seems a little scary if true.
In [2]: cirq.PhasedXPowGate(phase_exponent=0, exponent=0.5) == cirq.X ** 0.5
Out[2]: True Therefore, the example that you quoted would return true in both the cases. I would also not include an explicit isinstance check when doing approximate equality because it would modify the behavior of already well known concepts in Cirq (i.e. Value Equality and Equality ignoring global phase). |
Ahhhh that makes a lot of sense. In my explorations I actually used >>> cirq.PhasedXZGate(x_exponent=0.5, z_exponent=0, axis_phase_exponent=0) == cirq.X**0.5
False But I think that's actually a bug having done a bit more exploring lol. This makes a lot more sense now. My only remaining concern is that I think we should default to having |
The reason I've chosen the default value to be True is because that's what is being used across the codebase via the legacy checks. Eg: At all the places where cirq.Gateset(cirq.GateFamily(cirq.X ** 0.5, ignore_global_phase = True)) instead of cirq.Gateset(cirq.X ** 0.5) Also, note that |
…sets_exp_bug_fix
Now that we are talking about it, I think we should also rename the |
I've renamed |
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.
After offline sync, LGTM with some nits.
@@ -295,7 +310,7 @@ def __contains__(self, item: Union[raw_types.Gate, raw_types.Operation]) -> bool | |||
) | |||
return True | |||
|
|||
return any(item in gate_family for gate_family in self._custom_gate_families) | |||
return any(item in gate_family for gate_family in self._gates) |
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.
Can we add a comment that the above checks will still catch GateFamilys that are typed based and GateFamilys that have ignore_global_phase=False ?
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.
Expanded the docstring comment to explain the new code path.
@MichaelBroughton Thanks for the approval. I've addressed the nits. Merging now. |
Automerge cancelled: A required status check is not present. Missing statuses: ['Pytest Windows (3.7)', 'Pytest Windows (3.8)', 'Pytest Windows (3.9)', 'Typescript lint check', 'Typescript tests', 'Typescript tests coverage'] |
…antumlib#4542) * GateFamily value equality ignoring global phase * Rename accept_global_phase to accept_global_phase_op
…antumlib#4542) * GateFamily value equality ignoring global phase * Rename accept_global_phase to accept_global_phase_op
The recent adoption of
GateFamily
across Cirq introduced an unnoticed and unintended change -- i.e. equality comparison of eigen gates now depended upon theglobal_shift
factor as well. For instance, before the adoption of Gatesets, an isinstance check of the formisinstance(op, cirq.XPowGate) and op.exponent == 1
would acceptcirq.X
and also acceptcirq.Rx(rads = np.pi)
. The latter would get rejected now since GateFamily's started relying only on value equality checks.Since comparison upto global phase is what is often needed, this PR introduces a configurable option to
GateFamily
and updates the default behavior of checking value equality viacirq.equal_up_to_global_phase
. Also added tests atGateFamily
use sites which would have failed in the original implementation.Part of roadmap item #3243