forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_device_test.py
612 lines (513 loc) · 22.1 KB
/
grid_device_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# Copyright 2022 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.
from dataclasses import dataclass
from typing import Dict, List, Tuple
import unittest.mock as mock
import pytest
import cirq
import cirq_google
from cirq_google.api import v2
from cirq_google.devices import grid_device
GRID_HEIGHT = 5
@dataclass
class _DeviceInfo:
"""Dataclass for device information relevant to GridDevice tests."""
grid_qubits: List[cirq.GridQubit]
qubit_pairs: List[Tuple[cirq.GridQubit, cirq.GridQubit]]
expected_gateset: cirq.Gateset
expected_gate_durations: Dict[cirq.GateFamily, cirq.Duration]
expected_target_gatesets: Tuple[cirq.CompilationTargetGateset, ...]
def _create_device_spec_with_horizontal_couplings():
# Qubit layout:
# x -- x
# x -- x
# x -- x
# x -- x
# x -- x
grid_qubits = [cirq.GridQubit(i, j) for i in range(GRID_HEIGHT) for j in range(2)]
spec = v2.device_pb2.DeviceSpecification()
spec.valid_qubits.extend([v2.qubit_to_proto_id(q) for q in grid_qubits])
qubit_pairs = []
grid_targets = spec.valid_targets.add()
grid_targets.name = '2_qubit_targets'
grid_targets.target_ordering = v2.device_pb2.TargetSet.SYMMETRIC
for row in range(int(GRID_HEIGHT / 2)):
qubit_pairs.append((cirq.GridQubit(row, 0), cirq.GridQubit(row, 1)))
for row in range(int(GRID_HEIGHT / 2), GRID_HEIGHT):
# Flip the qubit pair order for the second half of qubits
# to verify GridDevice properly handles pair symmetry.
qubit_pairs.append((cirq.GridQubit(row, 1), cirq.GridQubit(row, 0)))
for pair in qubit_pairs:
new_target = grid_targets.targets.add()
new_target.ids.extend([v2.qubit_to_proto_id(q) for q in pair])
gate_names = [
'syc',
'sqrt_iswap',
'sqrt_iswap_inv',
'cz',
'phased_xz',
'virtual_zpow',
'physical_zpow',
'coupler_pulse',
'meas',
'wait',
]
gate_durations = [(n, i * 1000) for i, n in enumerate(gate_names)]
for gate_name, duration in sorted(gate_durations):
gate = spec.valid_gates.add()
getattr(gate, gate_name).SetInParent()
gate.gate_duration_picos = duration
expected_gateset = cirq.Gateset(
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
cirq.ops.phased_x_z_gate.PhasedXZGate,
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
),
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
),
cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
cirq.ops.measurement_gate.MeasurementGate,
cirq.ops.wait_gate.WaitGate,
)
base_duration = cirq.Duration(picos=1_000)
expected_gate_durations = {
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]): base_duration * 0,
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]): base_duration * 1,
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]): base_duration * 2,
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]): base_duration * 3,
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
cirq.GateFamily(cirq.ops.common_gates.XPowGate): base_duration * 4,
cirq.GateFamily(cirq.ops.common_gates.YPowGate): base_duration * 4,
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate): base_duration * 4,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
): base_duration
* 5,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
): base_duration
* 6,
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7,
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
}
expected_target_gatesets = (
cirq_google.GoogleCZTargetGateset(
additional_gates=[
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
),
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
),
cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
cirq.ops.wait_gate.WaitGate,
]
),
cirq_google.SycamoreTargetGateset(),
cirq.SqrtIswapTargetGateset(
additional_gates=[
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
),
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
),
cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
cirq.ops.wait_gate.WaitGate,
]
),
)
return (
_DeviceInfo(
grid_qubits,
qubit_pairs,
expected_gateset,
expected_gate_durations,
expected_target_gatesets,
),
spec,
)
def _create_device_spec_with_all_couplings():
# Qubit layout:
# x -- x
# | |
# x -- x
# | |
# x -- x
# | |
# x -- x
# | |
# x -- x
_, spec = _create_device_spec_with_horizontal_couplings()
for row in range(GRID_HEIGHT - 1):
for col in range(2):
new_target = spec.valid_targets[0].targets.add()
new_target.ids.extend(
[
v2.qubit_to_proto_id(cirq.GridQubit(row, col)),
v2.qubit_to_proto_id(cirq.GridQubit(row + 1, col)),
]
)
return spec
def _create_device_spec_with_isolated_qubits():
# Qubit layout:
# x -- x
# x -- x
# x -- x
# x -- x
# x -- x
# x x
device_info, spec = _create_device_spec_with_horizontal_couplings()
isolated_qubits = [cirq.GridQubit(GRID_HEIGHT, j) for j in range(2)]
spec.valid_qubits.extend([v2.qubit_to_proto_id(q) for q in isolated_qubits])
device_info.grid_qubits.extend(isolated_qubits)
return device_info, spec
def _create_device_spec_duplicate_qubit() -> v2.device_pb2.DeviceSpecification:
"""Creates a DeviceSpecification with a qubit name that does not conform to '<int>_<int>'."""
q_proto_id = v2.qubit_to_proto_id(cirq.GridQubit(0, 0))
spec = v2.device_pb2.DeviceSpecification()
spec.valid_qubits.extend([q_proto_id, q_proto_id])
return spec
def _create_device_spec_invalid_qubit_name() -> v2.device_pb2.DeviceSpecification:
"""Creates a DeviceSpecification with a qubit name that does not conform to '<int>_<int>'."""
q_proto_id = v2.qubit_to_proto_id(cirq.NamedQubit('q0_0'))
spec = v2.device_pb2.DeviceSpecification()
spec.valid_qubits.extend([q_proto_id])
return spec
def _create_device_spec_qubit_pair_self_loops() -> v2.device_pb2.DeviceSpecification:
"""Creates an invalid DeviceSpecification with a qubit pair ('0_0', '0_0')."""
q_proto_id = v2.qubit_to_proto_id(cirq.GridQubit(0, 0))
spec = v2.device_pb2.DeviceSpecification()
spec.valid_qubits.extend([q_proto_id])
targets = spec.valid_targets.add()
targets.name = 'test_targets'
targets.target_ordering = v2.device_pb2.TargetSet.SYMMETRIC
new_target = targets.targets.add()
new_target.ids.extend([q_proto_id, q_proto_id])
return spec
def _create_device_spec_invalid_qubit_in_qubit_pair() -> v2.device_pb2.DeviceSpecification:
"""Creates a DeviceSpecification where qubit '0_1' is in a pair but not in valid_qubits."""
q_proto_ids = [v2.qubit_to_proto_id(cirq.GridQubit(0, i)) for i in range(2)]
spec = v2.device_pb2.DeviceSpecification()
spec.valid_qubits.extend([q_proto_ids[0]])
targets = spec.valid_targets.add()
targets.name = 'test_targets'
targets.target_ordering = v2.device_pb2.TargetSet.SYMMETRIC
new_target = targets.targets.add()
new_target.ids.extend([q_proto_ids[0], q_proto_ids[1]])
return spec
def _create_device_spec_unexpected_asymmetric_target() -> v2.device_pb2.DeviceSpecification:
"""Creates a DeviceSpecification containing an ASYMMETRIC target set."""
spec = v2.device_pb2.DeviceSpecification()
targets = spec.valid_targets.add()
targets.name = 'test_targets'
targets.target_ordering = v2.device_pb2.TargetSet.ASYMMETRIC
return spec
def test_grid_device_from_proto():
device_info, spec = _create_device_spec_with_horizontal_couplings()
device = cirq_google.GridDevice.from_proto(spec)
assert len(device.metadata.qubit_set) == len(device_info.grid_qubits)
assert device.metadata.qubit_set == frozenset(device_info.grid_qubits)
assert all(frozenset(pair) in device.metadata.qubit_pairs for pair in device_info.qubit_pairs)
assert device.metadata.gateset == device_info.expected_gateset
assert device.metadata.gate_durations == device_info.expected_gate_durations
assert (
tuple(device.metadata.compilation_target_gatesets) == device_info.expected_target_gatesets
)
def test_grid_device_validate_operations_positive():
device_info, spec = _create_device_spec_with_horizontal_couplings()
device = cirq_google.GridDevice.from_proto(spec)
# Gates that can be applied to any subset of valid qubits
variadic_gates = [cirq.measure, cirq.WaitGate(cirq.Duration(nanos=1), num_qubits=2)]
for q in device_info.grid_qubits:
device.validate_operation(cirq.X(q))
device.validate_operation(cirq.measure(q))
# horizontal qubit pairs
for i in range(GRID_HEIGHT):
device.validate_operation(
cirq.CZ(device_info.grid_qubits[2 * i], device_info.grid_qubits[2 * i + 1])
)
for gate in variadic_gates:
device.validate_operation(
gate(device_info.grid_qubits[2 * i], device_info.grid_qubits[2 * i + 1])
)
@pytest.mark.parametrize(
'gate_func',
[
lambda _: cirq.measure,
lambda num_qubits: cirq.WaitGate(cirq.Duration(nanos=1), num_qubits=num_qubits),
],
)
def test_grid_device_validate_operations_variadic_gates_positive(gate_func):
device_info, spec = _create_device_spec_with_horizontal_couplings()
device = cirq_google.GridDevice.from_proto(spec)
# Single qubit operations
for q in device_info.grid_qubits:
device.validate_operation(gate_func(1)(q))
# horizontal qubit pairs (coupled)
for i in range(GRID_HEIGHT):
device.validate_operation(
gate_func(2)(device_info.grid_qubits[2 * i], device_info.grid_qubits[2 * i + 1])
)
# Variadic gates across vertical qubit pairs (uncoupled pairs) should succeed.
for i in range(GRID_HEIGHT - 1):
device.validate_operation(
gate_func(2)(device_info.grid_qubits[2 * i], device_info.grid_qubits[2 * (i + 1)])
)
device.validate_operation(
gate_func(2)(
device_info.grid_qubits[2 * i + 1], device_info.grid_qubits[2 * (i + 1) + 1]
)
)
# 3-qubit measurements
for i in range(GRID_HEIGHT - 2):
device.validate_operation(
gate_func(3)(
device_info.grid_qubits[2 * i],
device_info.grid_qubits[2 * (i + 1)],
device_info.grid_qubits[2 * (i + 2)],
)
)
device.validate_operation(
gate_func(3)(
device_info.grid_qubits[2 * i + 1],
device_info.grid_qubits[2 * (i + 1) + 1],
device_info.grid_qubits[2 * (i + 2) + 1],
)
)
# All-qubit measurement
device.validate_operation(gate_func(len(device_info.grid_qubits))(*device_info.grid_qubits))
def test_grid_device_validate_operations_negative():
device_info, spec = _create_device_spec_with_horizontal_couplings()
device = cirq_google.GridDevice.from_proto(spec)
bad_qubit = cirq.GridQubit(10, 10)
with pytest.raises(ValueError, match='Qubit not on device'):
device.validate_operation(cirq.X(bad_qubit))
# vertical qubit pair
q00, q10 = device_info.grid_qubits[0], device_info.grid_qubits[2] # (0, 0), (1, 0)
with pytest.raises(ValueError, match='Qubit pair is not valid'):
device.validate_operation(cirq.CZ(q00, q10))
with pytest.raises(ValueError, match='gate which is not supported'):
device.validate_operation(cirq.H(device_info.grid_qubits[0]))
@pytest.mark.parametrize(
'spec, error_match',
[
(_create_device_spec_duplicate_qubit(), 'Invalid DeviceSpecification: .*duplicate qubit'),
(
_create_device_spec_invalid_qubit_name(),
'Invalid DeviceSpecification: .*not in the GridQubit form',
),
(
_create_device_spec_invalid_qubit_in_qubit_pair(),
'Invalid DeviceSpecification: .*which is not in valid_qubits',
),
(
_create_device_spec_qubit_pair_self_loops(),
'Invalid DeviceSpecification: .*contains repeated qubits',
),
(
_create_device_spec_unexpected_asymmetric_target(),
'Invalid DeviceSpecification: .*cannot be ASYMMETRIC',
),
],
)
def test_grid_device_invalid_device_specification(spec, error_match):
with pytest.raises(ValueError, match=error_match):
cirq_google.GridDevice.from_proto(spec)
def test_grid_device_repr_json():
_, spec = _create_device_spec_with_horizontal_couplings()
device = cirq_google.GridDevice.from_proto(spec)
assert eval(repr(device)) == device
assert cirq.read_json(json_text=cirq.to_json(device)) == device
def test_grid_device_str_grid_qubits():
spec = _create_device_spec_with_all_couplings()
device = cirq_google.GridDevice.from_proto(spec)
assert (
str(device)
== """\
(0, 0)───(0, 1)
│ │
│ │
(1, 0)───(1, 1)
│ │
│ │
(2, 0)───(2, 1)
│ │
│ │
(3, 0)───(3, 1)
│ │
│ │
(4, 0)───(4, 1)"""
)
@pytest.mark.parametrize('cycle,func', [(False, str), (True, repr)])
def test_grid_device_repr_pretty(cycle, func):
spec = _create_device_spec_with_all_couplings()
device = cirq_google.GridDevice.from_proto(spec)
printer = mock.Mock()
device._repr_pretty_(printer, cycle)
printer.text.assert_called_once_with(func(device))
def test_device_from_device_information_equals_device_from_proto():
device_info, spec = _create_device_spec_with_isolated_qubits()
# The set of gates in gateset and gate durations are consistent with what's generated in
# _create_device_spec_with_horizontal_couplings()
gateset = cirq.Gateset(
cirq_google.SYC,
cirq.SQRT_ISWAP,
cirq.SQRT_ISWAP_INV,
cirq.CZ,
cirq.ops.phased_x_z_gate.PhasedXZGate,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
),
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
),
cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
cirq.ops.measurement_gate.MeasurementGate,
cirq.ops.wait_gate.WaitGate,
)
base_duration = cirq.Duration(picos=1_000)
gate_durations = {
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
cirq.GateFamily(cirq.CZ): base_duration * 3,
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
): base_duration
* 5,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
): base_duration
* 6,
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7,
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
}
device_from_information = cirq_google.GridDevice._from_device_information(
qubit_pairs=device_info.qubit_pairs,
gateset=gateset,
gate_durations=gate_durations,
all_qubits=device_info.grid_qubits,
)
assert device_from_information == cirq_google.GridDevice.from_proto(spec)
@pytest.mark.parametrize(
'error_match, qubit_pairs, gateset, gate_durations',
[
(
'Self loop encountered in qubit',
[(cirq.GridQubit(0, 0), cirq.GridQubit(0, 0))],
cirq.Gateset(),
None,
),
(
'Unrecognized gate',
[(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))],
cirq.Gateset(cirq.H),
None,
),
(
'Some gate_durations keys are not found in gateset',
[(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))],
cirq.Gateset(cirq.CZ),
{cirq.GateFamily(cirq.SQRT_ISWAP): cirq.Duration(picos=1_000)},
),
(
'Multiple gate families .* expected to have the same duration value',
[(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))],
cirq.Gateset(cirq.PhasedXZGate, cirq.XPowGate),
{
cirq.GateFamily(cirq.PhasedXZGate): cirq.Duration(picos=1_000),
cirq.GateFamily(cirq.XPowGate): cirq.Duration(picos=2_000),
},
),
],
)
def test_from_device_information_invalid_input(error_match, qubit_pairs, gateset, gate_durations):
with pytest.raises(ValueError, match=error_match):
grid_device.GridDevice._from_device_information(
qubit_pairs=qubit_pairs, gateset=gateset, gate_durations=gate_durations
)
def test_from_device_information_fsim_gate_family():
"""Verifies that FSimGateFamilies are recognized correctly."""
gateset = cirq.Gateset(
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
)
device = grid_device.GridDevice._from_device_information(
qubit_pairs=[(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))], gateset=gateset
)
assert gateset.gates.issubset(device.metadata.gateset.gates)
def test_from_device_information_empty():
device = grid_device.GridDevice._from_device_information(
qubit_pairs=[], gateset=cirq.Gateset(), gate_durations=None
)
assert len(device.metadata.qubit_set) == 0
assert len(device.metadata.qubit_pairs) == 0
assert device.metadata.gateset == cirq.Gateset()
assert device.metadata.gate_durations is None
def test_to_proto():
device_info, expected_spec = _create_device_spec_with_horizontal_couplings()
# The set of gates in gate_durations are consistent with what's generated in
# _create_device_spec_with_horizontal_couplings()
base_duration = cirq.Duration(picos=1_000)
gate_durations = {
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
cirq.GateFamily(cirq.CZ): base_duration * 3,
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
): base_duration
* 5,
cirq.GateFamily(
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
): base_duration
* 6,
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7,
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
}
spec = cirq_google.GridDevice._from_device_information(
qubit_pairs=device_info.qubit_pairs,
gateset=cirq.Gateset(*gate_durations.keys()),
gate_durations=gate_durations,
).to_proto()
assert cirq_google.GridDevice.from_proto(spec) == cirq_google.GridDevice.from_proto(
expected_spec
)