Skip to content

Add default decomposition for cirq.QubitPermutationGate in terms of adjacent swaps #5093

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

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cirq-core/cirq/ops/permutation_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from cirq import protocols, value
from cirq._compat import deprecated
from cirq.ops import raw_types
from cirq.ops import raw_types, swap_gates

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -74,6 +74,15 @@ def num_qubits(self):
def _has_unitary_(self):
return True

def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
qubit_ids = [*range(len(qubits))]
for i in range(len(qubits)):
q_i = qubit_ids.index(self._permutation.index(i))
for j in range(q_i, i, -1):
yield swap_gates.SWAP(qubits[j], qubits[j - 1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Limiting SWAPs to adjacent-index qubits presupposes a linear qubit layout; on a 2D grid or more exotic layout, this will not work:

q = GridQubit.rect(2, 2)
# q[1] and q[2] are not adjacent ((0, 1) and (1, 0))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that the decomposition here is not intended to be architecture aware. The goal here is to simply write some decomposition in terms of other "simpler" gates which eventually ends up decomposing into the native gateset.

For device specific routing, we would need more sophisticated transformers which take care of device topology, but that is out of scope of this PR. The choice to use adjacent index swaps which can be useful for a linear qubit layout is an arbitrary one, mostly because running circuits on a chain of qubits is a common use case and a low hanging fruit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is meant to be architecture-agnostic, why don't we swap qubits directly (ignoring adjacency)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As I said, it's a choice we need to make. Both the approaches would be valid for the scope of this PR.

If a user is relying on the default decomposition of Cirq for compiling their circuit, it means they have already given up fine grained control. The reason I want to stick to this approach is that it's a low hanging fruit which targets a common use case of running circuits on a chain of qubits. It's not going to be enough for every scenario, but still slightly better than assuming all to all connectivity.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not going to be enough for every scenario, but still slightly better than assuming all to all connectivity.

I'm not convinced that linear-swap is better than all-to-all, or even equally valid. Compared to linear-swap, all-to-all is:

  • cheaper to run on simulators (which use default decomposition)
  • easier to convert to efficient circuits in device-specific layout
  • cheaper to construct here (low priority, but worth considering)

I'd support adding a separate method for converting to linear-swap, but the default should be all-to-all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cheaper to run on simulators (which use default decomposition)

The class already has a _apply_unitary_ defined, which should be used by simulators over the default decomposition. Is that not the case?

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 true for Cirq-internal simulators, but not in general. For example, qsim attempts to decompose gates to a specific subset that it natively supports, falling back on conversion to unitary only if no decomposition is defined (code).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened #5097 to track the optimization.

qubit_ids[j], qubit_ids[j - 1] = qubit_ids[j - 1], qubit_ids[j]
assert self._permutation[qubit_ids[i]] == i

def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs'):
# Compute the permutation index list.
permuted_axes = list(range(len(args.target_tensor.shape)))
Expand Down
11 changes: 9 additions & 2 deletions cirq-core/cirq/ops/permutation_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pytest

import cirq
import numpy as np
from cirq.ops import QubitPermutationGate


Expand All @@ -30,8 +31,12 @@ def test_permutation_gate_repr():
cirq.testing.assert_equivalent_repr(QubitPermutationGate([0, 1]))


def test_permutation_gate_consistent_protocols():
gate = QubitPermutationGate([1, 0, 2, 3])
rs = np.random.RandomState(seed=1234)


@pytest.mark.parametrize('permutation', [rs.permutation(i) for i in range(3, 7)])
def test_permutation_gate_consistent_protocols(permutation):
gate = QubitPermutationGate(list(permutation))
cirq.testing.assert_implements_consistent_protocols(gate)


Expand Down Expand Up @@ -98,6 +103,8 @@ def test_permutation_gate_maps(maps, permutation):
permutationOp = cirq.QubitPermutationGate(permutation).on(*qs)
circuit = cirq.Circuit(permutationOp)
cirq.testing.assert_equivalent_computational_basis_map(maps, circuit)
circuit = cirq.Circuit(cirq.I.on_each(*qs), cirq.decompose(permutationOp))
cirq.testing.assert_equivalent_computational_basis_map(maps, circuit)


def test_setters_deprecated():
Expand Down