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 default decomposition for cirq.QubitPermutationGate in terms of adjacent swaps #5093

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions cirq-core/cirq/ops/permutation_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, Sequence, Tuple, TYPE_CHECKING
from typing import Any, Dict, Iterable, Sequence, Tuple, TYPE_CHECKING

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,25 @@ def num_qubits(self):
def _has_unitary_(self):
return True

def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
tanujkhattar marked this conversation as resolved.
Show resolved Hide resolved
n = len(qubits)
qubit_ids = [*range(n)]
is_sorted = False

def _swap_if_out_of_order(idx: int) -> Iterable['cirq.Operation']:
nonlocal is_sorted
if self._permutation[qubit_ids[idx]] > self._permutation[qubit_ids[idx + 1]]:
yield swap_gates.SWAP(qubits[idx], qubits[idx + 1])
qubit_ids[idx + 1], qubit_ids[idx] = qubit_ids[idx], qubit_ids[idx + 1]
is_sorted = False

while not is_sorted:
is_sorted = True
for i in range(0, n - 1, 2):
yield from _swap_if_out_of_order(i)
for i in range(1, n - 1, 2):
yield from _swap_if_out_of_order(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