-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Attempt canonicalization first when decomposing controlled gates #7242 #7269
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7269 +/- ##
==========================================
+ Coverage 98.64% 98.67% +0.02%
==========================================
Files 1106 1106
Lines 95985 96102 +117
==========================================
+ Hits 94688 94824 +136
+ Misses 1297 1278 -19 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
# Prefer the subgate controlled version if available | ||
if self != controlled_sub_gate: | ||
# Prevent 2-cycle from appearing in recursive decomposition | ||
if not isinstance(controlled_sub_gate, ControlledGate) or not isinstance( |
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.
Is this condition possible? The constructor for ControlledGate absorbs the control layers if the subgate is another ControlledGate. Do any of the unit tests fail if this check is removed?
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.
Yes, I tried first without and the unit tests were failing.
I discovered that a 2-cycle forms in the decomposition for something like CCZ and control_qid_shape = [3].
The following happens:
- when the sub gate is CZ,
CZPowGate.controlled()
sees thatcontrol_qid_shape[-1] != 2
and returns aControlledGate
. Thenself != controlled_sub_gate
is false. Next, the special case inControlledGate._decompose_with_context_()
forCZPowGate
creates aZ
gate and adds +1 to the control qubits; - when the sub gate is Z, the
ZPowGate.controlled()
method returns aCZPowGate
and subtracts -1 from the control qubits. Thenself != controlled_sub_gate
is true.
This cycles infinitely, adding and removing a control qubit.
if self != controlled_sub_gate: | ||
# Prevent 2-cycle from appearing in recursive decomposition | ||
if not isinstance(controlled_sub_gate, ControlledGate) or not isinstance( | ||
controlled_sub_gate.sub_gate, common_gates.CZPowGate |
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.
Do any of the unit tests depend on this condition? If #7241 is done, does that mitigate the need for this check?
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 am not sure, can you check the example in the comment above and see if it would work when #7241 is resolved?
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.
Will give it a look tonight if I get a chance
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.
Yeah, seems like if we make the end of CXPowGate.controlled
look something like
if self._global_shift != 0 or not isinstance(result, controlled_gate.ControlledGate):
return result
if (
isinstance(result.control_values, cv.ProductOfSums)
and result.control_values[-1] == (1,)
and result.control_qid_shape[-1] == 2
):
return cirq.CCXPowGate(
exponent=self._exponent, global_shift=self._global_shift
).controlled(
result.num_controls() - 1, result.control_values[:-1], result.control_qid_shape[:-1]
)
return controlled_gate.ControlledGate(
XPowGate(exponent=self.exponent),
num_controls=result.num_controls() + 1,
control_values=result.control_values & cv.ProductOfSums([1]),
control_qid_shape=result.control_qid_shape + (2,),
)
(basically updating the final return value to be a controlled X with an extra control value, instead of a controlled CX), and similar for CZPowGate
, then that mitigates the need for either of those checks.
I think any of the following approaches would work:
- Do Eliminate multiple control layers on CX/CZ.controlled([0]) #7241 first and pull that into here once it's been merged.
- Do Eliminate multiple control layers on CX/CZ.controlled([0]) #7241 and Attempt canonicalization first when decomposing controlled gates #7242 both here in the same PR.
- Just add a comment to your
if
conditions that they can be removed once Eliminate multiple control layers on CX/CZ.controlled([0]) #7241 is complete.
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 would prefer to add a comment and then remove the check when doing #7241, what do you think?
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.
That works. An ulterior motive for the set of issues I posted recently is to remove all the casting and type checking from ControlledGate.decompose. Once all four are complete, I'm pretty sure all of the type checks can be eliminated from that function, and things will generally be more consistent.
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.
Thank you! I added a comment.
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 cc @pavoljuhas
This fixes #7242