@@ -290,7 +290,7 @@ def test_h_str():
290
290
assert str (cirq .H ** 0.5 ) == 'H^0.5'
291
291
292
292
293
- def test_x_act_on ():
293
+ def test_x_act_on_tableau ():
294
294
with pytest .raises (TypeError , match = "Failed to act" ):
295
295
cirq .act_on (cirq .X , object ())
296
296
original_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 31 )
@@ -326,14 +326,21 @@ def test_x_act_on():
326
326
cirq .act_on (cirq .X ** foo , args )
327
327
328
328
329
- class PhaserGate (cirq .SingleQubitGate ):
329
+ class iZGate (cirq .SingleQubitGate ):
330
330
"""Equivalent to an iZ gate without _act_on_ defined on it."""
331
331
332
332
def _unitary_ (self ):
333
333
return np .array ([[1j , 0 ], [0 , - 1j ]])
334
334
335
335
336
- def test_y_act_on ():
336
+ class MinusOnePhaseGate (cirq .SingleQubitGate ):
337
+ """Equivalent to a -1 global phase without _act_on_ defined on it."""
338
+
339
+ def _unitary_ (self ):
340
+ return np .array ([[- 1 , 0 ], [0 , - 1 ]])
341
+
342
+
343
+ def test_y_act_on_tableau ():
337
344
with pytest .raises (TypeError , match = "Failed to act" ):
338
345
cirq .act_on (cirq .Y , object ())
339
346
original_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 31 )
@@ -348,18 +355,18 @@ def test_y_act_on():
348
355
349
356
cirq .act_on (cirq .Y ** 0.5 , args , allow_decompose = False )
350
357
cirq .act_on (cirq .Y ** 0.5 , args , allow_decompose = False )
351
- cirq .act_on (PhaserGate (), args )
358
+ cirq .act_on (iZGate (), args )
352
359
assert args .log_of_measurement_results == {}
353
360
assert args .tableau == flipped_tableau
354
361
355
362
cirq .act_on (cirq .Y , args , allow_decompose = False )
356
- cirq .act_on (PhaserGate (), args , allow_decompose = True )
363
+ cirq .act_on (iZGate (), args , allow_decompose = True )
357
364
assert args .log_of_measurement_results == {}
358
365
assert args .tableau == original_tableau
359
366
360
367
cirq .act_on (cirq .Y ** 3.5 , args , allow_decompose = False )
361
368
cirq .act_on (cirq .Y ** 3.5 , args , allow_decompose = False )
362
- cirq .act_on (PhaserGate (), args )
369
+ cirq .act_on (iZGate (), args )
363
370
assert args .log_of_measurement_results == {}
364
371
assert args .tableau == flipped_tableau
365
372
@@ -372,9 +379,11 @@ def test_y_act_on():
372
379
cirq .act_on (cirq .Y ** foo , args )
373
380
374
381
375
- def test_z_h_act_on ():
382
+ def test_z_h_act_on_tableau ():
376
383
with pytest .raises (TypeError , match = "Failed to act" ):
377
- cirq .act_on (cirq .Y , object ())
384
+ cirq .act_on (cirq .Z , object ())
385
+ with pytest .raises (TypeError , match = "Failed to act" ):
386
+ cirq .act_on (cirq .H , object ())
378
387
original_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 31 )
379
388
flipped_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 23 )
380
389
@@ -417,18 +426,16 @@ def test_z_h_act_on():
417
426
with pytest .raises (TypeError , match = "Failed to act action on state" ):
418
427
cirq .act_on (cirq .Z ** foo , args )
419
428
420
- foo = sympy .Symbol ('foo' )
421
429
with pytest .raises (TypeError , match = "Failed to act action on state" ):
422
430
cirq .act_on (cirq .H ** foo , args )
423
431
424
- foo = sympy .Symbol ('foo' )
425
432
with pytest .raises (TypeError , match = "Failed to act action on state" ):
426
433
cirq .act_on (cirq .H ** 1.5 , args )
427
434
428
435
429
- def test_cx_act_on ():
436
+ def test_cx_act_on_tableau ():
430
437
with pytest .raises (TypeError , match = "Failed to act" ):
431
- cirq .act_on (cirq .Y , object ())
438
+ cirq .act_on (cirq .CX , object ())
432
439
original_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 31 )
433
440
434
441
args = cirq .ActOnCliffordTableauArgs (
@@ -471,7 +478,7 @@ def test_cx_act_on():
471
478
cirq .act_on (cirq .CX ** 1.5 , args )
472
479
473
480
474
- def test_cz_act_on ():
481
+ def test_cz_act_on_tableau ():
475
482
with pytest .raises (TypeError , match = "Failed to act" ):
476
483
cirq .act_on (cirq .Y , object ())
477
484
original_tableau = cirq .CliffordTableau (num_qubits = 5 , initial_state = 31 )
@@ -516,38 +523,102 @@ def test_cz_act_on():
516
523
cirq .act_on (cirq .CZ ** 1.5 , args )
517
524
518
525
526
+ foo = sympy .Symbol ('foo' )
527
+
528
+
529
+ @pytest .mark .parametrize ('input_gate_sequence, outcome' , [
530
+ ([cirq .X ** foo ], 'Error' ),
531
+ ([cirq .X ** 0.25 ], 'Error' ),
532
+ ([cirq .X ** 4 ], 'Original' ),
533
+ ([cirq .X ** 0.5 , cirq .X ** 0.5 ], 'Flipped' ),
534
+ ([cirq .X ], 'Flipped' ),
535
+ ([cirq .X ** 3.5 , cirq .X ** 3.5 ], 'Flipped' ),
536
+ ([cirq .Y ** foo ], 'Error' ),
537
+ ([cirq .Y ** 0.25 ], 'Error' ),
538
+ ([cirq .Y ** 4 ], 'Original' ),
539
+ ([cirq .Y ** 0.5 , cirq .Y ** 0.5 , iZGate ()], 'Flipped' ),
540
+ ([cirq .Y , iZGate ()], 'Flipped' ),
541
+ ([cirq .Y ** 3.5 , cirq .Y ** 3.5 , iZGate ()], 'Flipped' ),
542
+ ([cirq .Z ** foo ], 'Error' ),
543
+ ([cirq .H ** foo ], 'Error' ),
544
+ ([cirq .H ** 1.5 ], 'Error' ),
545
+ ([cirq .Z ** 4 ], 'Original' ),
546
+ ([cirq .H ** 4 ], 'Original' ),
547
+ ([cirq .H , cirq .S , cirq .S , cirq .H ], 'Flipped' ),
548
+ ([cirq .H , cirq .Z , cirq .H ], 'Flipped' ),
549
+ ([cirq .H , cirq .Z ** 3.5 , cirq .Z ** 3.5 , cirq .H ], 'Flipped' ),
550
+ ([cirq .CX ** foo ], 'Error' ),
551
+ ([cirq .CX ** 1.5 ], 'Error' ),
552
+ ([cirq .CX ** 4 ], 'Original' ),
553
+ ([cirq .CX ], 'Flipped' ),
554
+ ([cirq .CZ ** foo ], 'Error' ),
555
+ ([cirq .CZ ** 1.5 ], 'Error' ),
556
+ ([cirq .CZ ** 4 ], 'Original' ),
557
+ ([cirq .CZ , MinusOnePhaseGate ()], 'Original' ),
558
+ ])
559
+ def test_act_on_ch_form (input_gate_sequence , outcome ):
560
+ original_state = cirq .StabilizerStateChForm (num_qubits = 5 , initial_state = 31 )
561
+ num_qubits = cirq .num_qubits (input_gate_sequence [0 ])
562
+ if num_qubits == 1 :
563
+ axes = [1 ]
564
+ else :
565
+ assert num_qubits == 2
566
+ axes = [0 , 1 ]
567
+ args = cirq .ActOnStabilizerCHFormArgs (state = original_state .copy (),
568
+ axes = axes )
569
+
570
+ flipped_state = cirq .StabilizerStateChForm (num_qubits = 5 , initial_state = 23 )
571
+
572
+ if outcome == 'Error' :
573
+ with pytest .raises (TypeError , match = "Failed to act action on state" ):
574
+ for input_gate in input_gate_sequence :
575
+ cirq .act_on (input_gate , args )
576
+ return
577
+
578
+ for input_gate in input_gate_sequence :
579
+ cirq .act_on (input_gate , args )
580
+
581
+ if outcome == 'Original' :
582
+ np .testing .assert_allclose (args .state .state_vector (),
583
+ original_state .state_vector ())
584
+
585
+ if outcome == 'Flipped' :
586
+ np .testing .assert_allclose (args .state .state_vector (),
587
+ flipped_state .state_vector ())
588
+
589
+
519
590
@pytest .mark .parametrize (
520
- 'input_gate' ,
591
+ 'input_gate, assert_implemented ' ,
521
592
[
522
- cirq .X ,
523
- cirq .Y ,
524
- cirq .Z ,
525
- cirq .X ** 0.5 ,
526
- cirq .Y ** 0.5 ,
527
- cirq .Z ** 0.5 ,
528
- cirq .X ** 3.5 ,
529
- cirq .Y ** 3.5 ,
530
- cirq .Z ** 3.5 ,
531
- cirq .X ** 4 ,
532
- cirq .Y ** 4 ,
533
- cirq .Z ** 4 ,
534
- cirq .H ,
535
- cirq .CX ,
536
- cirq .CZ ,
537
- cirq .H ** 4 ,
538
- cirq .CX ** 4 ,
539
- cirq .CZ ** 4 ,
540
- # Gates not supported by CliffordTableau should not fail too.
541
- cirq .X ** 0.25 ,
542
- cirq .Y ** 0.25 ,
543
- cirq .Z ** 0.25 ,
544
- cirq .H ** 0.5 ,
545
- cirq .CX ** 0.5 ,
546
- cirq .CZ ** 0.5
593
+ ( cirq .X , True ) ,
594
+ ( cirq .Y , True ) ,
595
+ ( cirq .Z , True ) ,
596
+ ( cirq .X ** 0.5 , True ) ,
597
+ ( cirq .Y ** 0.5 , True ) ,
598
+ ( cirq .Z ** 0.5 , True ) ,
599
+ ( cirq .X ** 3.5 , True ) ,
600
+ ( cirq .Y ** 3.5 , True ) ,
601
+ ( cirq .Z ** 3.5 , True ) ,
602
+ ( cirq .X ** 4 , True ) ,
603
+ ( cirq .Y ** 4 , True ) ,
604
+ ( cirq .Z ** 4 , True ) ,
605
+ ( cirq .H , True ) ,
606
+ ( cirq .CX , True ) ,
607
+ ( cirq .CZ , True ) ,
608
+ ( cirq .H ** 4 , True ) ,
609
+ ( cirq .CX ** 4 , True ) ,
610
+ ( cirq .CZ ** 4 , True ) ,
611
+ # Unsupported gates should not fail too.
612
+ ( cirq .X ** 0.25 , False ) ,
613
+ ( cirq .Y ** 0.25 , False ) ,
614
+ ( cirq .Z ** 0.25 , False ) ,
615
+ ( cirq .H ** 0.5 , False ) ,
616
+ ( cirq .CX ** 0.5 , False ) ,
617
+ ( cirq .CZ ** 0.5 , False ),
547
618
])
548
- def test_act_on_clifford_tableau (input_gate ):
549
- cirq .testing .assert_act_on_clifford_tableau_effect_matches_unitary (
550
- input_gate )
619
+ def test_act_on_consistency (input_gate , assert_implemented ):
620
+ cirq .testing .assert_all_implemented_act_on_effects_match_unitary (
621
+ input_gate , assert_implemented , assert_implemented )
551
622
552
623
553
624
def test_runtime_types_of_rot_gates ():
0 commit comments