-
Notifications
You must be signed in to change notification settings - Fork 980
/
clifford_optimize_test.py
124 lines (93 loc) · 3.47 KB
/
clifford_optimize_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import cirq
from cirq.contrib.paulistring import converted_gate_set, clifford_optimized_circuit
def test_optimize():
q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(
cirq.X(q1) ** 0.5,
cirq.CZ(q0, q1),
cirq.Z(q0) ** 0.25,
cirq.X(q1) ** 0.25,
cirq.CZ(q0, q1),
cirq.X(q1) ** -0.5,
)
c_expected = converted_gate_set(
cirq.Circuit(cirq.CZ(q0, q1), cirq.Z(q0) ** 0.25, cirq.X(q1) ** 0.25, cirq.CZ(q0, q1))
)
c_opt = clifford_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(c_orig.unitary(), c_opt.unitary(), atol=1e-7)
assert c_opt == c_expected
cirq.testing.assert_has_diagram(
c_opt,
"""
0: ───@───[Z]^0.25───@───
│ │
1: ───@───[X]^0.25───@───
""",
)
def test_remove_czs():
q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(cirq.CZ(q0, q1), cirq.Z(q0) ** 0.5, cirq.CZ(q0, q1))
c_expected = converted_gate_set(cirq.Circuit(cirq.Z(q0) ** 0.5))
c_opt = clifford_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(
c_orig.unitary(), c_opt.unitary(qubits_that_should_be_present=(q0, q1)), atol=1e-7
)
assert c_opt == c_expected
cirq.testing.assert_has_diagram(
c_opt,
"""
0: ───Z^0.5───
""",
)
def test_remove_staggered_czs():
q0, q1, q2 = cirq.LineQubit.range(3)
c_orig = cirq.Circuit(cirq.CZ(q0, q1), cirq.CZ(q1, q2), cirq.CZ(q0, q1))
c_expected = converted_gate_set(cirq.Circuit(cirq.CZ(q1, q2)))
c_opt = clifford_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(
c_orig.unitary(), c_opt.unitary(qubits_that_should_be_present=(q0, q1, q2)), atol=1e-7
)
assert c_opt == c_expected
cirq.testing.assert_has_diagram(
c_opt,
"""
1: ───@───
│
2: ───@───
""",
)
def test_with_measurements():
q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(cirq.X(q0), cirq.CZ(q0, q1), cirq.measure(q0, q1, key='m'))
c_expected = converted_gate_set(
cirq.Circuit(cirq.CZ(q0, q1), cirq.X(q0), cirq.Z(q1), cirq.measure(q0, q1, key='m'))
)
c_opt = clifford_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(c_orig.unitary(), c_opt.unitary(), atol=1e-7)
assert c_opt == c_expected
cirq.testing.assert_has_diagram(
c_opt,
"""
0: ───@───X───M('m')───
│ │
1: ───@───Z───M────────
""",
)
def test_optimize_large_circuit():
q0, q1, q2 = cirq.LineQubit.range(3)
c_orig = cirq.testing.nonoptimal_toffoli_circuit(q0, q1, q2)
c_opt = clifford_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(c_orig.unitary(), c_opt.unitary(), atol=1e-7)