Skip to content
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

Extend default decomposition of cirq.ControlledGate and cirq.ControlledOperation to end in X/Y/Z/CZ target gateset #5091

Merged

Conversation

tanujkhattar
Copy link
Collaborator

When decomposed, controlled gates and operations simply fall back on the decomposition of underlying sub_gate / sub_operation and return apply appropriate controls to each decomposed operation.

If we can ensure that all underlying gates / operations decompose to X/Y/Z/CZ target gateset, then their controlled versions will decompose to:

  • Multi controlled single qubit rotations (corresponding to (X/Y/Z).controlled_by(...)) OR
  • Multi controlled CZs, which is also equivalent to a multi controlled single qubit rotation (Z.controlled_by(...))

In Cirq, we have an analytical method to decompose a multi controlled rotation into X/Y/Z/CZ - cirq.decompose_multi_controlled_rotation, which is now used in the _decompose_ method of controlled gates.

However, there are many corner cases and limitations of the current approach, which are dealt appropriately in this PR to enable a "best-effort" decomposition of controlled gates to the cirq target gateset. Some of the limitations are:

  • If decomposition of sub_gate / sub_operation ignores global phase, then the controlled operation cannot directly rely on decomposing the sub operation. An explicit check is added to not fallback on sub_gate if sub_gate is a MatrixGate.
  • decompose_multi_controlled_rotation works only for qubits (doesn't work for qudits) and when all control_values are 1. Appropriate logic is added to extend its functionality to handle control_values which are 0 or (0, 1).
  • We have explicit types for a few important controlled gates, like CCZ, CZ, CCX, CX etc. in cirq. Appropriate type conversion logic is added to smartly infer the types of equivalent gates (eg: Controlled(sub_gate=CZ) should be inferred as CCZ) such that their decompositions can be used for decomposing the controlled gates.

This is definitely the most tricky one to get right and I've added appropriate tests to cover the different cases.

Part of #4858

…edOperation to end in X/Y/Z/CZ target gateset
@tanujkhattar tanujkhattar requested review from a team, vtomole and cduck as code owners March 17, 2022 02:52
@tanujkhattar tanujkhattar requested a review from viathor March 17, 2022 02:52
@CirqBot CirqBot added the size: M 50< lines changed <250 label Mar 17, 2022
@tanujkhattar tanujkhattar changed the title Extend default decomposition of cirq.ControlledGate and cirq.ControlledOperation to end in X/Y/Z/CZ target gateset Extend default decomposition of cirq.ControlledGate and cirq.ControlledOperation to end in X/Y/Z/CZ target gateset Mar 17, 2022
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass - I feel like I may need to stare at this some more to be confident that I understand the changes being made.

cirq-core/cirq/ops/controlled_operation_test.py Outdated Show resolved Hide resolved
@tanujkhattar tanujkhattar added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 17, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 17, 2022
@CirqBot CirqBot merged commit 1f47082 into quantumlib:master Mar 17, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Mar 17, 2022
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Apr 14, 2022
…olledOperation` to end in X/Y/Z/CZ target gateset (quantumlib#5091)

When decomposed, controlled gates and operations simply fall back on the decomposition of underlying sub_gate / sub_operation and return apply appropriate controls to each decomposed operation. 

If we can ensure that all underlying gates / operations decompose to X/Y/Z/CZ target gateset, then their controlled versions will decompose to:
 - Multi controlled single qubit rotations (corresponding to (X/Y/Z).controlled_by(...)) OR
 - Multi controlled CZs, which is also equivalent to a multi controlled single qubit rotation (Z.controlled_by(...))

In Cirq, we have an analytical method to decompose a multi controlled rotation into X/Y/Z/CZ - `cirq.decompose_multi_controlled_rotation`, which is now used in the `_decompose_` method of controlled gates. 

However, there are many corner cases and limitations of the current approach, which are dealt appropriately in this PR to enable a "best-effort" decomposition of controlled gates to the cirq target gateset. Some of the limitations are:
 - If decomposition of sub_gate / sub_operation ignores global phase, then the controlled operation cannot directly rely on decomposing the sub operation. An explicit check is added to not fallback on sub_gate if sub_gate is a  MatrixGate.
 - `decompose_multi_controlled_rotation` works only for qubits (doesn't work for qudits) and when all control_values are 1. Appropriate logic is added to extend its functionality to handle control_values which are 0 or (0, 1). 
 - We have explicit types for a few important controlled gates, like `CCZ`, `CZ`, `CCX`, `CX` etc. in cirq. Appropriate type conversion logic is added to smartly infer the types of equivalent gates (eg: Controlled(sub_gate=CZ) should be inferred as CCZ) such that their decompositions can be used for decomposing the controlled gates. 


This is definitely the most tricky one to get right and I've added appropriate tests to cover the different cases. 


Part of quantumlib#4858
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…olledOperation` to end in X/Y/Z/CZ target gateset (quantumlib#5091)

When decomposed, controlled gates and operations simply fall back on the decomposition of underlying sub_gate / sub_operation and return apply appropriate controls to each decomposed operation. 

If we can ensure that all underlying gates / operations decompose to X/Y/Z/CZ target gateset, then their controlled versions will decompose to:
 - Multi controlled single qubit rotations (corresponding to (X/Y/Z).controlled_by(...)) OR
 - Multi controlled CZs, which is also equivalent to a multi controlled single qubit rotation (Z.controlled_by(...))

In Cirq, we have an analytical method to decompose a multi controlled rotation into X/Y/Z/CZ - `cirq.decompose_multi_controlled_rotation`, which is now used in the `_decompose_` method of controlled gates. 

However, there are many corner cases and limitations of the current approach, which are dealt appropriately in this PR to enable a "best-effort" decomposition of controlled gates to the cirq target gateset. Some of the limitations are:
 - If decomposition of sub_gate / sub_operation ignores global phase, then the controlled operation cannot directly rely on decomposing the sub operation. An explicit check is added to not fallback on sub_gate if sub_gate is a  MatrixGate.
 - `decompose_multi_controlled_rotation` works only for qubits (doesn't work for qudits) and when all control_values are 1. Appropriate logic is added to extend its functionality to handle control_values which are 0 or (0, 1). 
 - We have explicit types for a few important controlled gates, like `CCZ`, `CZ`, `CCX`, `CX` etc. in cirq. Appropriate type conversion logic is added to smartly infer the types of equivalent gates (eg: Controlled(sub_gate=CZ) should be inferred as CCZ) such that their decompositions can be used for decomposing the controlled gates. 


This is definitely the most tricky one to get right and I've added appropriate tests to cover the different cases. 


Part of quantumlib#4858
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…olledOperation` to end in X/Y/Z/CZ target gateset (quantumlib#5091)

When decomposed, controlled gates and operations simply fall back on the decomposition of underlying sub_gate / sub_operation and return apply appropriate controls to each decomposed operation. 

If we can ensure that all underlying gates / operations decompose to X/Y/Z/CZ target gateset, then their controlled versions will decompose to:
 - Multi controlled single qubit rotations (corresponding to (X/Y/Z).controlled_by(...)) OR
 - Multi controlled CZs, which is also equivalent to a multi controlled single qubit rotation (Z.controlled_by(...))

In Cirq, we have an analytical method to decompose a multi controlled rotation into X/Y/Z/CZ - `cirq.decompose_multi_controlled_rotation`, which is now used in the `_decompose_` method of controlled gates. 

However, there are many corner cases and limitations of the current approach, which are dealt appropriately in this PR to enable a "best-effort" decomposition of controlled gates to the cirq target gateset. Some of the limitations are:
 - If decomposition of sub_gate / sub_operation ignores global phase, then the controlled operation cannot directly rely on decomposing the sub operation. An explicit check is added to not fallback on sub_gate if sub_gate is a  MatrixGate.
 - `decompose_multi_controlled_rotation` works only for qubits (doesn't work for qudits) and when all control_values are 1. Appropriate logic is added to extend its functionality to handle control_values which are 0 or (0, 1). 
 - We have explicit types for a few important controlled gates, like `CCZ`, `CZ`, `CCX`, `CX` etc. in cirq. Appropriate type conversion logic is added to smartly infer the types of equivalent gates (eg: Controlled(sub_gate=CZ) should be inferred as CCZ) such that their decompositions can be used for decomposing the controlled gates. 


This is definitely the most tricky one to get right and I've added appropriate tests to cover the different cases. 


Part of quantumlib#4858
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.

4 participants