3
3
import cirq
4
4
5
5
6
- def assert_optimizes (before : cirq .Circuit , expected : cirq .Circuit ):
6
+ def assert_optimizes (before : cirq .Circuit , expected : cirq .Circuit , ** kwargs ):
7
7
actual = cirq .Circuit (before )
8
- opt = cirq .MergeInteractionsToSqrtIswap ()
8
+ opt = cirq .MergeInteractionsToSqrtIswap (** kwargs )
9
9
opt .optimize_circuit (actual )
10
10
11
11
# Ignore differences that would be caught by follow-up optimizations.
@@ -23,15 +23,23 @@ def assert_optimizes(before: cirq.Circuit, expected: cirq.Circuit):
23
23
assert actual == expected , f'ACTUAL { actual } : EXPECTED { expected } '
24
24
25
25
26
- def assert_optimization_not_broken (circuit ):
26
+ def assert_optimization_not_broken (circuit : cirq . Circuit ):
27
27
"""Check that the unitary matrix for the input circuit is the same (up to
28
28
global phase and rounding error) as the unitary matrix of the optimized
29
29
circuit."""
30
30
u_before = circuit .unitary ()
31
- cirq .MergeInteractions ().optimize_circuit (circuit )
32
- u_after = circuit .unitary ()
31
+ c_sqrt_iswap = circuit .copy ()
32
+ cirq .MergeInteractionsToSqrtIswap ().optimize_circuit (c_sqrt_iswap )
33
+ u_after = c_sqrt_iswap .unitary ()
33
34
34
- cirq .testing .assert_allclose_up_to_global_phase (u_before , u_after , atol = 1e-8 )
35
+ cirq .testing .assert_allclose_up_to_global_phase (u_before , u_after , atol = 2e-8 )
36
+
37
+ # Also test optimization with SQRT_ISWAP_INV
38
+ c_sqrt_iswap_inv = circuit .copy ()
39
+ cirq .MergeInteractionsToSqrtIswap (use_sqrt_iswap_inv = True ).optimize_circuit (c_sqrt_iswap_inv )
40
+ u_after2 = c_sqrt_iswap_inv .unitary ()
41
+
42
+ cirq .testing .assert_allclose_up_to_global_phase (u_before , u_after2 , atol = 2e-8 )
35
43
36
44
37
45
def test_clears_paired_cnot ():
@@ -47,6 +55,57 @@ def test_clears_paired_cnot():
47
55
)
48
56
49
57
58
+ def test_simplifies_sqrt_iswap ():
59
+ a , b = cirq .LineQubit .range (2 )
60
+ assert_optimizes (
61
+ before = cirq .Circuit (
62
+ [
63
+ # SQRT_ISWAP**8 == Identity
64
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
65
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
66
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
67
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
68
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
69
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
70
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
71
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
72
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
73
+ ]
74
+ ),
75
+ expected = cirq .Circuit (
76
+ [
77
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
78
+ ]
79
+ ),
80
+ )
81
+
82
+
83
+ def test_simplifies_sqrt_iswap_inv ():
84
+ a , b = cirq .LineQubit .range (2 )
85
+ assert_optimizes (
86
+ use_sqrt_iswap_inv = True ,
87
+ before = cirq .Circuit (
88
+ [
89
+ # SQRT_ISWAP**8 == Identity
90
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
91
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
92
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
93
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
94
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
95
+ cirq .Moment ([cirq .SQRT_ISWAP_INV (a , b )]),
96
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
97
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
98
+ cirq .Moment ([cirq .SQRT_ISWAP (a , b )]),
99
+ ]
100
+ ),
101
+ expected = cirq .Circuit (
102
+ [
103
+ cirq .Moment ([cirq .SQRT_ISWAP_INV (a , b )]),
104
+ ]
105
+ ),
106
+ )
107
+
108
+
50
109
def test_cnots_separated_by_single_gates_correct ():
51
110
a , b = cirq .LineQubit .range (2 )
52
111
assert_optimization_not_broken (
0 commit comments