-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,17 @@ def _decompose_with_context_( | |
self, qubits: Tuple['cirq.Qid', ...], context: Optional['cirq.DecompositionContext'] = None | ||
) -> Union[None, NotImplementedType, 'cirq.OP_TREE']: | ||
control_qubits = list(qubits[: self.num_controls()]) | ||
controlled_sub_gate = self.sub_gate.controlled( | ||
self.num_controls(), self.control_values, self.control_qid_shape | ||
) | ||
# Prefer the subgate controlled version if available | ||
if self != controlled_sub_gate: | ||
# Prevent 2-cycle from appearing in the recursive decomposition | ||
# TODO: Remove after #7241 is resolved | ||
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 commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, seems like if we make the end of 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 I think any of the following approaches would work:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! I added a comment. |
||
): | ||
return controlled_sub_gate.on(*qubits) | ||
if ( | ||
protocols.has_unitary(self.sub_gate) | ||
and protocols.num_qubits(self.sub_gate) == 1 | ||
|
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:
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;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.