@@ -27,10 +27,55 @@ def test_insertion_sort() -> None:
2727 cirq .GlobalPhaseGate (1j ).on (),
2828 )
2929 sorted_circuit = cirq .transformers .insertion_sort_transformer (c )
30- assert sorted_circuit == cirq .Circuit (
31- cirq .GlobalPhaseGate (1j ).on (),
32- cirq .CZ (cirq .q (0 ), cirq .q (1 )),
33- cirq .CZ (cirq .q (2 ), cirq .q (1 )),
34- cirq .CZ (cirq .q (2 ), cirq .q (1 )),
35- cirq .CZ (cirq .q (2 ), cirq .q (4 )),
30+ cirq .testing .assert_same_circuits (
31+ sorted_circuit ,
32+ cirq .Circuit (
33+ cirq .GlobalPhaseGate (1j ).on (),
34+ cirq .CZ (cirq .q (0 ), cirq .q (1 )),
35+ cirq .CZ (cirq .q (2 ), cirq .q (1 )),
36+ cirq .CZ (cirq .q (2 ), cirq .q (1 )),
37+ cirq .CZ (cirq .q (2 ), cirq .q (4 )),
38+ ),
39+ )
40+
41+
42+ def test_insertion_sort_same_measurement_key () -> None :
43+ q0 , q1 = cirq .LineQubit .range (2 )
44+ c = cirq .Circuit (cirq .measure (q1 , key = 'k' ), cirq .measure (q0 , key = 'k' ))
45+ cirq .testing .assert_same_circuits (cirq .transformers .insertion_sort_transformer (c ), c )
46+
47+
48+ def test_insertion_sort_measurement_and_control_key_conflict () -> None :
49+ q0 , q1 = cirq .LineQubit .range (2 )
50+ c = cirq .Circuit (cirq .measure (q1 , key = 'k' ), cirq .X (q0 ).with_classical_controls ('k' ))
51+ # Second operation depends on the first so they don't commute.
52+ cirq .testing .assert_same_circuits (cirq .transformers .insertion_sort_transformer (c ), c )
53+
54+
55+ def test_insertion_sort_measurement_and_control_key_conflict_other_way_around () -> None :
56+ q0 , q1 = cirq .LineQubit .range (2 )
57+ c = cirq .Circuit (
58+ cirq .measure (q0 , key = 'k' ),
59+ cirq .X (q1 ).with_classical_controls ('k' ),
60+ cirq .measure (q0 , key = 'k' ),
61+ )
62+ cirq .testing .assert_same_circuits (cirq .transformers .insertion_sort_transformer (c ), c )
63+
64+
65+ def test_insertion_sort_distinct_measurement_keys () -> None :
66+ q0 , q1 = cirq .LineQubit .range (2 )
67+ c = cirq .Circuit (cirq .measure (q1 , key = 'k1' ), cirq .measure (q0 , key = 'k0' ))
68+ # Measurement keys are distinct, so the measurements commute.
69+ expected = cirq .Circuit (cirq .measure (q0 , key = 'k0' ), cirq .measure (q1 , key = 'k1' ))
70+ assert cirq .transformers .insertion_sort_transformer (c )[0 ].operations == expected [0 ].operations
71+
72+
73+ def test_insertion_sort_shared_control_key () -> None :
74+ q0 , q1 = cirq .LineQubit .range (2 )
75+ c = cirq .Circuit (
76+ cirq .X (q1 ).with_classical_controls ('k' ), cirq .X (q0 ).with_classical_controls ('k' )
77+ )
78+ expected = cirq .Circuit (
79+ cirq .X (q0 ).with_classical_controls ('k' ), cirq .X (q1 ).with_classical_controls ('k' )
3680 )
81+ assert cirq .transformers .insertion_sort_transformer (c )[0 ].operations == expected [0 ].operations
0 commit comments