Skip to content

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

Merged
merged 3 commits into from
May 23, 2025

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented May 21, 2025

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.

@github-actions github-actions bot added the size: M 50< lines changed <250 label May 21, 2025
Copy link

codecov bot commented May 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (f5228d7) to head (d70f785).
Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daxfohl daxfohl marked this pull request as ready for review May 21, 2025 20:27
@daxfohl daxfohl requested review from vtomole and a team as code owners May 21, 2025 20:27
@daxfohl daxfohl requested a review from fdmalone May 21, 2025 20:27
@daxfohl daxfohl changed the title Flatten controlled-CZ and controlled-CX when possible Flatten controlled-CZ and controlled-CX more consistently May 22, 2025
Copy link
Collaborator

@pavoljuhas pavoljuhas left a 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

@daxfohl daxfohl enabled auto-merge May 23, 2025 15:28
@daxfohl daxfohl added this pull request to the merge queue May 23, 2025
Merged via the queue into quantumlib:main with commit 1c0ba5b May 23, 2025
35 checks passed
@daxfohl daxfohl deleted the simplify-controlled-cz branch May 23, 2025 15:58
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eliminate multiple control layers on CX/CZ.controlled([0])
2 participants