@@ -33,14 +33,20 @@ def _create_device_spec_with_horizontal_couplings():
33
33
# x -- x
34
34
35
35
grid_qubits = [cirq .GridQubit (i , j ) for i in range (GRID_HEIGHT ) for j in range (2 )]
36
+
36
37
spec = v2 .device_pb2 .DeviceSpecification ()
37
38
spec .valid_qubits .extend ([v2 .qubit_to_proto_id (q ) for q in grid_qubits ])
38
39
grid_targets = spec .valid_targets .add ()
39
40
grid_targets .name = '2_qubit_targets'
40
41
grid_targets .target_ordering = v2 .device_pb2 .TargetSet .SYMMETRIC
41
- for row in range (GRID_HEIGHT ):
42
+ for row in range (int ( GRID_HEIGHT / 2 ) ):
42
43
new_target = grid_targets .targets .add ()
43
44
new_target .ids .extend ([v2 .qubit_to_proto_id (cirq .GridQubit (row , j )) for j in range (2 )])
45
+ for row in range (int (GRID_HEIGHT / 2 ), GRID_HEIGHT ):
46
+ # Flip the qubit pair order for the second half of qubits
47
+ # to verify GridDevice properly handles pair symmetry.
48
+ new_target = grid_targets .targets .add ()
49
+ new_target .ids .extend ([v2 .qubit_to_proto_id (cirq .GridQubit (row , 1 - j )) for j in range (2 )])
44
50
gate = spec .valid_gates .add ()
45
51
gate .syc .SetInParent ()
46
52
gate .gate_duration_picos = 12000
@@ -74,7 +80,7 @@ def _create_device_spec_with_all_couplings():
74
80
return grid_qubits , spec
75
81
76
82
77
- def _create_device_spec_with_qubit_pair_self_loops () -> v2 .device_pb2 .DeviceSpecification :
83
+ def _create_device_spec_qubit_pair_self_loops () -> v2 .device_pb2 .DeviceSpecification :
78
84
q_proto_id = v2 .qubit_to_proto_id (cirq .NamedQubit ('q' ))
79
85
80
86
spec = v2 .device_pb2 .DeviceSpecification ()
@@ -88,7 +94,7 @@ def _create_device_spec_with_qubit_pair_self_loops() -> v2.device_pb2.DeviceSpec
88
94
return spec
89
95
90
96
91
- def _create_device_spec_with_invalid_qubit_in_qubit_pair () -> v2 .device_pb2 .DeviceSpecification :
97
+ def _create_device_spec_invalid_qubit_in_qubit_pair () -> v2 .device_pb2 .DeviceSpecification :
92
98
q_proto_ids = [v2 .qubit_to_proto_id (cirq .GridQubit (0 , i )) for i in range (2 )]
93
99
94
100
spec = v2 .device_pb2 .DeviceSpecification ()
@@ -102,22 +108,36 @@ def _create_device_spec_with_invalid_qubit_in_qubit_pair() -> v2.device_pb2.Devi
102
108
return spec
103
109
104
110
105
- def test_google_device_from_proto_and_validation ():
111
+ def _create_device_spec_invalid_subset_permutation_target () -> v2 .device_pb2 .DeviceSpecification :
112
+ q_proto_ids = [v2 .qubit_to_proto_id (cirq .GridQubit (0 , i )) for i in range (2 )]
113
+
114
+ spec = v2 .device_pb2 .DeviceSpecification ()
115
+ spec .valid_qubits .extend (q_proto_ids )
116
+ targets = spec .valid_targets .add ()
117
+ targets .name = 'test_targets'
118
+ targets .target_ordering = v2 .device_pb2 .TargetSet .SUBSET_PERMUTATION
119
+ new_target = targets .targets .add ()
120
+ new_target .ids .extend (q_proto_ids ) # should only have 1 qubit instead
121
+
122
+ return spec
123
+
124
+
125
+ def test_grid_device_from_proto_and_validation ():
106
126
grid_qubits , spec = _create_device_spec_with_horizontal_couplings ()
107
127
108
- device = cirq_google .GoogleDevice .from_proto (spec )
128
+ device = cirq_google .GridDevice .from_proto (spec )
109
129
110
130
assert len (device .metadata .qubit_set ) == len (grid_qubits )
111
131
assert device .metadata .qubit_set == frozenset (grid_qubits )
112
132
assert all (
113
- ( cirq .GridQubit (row , 0 ), cirq .GridQubit (row , 1 )) in device .metadata .qubit_pairs
133
+ frozenset (( cirq .GridQubit (row , 0 ), cirq .GridQubit (row , 1 ) )) in device .metadata .qubit_pairs
114
134
for row in range (GRID_HEIGHT )
115
135
)
116
136
117
137
118
- def test_google_device_validate_operations_positive ():
138
+ def test_grid_device_validate_operations_positive ():
119
139
grid_qubits , spec = _create_device_spec_with_horizontal_couplings ()
120
- device = cirq_google .GoogleDevice .from_proto (spec )
140
+ device = cirq_google .GridDevice .from_proto (spec )
121
141
122
142
for q in grid_qubits :
123
143
device .validate_operation (cirq .X (q ))
@@ -129,9 +149,9 @@ def test_google_device_validate_operations_positive():
129
149
# TODO(#5050) verify validate_operations gateset support
130
150
131
151
132
- def test_google_device_validate_operations_negative ():
152
+ def test_grid_device_validate_operations_negative ():
133
153
grid_qubits , spec = _create_device_spec_with_horizontal_couplings ()
134
- device = cirq_google .GoogleDevice .from_proto (spec )
154
+ device = cirq_google .GridDevice .from_proto (spec )
135
155
136
156
q = cirq .GridQubit (10 , 10 )
137
157
with pytest .raises (ValueError , match = 'Qubit not on device' ):
@@ -145,31 +165,32 @@ def test_google_device_validate_operations_negative():
145
165
# TODO(#5050) verify validate_operations gateset errors
146
166
147
167
148
- @pytest .mark .parametrize (
149
- 'spec' ,
150
- [
151
- # TODO(#5050) implement once gateset support is implemented
152
- # _create_device_spec_with_missing_gate_durations(),
153
- _create_device_spec_with_qubit_pair_self_loops (),
154
- _create_device_spec_with_invalid_qubit_in_qubit_pair (),
155
- ],
156
- )
157
- def test_google_device_invalid_device_spec (spec ):
158
- with pytest .raises (ValueError , match = 'DeviceSpecification is invalid' ):
159
- cirq_google .GoogleDevice .from_proto (spec )
168
+ def test_grid_device_invalid_qubit_in_qubit_pair ():
169
+ with pytest .raises (ValueError , match = 'which is not in valid_qubits' ):
170
+ cirq_google .GridDevice .from_proto (_create_device_spec_invalid_qubit_in_qubit_pair ())
171
+
172
+
173
+ def test_grid_device_invalid_target_self_loops ():
174
+ with pytest .raises (ValueError , match = 'contains repeated qubits' ):
175
+ cirq_google .GridDevice .from_proto (_create_device_spec_qubit_pair_self_loops ())
176
+
177
+
178
+ def test_grid_device_invalid_subset_permutation_target ():
179
+ with pytest .raises (ValueError , match = 'does not have exactly 1 qubit' ):
180
+ cirq_google .GridDevice .from_proto (_create_device_spec_invalid_subset_permutation_target ())
160
181
161
182
162
- def test_google_device_repr_json ():
183
+ def test_grid_device_repr_json ():
163
184
_ , spec = _create_device_spec_with_horizontal_couplings ()
164
- device = cirq_google .GoogleDevice .from_proto (spec )
185
+ device = cirq_google .GridDevice .from_proto (spec )
165
186
166
187
assert eval (repr (device )) == device
167
188
assert cirq .read_json (json_text = cirq .to_json (device )) == device
168
189
169
190
170
- def test_google_device_str_grid_qubits ():
191
+ def test_grid_device_str_grid_qubits ():
171
192
_ , spec = _create_device_spec_with_all_couplings ()
172
- device = cirq_google .GoogleDevice .from_proto (spec )
193
+ device = cirq_google .GridDevice .from_proto (spec )
173
194
174
195
assert (
175
196
str (device )
@@ -191,17 +212,17 @@ def test_google_device_str_grid_qubits():
191
212
192
213
193
214
@pytest .mark .parametrize ('cycle,func' , [(False , str ), (True , repr )])
194
- def test_google_device_repr_pretty (cycle , func ):
215
+ def test_grid_device_repr_pretty (cycle , func ):
195
216
_ , spec = _create_device_spec_with_all_couplings ()
196
- device = cirq_google .GoogleDevice .from_proto (spec )
217
+ device = cirq_google .GridDevice .from_proto (spec )
197
218
printer = mock .Mock ()
198
219
device ._repr_pretty_ (printer , cycle )
199
220
printer .text .assert_called_once_with (func (device ))
200
221
201
222
202
- def test_serializable_device_str_named_qubits ():
223
+ def test_grid_device_str_named_qubits ():
203
224
q_proto_id = v2 .qubit_to_proto_id (cirq .NamedQubit ('q' ))
204
225
spec = v2 .device_pb2 .DeviceSpecification ()
205
226
spec .valid_qubits .extend ([q_proto_id ])
206
- device = cirq_google .GoogleDevice .from_proto (spec )
227
+ device = cirq_google .GridDevice .from_proto (spec )
207
228
assert device .__class__ .__name__ in str (device )
0 commit comments