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

Add cirq.CZTargetGateset to replace cirq.ConvertToCZAndSingleGates and cirq.MergeInteractions #5007

Merged
merged 9 commits into from
Feb 25, 2022

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Feb 19, 2022

cc @MichaelBroughton @dstrain115

@CirqBot CirqBot added the size: L 250< lines changed <1000 label Feb 19, 2022
Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

Few high level questions before detailed review. Main one is why do we need another level of interface granularity between TwoQubitCompilationTargetGateset and CompilationTargetGateset ? It seems the gains in having this are pretty small unless I'm missing something.

Comment on lines -144 to -146
1: ───────────@───@────────────────────────────────────@───
2: ───────────────@────────────────────────────────────────
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious: how did this happen ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This happens because the newly introduced transformer is moment-preserving. Specifically, the original circuit is:

0: ───X───Y───Z───X^0.5───Y^0.5───S───X^-0.5───Y^-0.5───S^-1───H───@───X^0.25───Y^0.25───T───@───
                                                                   │                         │
1: ────────────────────────────────────────────────────────────────@───@─────────────────────@───
                                                                       │
2: ────────────────────────────────────────────────────────────────────@───────────────────────── 

In the first step, the following connected component gets merged and compiled:

      [ 0: ───X───Y───Z───X^0.5───Y^0.5───S───X^-0.5───Y^-0.5───S^-1───H───@───X^0.25───Y^0.25───T─── ]
0: ───[                                                                    │                          ]────────────────────────────────────────
      [ 1: ────────────────────────────────────────────────────────────────@───────────────────────── ]['_default_merged_k_qubit_unitaries']
      │
1: ───#2───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

as a result we have the following (moment-preserving) circuit:

0: ───PhXZ(a=0.5,x=0.5,z=0)───@───PhXZ(a=0.304,x=0.333,z=0.142)───────@───
                              │                                       │
1: ───────────────────────────@───────────────────────────────────@───@───
                                                                  │
2: ───────────────────────────────────────────────────────────────@───────

whereas in the original workflow, the CZ(1, 2) would have overlapped with PhXZ in moment-2. The final compilation step beyond this point (ConvertToPauliStringPhasors) has not yet been migrated to the new primitives, as a result in the final output, CZ(1, 2) is just shifted to the right by 1 unit but still overlaps with a single qubit gate introduced due to ConvertToPauliStringPhasors.

This is technically a breaking change but I'm not too worried because it's in contrib/.

@tanujkhattar
Copy link
Collaborator Author

Main one is why do we need another level of interface granularity between TwoQubitCompilationTargetGateset and CompilationTargetGateset ? It seems the gains in having this are pretty small unless I'm missing something.

This is to avoid replicating the logic currently implemented in decompose_to_target_gateset in every implementation, which does the following:

  1. Identify newly created connected components of 1q and 2q interactions (op_tree_old)
  2. Find analytical decomposition of the component using _decompose_two_qubit_operation (op_tree_new).
  3. Replace op_tree_old with op_tree_new iff:
    a. Either op_tree_old contains any 2q interaction which is not part of the current gateset
    b. OR all 2q interactions in op_tree_old are part of the current gateset but op_tree_new contains less number of 2q interactions (and hence is more efficient) than op_tree_old.

Note that 3b will not necessarily be the case always and therefore it's important to not replace op_tree_old with op_tree_new if op_tree_old is a more efficient implementation compared to op_tree_new. This logic currently also exists in both MergeInteractions and MergeInteractionsToSqrtISwap.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

Few nits, but after resolved we should be good to merge.


1. Expand composite gates acting on > 2 qubits using
2. Merge connected components of 1 and 2 qubit unitaries into tagged circuit operations.
3. Use `_decompose_single_qubit_operation` and `_decompose_two_qubit_operation` to figure
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we expect users of this to interface to atleast have to override _decompose_two_qubit_operation we should probably make it public ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we should keep it private because _decompose_two_qubit_operation is not supposed to be accessed from outside the class. The docstrings do specify that self._decompose_two_qubit_operation is an abstract method and should be overwritten by the users. I don't see why we need to make it public.

@tanujkhattar
Copy link
Collaborator Author

@MichaelBroughton Addressed all comments and also added serialization support for the cirq.CZTargetGateset class. PTAL.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

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

LGTM

None or NotImplemented if decomposition of `op` is unknown.
"""
return (
ops.PhasedXZGate.from_matrix(protocols.unitary(op)).on(op.qubits[0])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is PhasedXZ general enough to multiple architectures to be placed in cirq-core?

95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
…` and `cirq.MergeInteractions` (quantumlib#5007)

* Add CZTargetGateset to replace ConvertToCZAndSingleGates and MergeInteractions

* Address feedback and add serialization support for CZTargetGateset.

* Flatten new optree before computing new_2q_gate_count so it works when op tree is a generator.
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…` and `cirq.MergeInteractions` (quantumlib#5007)

* Add CZTargetGateset to replace ConvertToCZAndSingleGates and MergeInteractions

* Address feedback and add serialization support for CZTargetGateset.

* Flatten new optree before computing new_2q_gate_count so it works when op tree is a generator.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…` and `cirq.MergeInteractions` (quantumlib#5007)

* Add CZTargetGateset to replace ConvertToCZAndSingleGates and MergeInteractions

* Address feedback and add serialization support for CZTargetGateset.

* Flatten new optree before computing new_2q_gate_count so it works when op tree is a generator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants