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 clean_operations flag to cirq.two_qubit_matrix_to_sqrt_iswap_operations #5002

Merged
merged 2 commits into from
Feb 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

import numpy as np

from cirq import ops, linalg, protocols
from cirq import circuits, ops, linalg, protocols
from cirq.transformers.analytical_decompositions import single_qubit_decompositions
from cirq.transformers.merge_single_qubit_gates import merge_single_qubit_gates_to_phxz

if TYPE_CHECKING:
import cirq
Expand All @@ -40,6 +41,7 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
use_sqrt_iswap_inv: bool = False,
atol: float = 1e-8,
check_preconditions: bool = True,
clean_operations: bool = False,
) -> Sequence['cirq.Operation']:
"""Decomposes a two-qubit operation into ZPow/XPow/YPow/sqrt-iSWAP gates.

Expand Down Expand Up @@ -69,6 +71,8 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
construction.
check_preconditions: If set, verifies that the input corresponds to a
4x4 unitary before decomposing.
clean_operations: Merges runs of single qubit gates to a single `cirq.PhasedXZGate` in
the resulting operations list.

Returns:
A list of operations implementing the matrix including at most three
Expand All @@ -92,7 +96,11 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
operations = _kak_decomposition_to_sqrt_iswap_operations(
q0, q1, kak, required_sqrt_iswap_count, use_sqrt_iswap_inv, atol=atol
)
return operations
return (
[*merge_single_qubit_gates_to_phxz(circuits.Circuit(operations)).all_operations()]
if clean_operations
else operations
)


def _kak_decomposition_to_sqrt_iswap_operations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ def test_decomp_optimal3(u):
@pytest.mark.parametrize('u', ALL_REGION_UNITARIES)
def test_all_weyl_regions(u):
q0, q1 = cirq.LineQubit.range(2)
ops = cirq.two_qubit_matrix_to_sqrt_iswap_operations(q0, q1, u, clean_operations=True)
assert_valid_decomp(u, ops, single_qubit_gate_types=(cirq.PhasedXZGate,))
ops = cirq.two_qubit_matrix_to_sqrt_iswap_operations(q0, q1, u)
assert_valid_decomp(u, ops)

Expand Down