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

Make sure all cirq gates decompose to XPow/YPow/ZPow/CZPow + Measurement #4858

Closed
tanujkhattar opened this issue Jan 19, 2022 · 3 comments · Fixed by #5107
Closed

Make sure all cirq gates decompose to XPow/YPow/ZPow/CZPow + Measurement #4858

tanujkhattar opened this issue Jan 19, 2022 · 3 comments · Fixed by #5107
Labels
area/decompose area/gates kind/feature-request Describes new functionality priority/p2 Next release should contain it triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Jan 19, 2022

As part of https://tinyurl.com/cirq-organize-decompose-rfc, we want to make sure that all cirq gates decompose to the XPow/YPow/ZPow/CZPow + Measuremet gateset.

This issue is to track all the gates where we need to explicitly add a _decompose_ method to make sure all decompositions end on the above target gateset.
We should also discuss what happens for gates where trivial decomposition is not present?

  1. MatrixGates : Should we add an analytical decomposition to the _decompose_ method of matrix gates?
  2. Same question as above for ControlledGates.

P1 - I need this no later than the next release (end of quarter)

Part of #3242

@tanujkhattar tanujkhattar added the kind/feature-request Describes new functionality label Jan 19, 2022
@vtomole vtomole changed the title Make sure all cirq gates decompose to XPow/YPow/ZPow/CZPow + Measuremet Make sure all cirq gates decompose to XPow/YPow/ZPow/CZPow + Measurement Jan 19, 2022
@tanujkhattar tanujkhattar added area/decompose area/gates priority/p2 Next release should contain it triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Jan 19, 2022
CirqBot pushed a commit that referenced this issue Jan 20, 2022
…inate in `CZPowGate`. (#4862)

Part of #4858

Decompositions are tested as part of `assert_eigengate_implements_consistent_protocols`, which is already tested for both gates.
@vtomole
Copy link
Collaborator

vtomole commented Jan 20, 2022

Should we add an analytical decomposition to the decompose method of matrix gates

How would this work? Is it possible to decompose any arbitrary MatrixGate into a target gateset?

@tanujkhattar
Copy link
Collaborator Author

@vtomole Yes. We have utilities in Cirq to decompose any arbitrary 2q Matrix Gate into the CZ gateset (see See https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/transformers/analytical_decompositions/two_qubit_to_cz.py) and there's an open issue #4022 to add compilation of any arbitrary unitary to 1/2q gates

MichaelBroughton pushed a commit to MichaelBroughton/Cirq that referenced this issue Jan 22, 2022
…inate in `CZPowGate`. (quantumlib#4862)

Part of quantumlib#4858

Decompositions are tested as part of `assert_eigengate_implements_consistent_protocols`, which is already tested for both gates.
@daxfohl
Copy link
Contributor

daxfohl commented Jan 22, 2022

I also think we should have MeasurementGate with invert_mask decompose into a series of pre- and post-measurement Xs that represent the invert mask (and the subsequent uninversion), so that "measurement" can be a uniquely defined thing.

@tanujkhattar tanujkhattar added triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Jan 26, 2022
CirqBot pushed a commit that referenced this issue Mar 16, 2022
…5083)

- Adds support for decomposing parameterized `cirq.PhasedXPowGate`s.
- Also modifies the decomposition to respect global phase, so that the decomposition is valid for controlled variants as well. 
- Part of #4858
CirqBot pushed a commit that referenced this issue Mar 16, 2022
…5085)

- Adds support for decomposing parameterized `cirq.DiagonalGate`.
- Global phase is ignored for parameterized version because `cirq.GlobalPhaseGate` doesn't yet support symbols. 
- Part of #4858
CirqBot pushed a commit that referenced this issue Mar 17, 2022
…olledOperation` to end in X/Y/Z/CZ target gateset (#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 #4858
CirqBot pushed a commit that referenced this issue Mar 18, 2022
…djacent swaps (#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of #4858

Closes #5090
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this issue 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
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this issue Apr 14, 2022
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…inate in `CZPowGate`. (quantumlib#4862)

Part of quantumlib#4858

Decompositions are tested as part of `assert_eigengate_implements_consistent_protocols`, which is already tested for both gates.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…uantumlib#5083)

- Adds support for decomposing parameterized `cirq.PhasedXPowGate`s.
- Also modifies the decomposition to respect global phase, so that the decomposition is valid for controlled variants as well. 
- Part of quantumlib#4858
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…uantumlib#5085)

- Adds support for decomposing parameterized `cirq.DiagonalGate`.
- Global phase is ignored for parameterized version because `cirq.GlobalPhaseGate` doesn't yet support symbols. 
- Part of quantumlib#4858
rht pushed a commit to rht/Cirq that referenced this issue 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
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…inate in `CZPowGate`. (quantumlib#4862)

Part of quantumlib#4858

Decompositions are tested as part of `assert_eigengate_implements_consistent_protocols`, which is already tested for both gates.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…uantumlib#5083)

- Adds support for decomposing parameterized `cirq.PhasedXPowGate`s.
- Also modifies the decomposition to respect global phase, so that the decomposition is valid for controlled variants as well. 
- Part of quantumlib#4858
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…uantumlib#5085)

- Adds support for decomposing parameterized `cirq.DiagonalGate`.
- Global phase is ignored for parameterized version because `cirq.GlobalPhaseGate` doesn't yet support symbols. 
- Part of quantumlib#4858
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue 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
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…djacent swaps (quantumlib#5093)

- Adds decomposition to `cirq.QubitPermutationGate` in terms of minimum number of adjacent swap operations on qubits. 
- Part of quantumlib#4858

Closes quantumlib#5090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/decompose area/gates kind/feature-request Describes new functionality priority/p2 Next release should contain it triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
3 participants