-
Notifications
You must be signed in to change notification settings - Fork 981
/
pauli_string_optimize_test.py
84 lines (68 loc) · 2.7 KB
/
pauli_string_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
# 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, pauli_string_optimized_circuit
def test_optimize():
q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(
cirq.X(q0) ** 0.25, cirq.H(q0), cirq.CZ(q0, q1), cirq.H(q0), cirq.X(q0) ** 0.125
)
c_expected = converted_gate_set(
cirq.Circuit(
cirq.Y(q0) ** -0.5,
cirq.CZ(q0, q1),
cirq.Z(q0) ** -0.125,
cirq.X(q0) ** 0.5,
cirq.Z(q0) ** 0.5,
),
no_clifford_gates=True,
)
c_opt = pauli_string_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(
c_orig.unitary(), c_expected.unitary(), atol=1e-7
)
cirq.testing.assert_has_diagram(
c_opt,
"""
0: ───[Y]^-0.5───@───[Z]^(-1/8)───[X]^0.5───[Z]^0.5───
│
1: ──────────────@────────────────────────────────────
""",
)
def test_handles_measurement_gate():
q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(
cirq.X(q0) ** 0.25,
cirq.H(q0),
cirq.CZ(q0, q1),
cirq.H(q0),
cirq.X(q0) ** 0.125,
cirq.measure(q1, key='m1'),
cirq.measure(q0, key='m0'),
)
c_opt = pauli_string_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(c_orig.unitary(), c_opt.unitary(), atol=1e-7)
cirq.testing.assert_has_diagram(
c_opt,
"""
0: ───[Y]^-0.5───@───[Z]^(-1/8)───[X]^0.5───[Z]^0.5───M('m0')───
│
1: ──────────────@───M('m1')────────────────────────────────────
""",
)
def test_optimize_large_circuit():
q0, q1, q2 = cirq.LineQubit.range(3)
c_orig = cirq.testing.nonoptimal_toffoli_circuit(q0, q1, q2)
c_opt = pauli_string_optimized_circuit(c_orig)
cirq.testing.assert_allclose_up_to_global_phase(c_orig.unitary(), c_opt.unitary(), atol=1e-7)