@@ -142,8 +142,12 @@ def get_val(op):
142
142
class OpSerializerTest (tf .test .TestCase , parameterized .TestCase ):
143
143
"""Test OpSerializer functions correctly."""
144
144
145
- @parameterized .parameters (TEST_CASES )
146
- def test_to_proto_attribute (self , val_type , val , arg_value ):
145
+ @parameterized .parameters ([
146
+ CASE + (x ,)
147
+ for CASE in TEST_CASES
148
+ for x in [cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )]
149
+ ])
150
+ def test_to_proto_attribute (self , val_type , val , arg_value , q ):
147
151
"""Test proto attribute serialization works."""
148
152
serializer = op_serializer .GateOpSerializer (
149
153
gate_type = GateWithAttribute ,
@@ -153,7 +157,6 @@ def test_to_proto_attribute(self, val_type, val, arg_value):
153
157
serialized_type = val_type ,
154
158
op_getter = 'val' )
155
159
])
156
- q = cirq .GridQubit (1 , 2 )
157
160
result = serializer .to_proto (GateWithAttribute (val )(q ),
158
161
arg_function_language = 'linear' )
159
162
expected = op_proto ({
@@ -164,13 +167,17 @@ def test_to_proto_attribute(self, val_type, val, arg_value):
164
167
'my_val' : arg_value
165
168
},
166
169
'qubits' : [{
167
- 'id' : '1_2'
170
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
168
171
}]
169
172
})
170
173
self .assertEqual (result , expected )
171
174
172
- @parameterized .parameters (TEST_CASES )
173
- def test_to_proto_property (self , val_type , val , arg_value ):
175
+ @parameterized .parameters ([
176
+ CASE + (x ,)
177
+ for CASE in TEST_CASES
178
+ for x in [cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )]
179
+ ])
180
+ def test_to_proto_property (self , val_type , val , arg_value , q ):
174
181
"""Test proto property serialization works."""
175
182
serializer = op_serializer .GateOpSerializer (
176
183
gate_type = GateWithProperty ,
@@ -180,7 +187,6 @@ def test_to_proto_property(self, val_type, val, arg_value):
180
187
serialized_type = val_type ,
181
188
op_getter = 'val' )
182
189
])
183
- q = cirq .GridQubit (1 , 2 )
184
190
result = serializer .to_proto (GateWithProperty (val )(q ),
185
191
arg_function_language = 'linear' )
186
192
expected = op_proto ({
@@ -191,13 +197,17 @@ def test_to_proto_property(self, val_type, val, arg_value):
191
197
'my_val' : arg_value
192
198
},
193
199
'qubits' : [{
194
- 'id' : '1_2'
200
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
195
201
}]
196
202
})
197
203
self .assertEqual (result , expected )
198
204
199
- @parameterized .parameters (TEST_CASES )
200
- def test_to_proto_callable (self , val_type , val , arg_value ):
205
+ @parameterized .parameters ([
206
+ CASE + (x ,)
207
+ for CASE in TEST_CASES
208
+ for x in [cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )]
209
+ ])
210
+ def test_to_proto_callable (self , val_type , val , arg_value , q ):
201
211
"""Test callable serialization works."""
202
212
serializer = op_serializer .GateOpSerializer (
203
213
gate_type = GateWithMethod ,
@@ -207,7 +217,6 @@ def test_to_proto_callable(self, val_type, val, arg_value):
207
217
serialized_type = val_type ,
208
218
op_getter = get_val )
209
219
])
210
- q = cirq .GridQubit (1 , 2 )
211
220
result = serializer .to_proto (GateWithMethod (val )(q ),
212
221
arg_function_language = 'linear' )
213
222
expected = op_proto ({
@@ -218,12 +227,13 @@ def test_to_proto_callable(self, val_type, val, arg_value):
218
227
'my_val' : arg_value
219
228
},
220
229
'qubits' : [{
221
- 'id' : '1_2'
230
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
222
231
}]
223
232
})
224
233
self .assertEqual (result , expected )
225
234
226
- def test_to_proto_gate_predicate (self ):
235
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
236
+ def test_to_proto_gate_predicate (self , q ):
227
237
"""Test can_serialize works."""
228
238
serializer = op_serializer .GateOpSerializer (
229
239
gate_type = GateWithAttribute ,
@@ -234,15 +244,15 @@ def test_to_proto_gate_predicate(self):
234
244
op_getter = 'val' )
235
245
],
236
246
can_serialize_predicate = lambda x : x .gate .val == 1 )
237
- q = cirq .GridQubit (1 , 2 )
238
247
self .assertIsNone (serializer .to_proto (GateWithAttribute (0 )(q )))
239
248
self .assertIsNotNone (serializer .to_proto (GateWithAttribute (1 )(q )))
240
249
self .assertFalse (
241
250
serializer .can_serialize_operation (GateWithAttribute (0 )(q )))
242
251
self .assertTrue (
243
252
serializer .can_serialize_operation (GateWithAttribute (1 )(q )))
244
253
245
- def test_to_proto_gate_mismatch (self ):
254
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
255
+ def test_to_proto_gate_mismatch (self , q ):
246
256
"""Test proto gate mismatch errors."""
247
257
serializer = op_serializer .GateOpSerializer (
248
258
gate_type = GateWithProperty ,
@@ -252,13 +262,13 @@ def test_to_proto_gate_mismatch(self):
252
262
serialized_type = float ,
253
263
op_getter = 'val' )
254
264
])
255
- q = cirq .GridQubit (1 , 2 )
256
265
with self .assertRaisesRegex (
257
266
ValueError ,
258
267
expected_regex = 'GateWithAttribute.*GateWithProperty' ):
259
268
serializer .to_proto (GateWithAttribute (1.0 )(q ))
260
269
261
- def test_to_proto_unsupported_type (self ):
270
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
271
+ def test_to_proto_unsupported_type (self , q ):
262
272
"""Test proto unsupported types errors."""
263
273
serializer = op_serializer .GateOpSerializer (
264
274
gate_type = GateWithProperty ,
@@ -268,11 +278,11 @@ def test_to_proto_unsupported_type(self):
268
278
serialized_type = bytes ,
269
279
op_getter = 'val' )
270
280
])
271
- q = cirq .GridQubit (1 , 2 )
272
281
with self .assertRaisesRegex (ValueError , expected_regex = 'bytes' ):
273
282
serializer .to_proto (GateWithProperty (b's' )(q ))
274
283
275
- def test_to_proto_required_but_not_present (self ):
284
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
285
+ def test_to_proto_required_but_not_present (self , q ):
276
286
"""Test required and missing args errors."""
277
287
serializer = op_serializer .GateOpSerializer (
278
288
gate_type = GateWithProperty ,
@@ -282,11 +292,11 @@ def test_to_proto_required_but_not_present(self):
282
292
serialized_type = float ,
283
293
op_getter = lambda x : None )
284
294
])
285
- q = cirq .GridQubit (1 , 2 )
286
295
with self .assertRaisesRegex (ValueError , expected_regex = 'required' ):
287
296
serializer .to_proto (GateWithProperty (1.0 )(q ))
288
297
289
- def test_to_proto_no_getattr (self ):
298
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
299
+ def test_to_proto_no_getattr (self , q ):
290
300
"""Test no op getter fails."""
291
301
serializer = op_serializer .GateOpSerializer (
292
302
gate_type = GateWithProperty ,
@@ -296,11 +306,11 @@ def test_to_proto_no_getattr(self):
296
306
serialized_type = float ,
297
307
op_getter = 'nope' )
298
308
])
299
- q = cirq .GridQubit (1 , 2 )
300
309
with self .assertRaisesRegex (ValueError , expected_regex = 'does not have' ):
301
310
serializer .to_proto (GateWithProperty (1.0 )(q ))
302
311
303
- def test_to_proto_not_required_ok (self ):
312
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
313
+ def test_to_proto_not_required_ok (self , q ):
304
314
"""Test non require arg absense succeeds."""
305
315
serializer = op_serializer .GateOpSerializer (
306
316
gate_type = GateWithProperty ,
@@ -326,15 +336,19 @@ def test_to_proto_not_required_ok(self):
326
336
}
327
337
},
328
338
'qubits' : [{
329
- 'id' : '1_2'
339
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
330
340
}]
331
341
})
332
342
333
- q = cirq .GridQubit (1 , 2 )
334
343
self .assertEqual (serializer .to_proto (GateWithProperty (0.125 )(q )),
335
344
expected )
336
345
337
346
@parameterized .parameters ([{
347
+ ** x ,
348
+ ** {
349
+ 'q' : q
350
+ }
351
+ } for x in [{
338
352
'val_type' : float ,
339
353
'val' : 's'
340
354
}, {
@@ -352,8 +366,8 @@ def test_to_proto_not_required_ok(self):
352
366
}, {
353
367
'val_type' : List [bool ],
354
368
'val' : (1.0 ,)
355
- }])
356
- def test_to_proto_type_mismatch (self , val_type , val ):
369
+ }] for q in [ cirq . GridQubit ( 1 , 2 ), cirq . LineQubit ( 4 )]] )
370
+ def test_to_proto_type_mismatch (self , val_type , val , q ):
357
371
"""Test type mismatch fails."""
358
372
serializer = op_serializer .GateOpSerializer (
359
373
gate_type = GateWithProperty ,
@@ -363,11 +377,11 @@ def test_to_proto_type_mismatch(self, val_type, val):
363
377
serialized_type = val_type ,
364
378
op_getter = 'val' )
365
379
])
366
- q = cirq .GridQubit (1 , 2 )
367
380
with self .assertRaisesRegex (ValueError , expected_regex = str (type (val ))):
368
381
serializer .to_proto (GateWithProperty (val )(q ))
369
382
370
- def test_can_serialize_operation_subclass (self ):
383
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
384
+ def test_can_serialize_operation_subclass (self , q ):
371
385
"""Test can serialize subclass."""
372
386
serializer = op_serializer .GateOpSerializer (
373
387
gate_type = GateWithAttribute ,
@@ -378,11 +392,11 @@ def test_can_serialize_operation_subclass(self):
378
392
op_getter = 'val' )
379
393
],
380
394
can_serialize_predicate = lambda x : x .gate .val == 1 )
381
- q = cirq .GridQubit (1 , 1 )
382
395
self .assertTrue (serializer .can_serialize_operation (SubclassGate (1 )(q )))
383
396
self .assertFalse (serializer .can_serialize_operation (SubclassGate (0 )(q )))
384
397
385
- def test_defaults_not_serialized (self ):
398
+ @parameterized .parameters ([cirq .GridQubit (1 , 2 ), cirq .LineQubit (4 )])
399
+ def test_defaults_not_serialized (self , q ):
386
400
"""Test defaults not serialized."""
387
401
serializer = op_serializer .GateOpSerializer (
388
402
gate_type = GateWithAttribute ,
@@ -393,7 +407,6 @@ def test_defaults_not_serialized(self):
393
407
default = 1.0 ,
394
408
op_getter = 'val' )
395
409
])
396
- q = cirq .GridQubit (1 , 2 )
397
410
no_default = op_proto ({
398
411
'gate' : {
399
412
'id' : 'my_gate'
@@ -406,7 +419,7 @@ def test_defaults_not_serialized(self):
406
419
}
407
420
},
408
421
'qubits' : [{
409
- 'id' : '1_2'
422
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
410
423
}]
411
424
})
412
425
self .assertEqual (no_default ,
@@ -416,7 +429,7 @@ def test_defaults_not_serialized(self):
416
429
'id' : 'my_gate'
417
430
},
418
431
'qubits' : [{
419
- 'id' : '1_2'
432
+ 'id' : '1_2' if isinstance ( q , cirq . GridQubit ) else '4'
420
433
}]
421
434
})
422
435
self .assertEqual (with_default ,
0 commit comments