-
Notifications
You must be signed in to change notification settings - Fork 985
/
coupler_pulse_test.py
221 lines (202 loc) · 6.72 KB
/
coupler_pulse_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2021 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 pytest
import sympy
import cirq
import cirq_google.experimental.ops.coupler_pulse as coupler_pulse
def test_consistent_protocols():
gate = coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10), coupling_mhz=25.0, rise_time=cirq.Duration(nanos=18)
)
cirq.testing.assert_implements_consistent_protocols(
gate,
setup_code='import cirq\nimport numpy as np\nimport sympy\nimport cirq_google',
qubit_count=2,
ignore_decompose_to_default_gateset=True,
)
assert gate.num_qubits() == 2
def test_equality():
eq = cirq.testing.EqualsTester()
eq.add_equality_group(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10),
coupling_mhz=25.0,
rise_time=cirq.Duration(nanos=18),
padding_time=cirq.Duration(nanos=4),
),
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10),
coupling_mhz=25.0,
rise_time=cirq.Duration(nanos=18),
padding_time=cirq.Duration(nanos=4),
),
)
eq.add_equality_group(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=12),
coupling_mhz=25.0,
rise_time=cirq.Duration(nanos=18),
padding_time=cirq.Duration(nanos=4),
)
)
eq.add_equality_group(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10),
coupling_mhz=26.0,
rise_time=cirq.Duration(nanos=18),
padding_time=cirq.Duration(nanos=4),
)
)
eq.add_equality_group(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10),
coupling_mhz=25.0,
rise_time=cirq.Duration(nanos=28),
padding_time=cirq.Duration(nanos=4),
)
)
eq.add_equality_group(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10),
coupling_mhz=25.0,
rise_time=cirq.Duration(nanos=18),
padding_time=cirq.Duration(nanos=40),
)
)
def test_coupler_pulse_str_repr():
gate = coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10), coupling_mhz=25.0, rise_time=cirq.Duration(nanos=18)
)
assert (
str(gate) == 'CouplerPulse(hold_time=10 ns, coupling_mhz=25.0, '
'rise_time=18 ns, padding_time=2500.0 ps, q0_detune_mhz=0.0, q1_detune_mhz=0.0)'
)
assert (
repr(gate) == 'cirq_google.experimental.ops.coupler_pulse.CouplerPulse('
'hold_time=cirq.Duration(nanos=10), '
'coupling_mhz=25.0, '
'rise_time=cirq.Duration(nanos=18), '
'padding_time=cirq.Duration(picos=2500.0), '
'q0_detune_mhz=0.0, '
'q1_detune_mhz=0.0)'
)
def test_coupler_pulse_json_deserialization_defaults_on_missing_fields():
gate = coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10), coupling_mhz=25.0, rise_time=cirq.Duration(nanos=18)
)
json_text = """{
"cirq_type": "CouplerPulse",
"hold_time": {
"cirq_type": "Duration",
"picos": 10000
},
"coupling_mhz": 25.0,
"rise_time": {
"cirq_type": "Duration",
"picos": 18000
},
"padding_time": {
"cirq_type": "Duration",
"picos": 2500.0
}
}"""
deserialized = cirq.read_json(json_text=json_text)
assert deserialized == gate
assert deserialized.q0_detune_mhz == 0.0
def test_coupler_pulse_circuit_diagram():
a, b = cirq.LineQubit.range(2)
gate = coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=10), coupling_mhz=25.0, rise_time=cirq.Duration(nanos=18)
)
circuit = cirq.Circuit(gate(a, b))
cirq.testing.assert_has_diagram(
circuit,
r"""
0: ───/‾‾(10 ns@25.0MHz)‾‾\───
│
1: ───/‾‾(10 ns@25.0MHz)‾‾\───
""",
)
@pytest.mark.parametrize(
'gate, resolver, expected',
[
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
{'t_ns': 50},
coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10),
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
{'g': 10},
coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10),
),
],
)
def test_coupler_pulse_resolution(gate, resolver, expected):
assert cirq.resolve_parameters(gate, resolver) == expected
@pytest.mark.parametrize(
'gate, param_names',
[
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
{'t_ns'},
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
{'g'},
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=sympy.Symbol('g')
),
{'g', 't_ns'},
),
],
)
def test_coupler_pulse_parameter_names(gate, param_names):
assert cirq.parameter_names(gate) == param_names
@pytest.mark.parametrize(
'gate, is_parameterized',
[
(coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10), False),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
True,
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
True,
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=sympy.Symbol('g')
),
True,
),
],
)
def test_coupler_pulse_is_parameterized(gate, is_parameterized):
assert cirq.is_parameterized(gate) == is_parameterized