Skip to content

Conversation

@tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented May 26, 2022

cirq.TwoQubitCompilationTargetGateset currently implements a logic to compare the analytical decomposition of a merged 2q connected component (new_optree) with the one which already exists in the circuit (old_optree) and switch iff:

  • Any gate in in old_optree is not part of the target gateset
  • Number of 2q gates in new_optree is less than those in old_optree

This PR changes the switch condition to:

  • Any 2q gate in in old_optree is not part of the target gateset # Any ==> Any 2q
  • Number of 2q gates in new_optree is less than those in old_optree

This is useful because if the old merged connected component (i.e. old_optree) is optimal in terms of 2q gates but contains single qubit gates outside of the target gateset; we should only replace the single qubit gates and not the entire decomposition with a more in-efficient decomposition.

See the newly added test for a dummy example.

cc @verult ; Part of #5254

… switch only when it's optimal in terms of 2q gate counts
@tanujkhattar tanujkhattar requested review from a team, cduck and vtomole as code owners May 26, 2022 00:45
@tanujkhattar tanujkhattar requested a review from maffoo May 26, 2022 00:45
@CirqBot CirqBot added the size: S 10< lines changed <50 label May 26, 2022
Comment on lines +173 to +176
any(
protocols.num_qubits(op) == 2 and op not in self
for op in ops.flatten_to_ops(old_optree)
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we do this part of the check before constructing new_optree so we don't waste time on it if it's not needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We will have to construct the new_optree irrespective of whether this check returns a True or False.

We will not switch to the new_optree only if both the checks are false (i.e. all 2q operations in old_optree are in self and old_2q_gate_count <= new_2q_gate_count.

return new_optree if switch_to_new else old_optree
if switch_to_new:
return new_optree
mapped_old_optree: List['cirq.OP_TREE'] = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is different from the old behavior: if all old ops (1q or 2q) are in the target gateset and new_2q_gate_count >= old_2q_gate_count, this previously would have returned the old optree. Is this intended?

(If gateset behavior would prevent this case from occurring, a comment explaining this would be helpful)

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 previously would have returned the old optree.

The behavior is unchanged, i.e., if all old ops (1q or 2q) are in the target gateset and new_2q_gate_count >= old_2q_gate_count; it would have previously returned old_optree and it would still return old_optree.

Notice that the mapped_old_optree will be same as old_optree if all operations are in self (the first if condition in the loop below).

@tanujkhattar tanujkhattar merged commit 1bbd2c4 into master May 26, 2022
augustehirth pushed a commit to augustehirth/Cirq that referenced this pull request May 27, 2022
… switch only when it's optimal in terms of 2q gate counts (quantumlib#5405)
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
… switch only when it's optimal in terms of 2q gate counts (quantumlib#5405)
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
… switch only when it's optimal in terms of 2q gate counts (quantumlib#5405)
@pavoljuhas pavoljuhas deleted the fix_2q_compilation_target_gateset branch January 22, 2025 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S 10< lines changed <50

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants