-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Flatten controlled-CZ and controlled-CX more consistently #7365
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7365 +/- ##
==========================================
- Coverage 98.68% 98.68% -0.01%
==========================================
Files 1112 1112
Lines 97585 97585
==========================================
- Hits 96303 96302 -1
- Misses 1282 1283 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Nice, thank you for fixing this! Before merging, please apply the patch below to address todo from #7269 (cc @codrut3).
diff --git a/cirq-core/cirq/ops/controlled_gate.py b/cirq-core/cirq/ops/controlled_gate.py
index c07b134b9..915c0805c 100644
--- a/cirq-core/cirq/ops/controlled_gate.py
+++ b/cirq-core/cirq/ops/controlled_gate.py
@@ -151,12 +151,7 @@ class ControlledGate(raw_types.Gate):
)
# 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
- ):
- return controlled_sub_gate.on(*qubits)
+ return controlled_sub_gate.on(*qubits)
if (
protocols.has_unitary(self.sub_gate)
and protocols.num_qubits(self.sub_gate) == 1
…#7365) Fixes quantumlib#7241 by absorbing the control layer of controlled-CZ into the control itself, leaving a controlled-Z, and does the equivalent for CX / X. As documented in the issue, this approach allows for more consistency in how controlled gates are represented. As stated in the linked issue, almost all existing controlled-CZ cases already work this way; the only outlier is when control_values==[0]. Eliminating that outlier allows most of the special handling to be consolidated in the base gates (Z and X), so it's already possible to see some benefit from the added consistency.
Fixes #7241 by absorbing the control layer of controlled-CZ into the control itself, leaving a controlled-Z, and does the equivalent for CX / X. As documented in the issue, this approach allows for more consistency in how controlled gates are represented.
As stated in the linked issue, almost all existing controlled-CZ cases already work this way; the only outlier is when control_values==[0]. Eliminating that outlier allows most of the special handling to be consolidated in the base gates (Z and X), so it's already possible to see some benefit from the added consistency.