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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 (e9a592d) to head (cb4ca25).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7365   +/-   ##
=======================================
  Coverage   98.68%   98.68%           
=======================================
  Files        1112     1112           
  Lines       97599    97600    +1     
=======================================
+ Hits        96315    96318    +3     
+ Misses       1284     1282    -2     

☔ 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

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