Skip to content

Commit 300eee8

Browse files
authored
Add clean_operations flag to cirq.two_qubit_matrix_to_sqrt_iswap_operations (quantumlib#5002)
* Add clean_operations flag to sqrt iswap decomposer * Add tests
1 parent cad82b3 commit 300eee8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
import numpy as np
2626

27-
from cirq import ops, linalg, protocols
27+
from cirq import circuits, ops, linalg, protocols
2828
from cirq.transformers.analytical_decompositions import single_qubit_decompositions
29+
from cirq.transformers.merge_single_qubit_gates import merge_single_qubit_gates_to_phxz
2930

3031
if TYPE_CHECKING:
3132
import cirq
@@ -40,6 +41,7 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
4041
use_sqrt_iswap_inv: bool = False,
4142
atol: float = 1e-8,
4243
check_preconditions: bool = True,
44+
clean_operations: bool = False,
4345
) -> Sequence['cirq.Operation']:
4446
"""Decomposes a two-qubit operation into ZPow/XPow/YPow/sqrt-iSWAP gates.
4547
@@ -69,6 +71,8 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
6971
construction.
7072
check_preconditions: If set, verifies that the input corresponds to a
7173
4x4 unitary before decomposing.
74+
clean_operations: Merges runs of single qubit gates to a single `cirq.PhasedXZGate` in
75+
the resulting operations list.
7276
7377
Returns:
7478
A list of operations implementing the matrix including at most three
@@ -92,7 +96,11 @@ def two_qubit_matrix_to_sqrt_iswap_operations(
9296
operations = _kak_decomposition_to_sqrt_iswap_operations(
9397
q0, q1, kak, required_sqrt_iswap_count, use_sqrt_iswap_inv, atol=atol
9498
)
95-
return operations
99+
return (
100+
[*merge_single_qubit_gates_to_phxz(circuits.Circuit(operations)).all_operations()]
101+
if clean_operations
102+
else operations
103+
)
96104

97105

98106
def _kak_decomposition_to_sqrt_iswap_operations(

cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ def test_decomp_optimal3(u):
382382
@pytest.mark.parametrize('u', ALL_REGION_UNITARIES)
383383
def test_all_weyl_regions(u):
384384
q0, q1 = cirq.LineQubit.range(2)
385+
ops = cirq.two_qubit_matrix_to_sqrt_iswap_operations(q0, q1, u, clean_operations=True)
386+
assert_valid_decomp(u, ops, single_qubit_gate_types=(cirq.PhasedXZGate,))
385387
ops = cirq.two_qubit_matrix_to_sqrt_iswap_operations(q0, q1, u)
386388
assert_valid_decomp(u, ops)
387389

0 commit comments

Comments
 (0)