-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allows decomposition of controlled 1x1 MatrixGate #7282
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
- Resolved conflicts - Synced with latest upstream changes
# is set equal to the phase angle. | ||
# Handle global phase gates by checking if the unitary is a 1x1 matrix with a global phase | ||
unitary = protocols.unitary(self.sub_gate, default=None) | ||
if ( |
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 was thinking this branch could be merged with the above branch. The conditions protocols.has_unitary(self.sub_gate)
and self._qid_shape_() == (2,) * len(self._qid_shape_())
apply to both. The global phase inner branch would then just need an extra check for protocols.num_qubits(self.sub_gate) == 0
. The if protocols.is_parameterized(self.sub_gate) or set(shape) != {2}:
check can be removed, because parameterized gates fail has_unitary
, and set(shape) == {2}
is equivalent to the self_qid_shape_()
condition. There's also no need for the three conditions you added, since zero qubits implies a 1x1 unitary, and the abs of a unitary must square-sum to 1 by definition.
…aseGate (#7283) ## Summary Fixes #7248 This PR improves the decomposition logic for controlled gates in Cirq, ensuring that controlled 1x1 `MatrixGate` and `GlobalPhaseGate` instances decompose correctly. It also adds a targeted unit tests to verify this behavior. ## Details - Updated the [_decompose_with_context_](cci:1://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate.py:159:4-241:9) method in [cirq-core/cirq/ops/controlled_gate.py](cci:7://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate.py:0:0-0:0) to: - Correctly handle controlled gates with 1x1 unitary (global phase) by decomposing them into a `DiagonalGate` with the correct phase. - Added a parameterized test and additional explicit unit tests in [cirq-core/cirq/ops/controlled_gate_test.py](cci:7://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate_test.py:0:0-0:0) to: - Check that controlled global phase gates decompose to the correct diagonal structure for various phases and control configurations. Supersedes #7282 --------- Co-authored-by: Michael Hucka <mhucka@caltech.edu> Co-authored-by: Dax Fohl <dax.fohl@gmail.com> Co-authored-by: Pavol Juhas <juhas@google.com>
…aseGate (quantumlib#7283) ## Summary Fixes quantumlib#7248 This PR improves the decomposition logic for controlled gates in Cirq, ensuring that controlled 1x1 `MatrixGate` and `GlobalPhaseGate` instances decompose correctly. It also adds a targeted unit tests to verify this behavior. ## Details - Updated the [_decompose_with_context_](cci:1://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate.py:159:4-241:9) method in [cirq-core/cirq/ops/controlled_gate.py](cci:7://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate.py:0:0-0:0) to: - Correctly handle controlled gates with 1x1 unitary (global phase) by decomposing them into a `DiagonalGate` with the correct phase. - Added a parameterized test and additional explicit unit tests in [cirq-core/cirq/ops/controlled_gate_test.py](cci:7://file:///Users/revanthgundala/projects/Cirq/cirq-core/cirq/ops/controlled_gate_test.py:0:0-0:0) to: - Check that controlled global phase gates decompose to the correct diagonal structure for various phases and control configurations. Supersedes quantumlib#7282 --------- Co-authored-by: Michael Hucka <mhucka@caltech.edu> Co-authored-by: Dax Fohl <dax.fohl@gmail.com> Co-authored-by: Pavol Juhas <juhas@google.com>
Summary
Fixes #7248
This PR improves the decomposition logic for controlled gates in Cirq, ensuring that controlled 1x1
MatrixGate
andGlobalPhaseGate
instances decompose correctly. It also adds a targeted unit test to verify the correct decomposition behavior.Details
DiagonalGate
with the correct phase.