@@ -5054,3 +5054,106 @@ def test_insert_moments_and_ops_latest() -> None:
50545054 cirq .Moment ([cirq .H (q [1 ])]),
50555055 )
50565056 assert c .insert (insert_index , moments_and_ops , cirq .InsertStrategy .LATEST ) == index_after
5057+
5058+
5059+ def test_insert_earliest_batch_with_measurement_key_dependency () -> None :
5060+ q0 , q1 = cirq .LineQubit .range (2 )
5061+ c = cirq .Circuit (cirq .X (q0 ), cirq .X (q1 ))
5062+ ops_to_insert = [cirq .measure (q0 , key = "k" ), cirq .X (q1 ).with_classical_controls ("k" )]
5063+ c .insert (0 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5064+
5065+ assert c == cirq .Circuit (
5066+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5067+ cirq .Moment (cirq .X (q1 ).with_classical_controls ("k" )),
5068+ cirq .Moment (cirq .X (q0 ), cirq .X (q1 )),
5069+ )
5070+
5071+
5072+ def test_insert_earliest_op_with_control_key_unions_with_existing_moment () -> None :
5073+ q0 , q1 = cirq .LineQubit .range (2 )
5074+ c = cirq .Circuit (cirq .X (q0 ))
5075+ ops_to_insert = [cirq .measure (q0 , key = "k" ), cirq .X (q1 ).with_classical_controls ("k" )]
5076+ c .insert (0 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5077+ assert c == cirq .Circuit (
5078+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5079+ cirq .Moment (cirq .X (q0 ), cirq .X (q1 ).with_classical_controls ("k" )),
5080+ )
5081+
5082+
5083+ def test_insert_earliest_op_with_control_key () -> None :
5084+ q0 , q1 = cirq .LineQubit .range (2 )
5085+ c = cirq .Circuit (cirq .measure (q0 , key = "k" ))
5086+ ops_to_insert = [cirq .X (q1 ).with_classical_controls ("k" )]
5087+ c .insert (0 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5088+
5089+ assert c == cirq .Circuit (
5090+ cirq .Moment (cirq .X (q1 ).with_classical_controls ("k" )), cirq .Moment (cirq .measure (q0 , key = "k" ))
5091+ )
5092+
5093+
5094+ def test_insert_earliest_batch_same_measurement_key () -> None :
5095+ q0 , q1 = cirq .LineQubit .range (2 )
5096+ c = cirq .Circuit (cirq .X (q0 ), cirq .X (q1 ))
5097+ ops_to_insert = [cirq .measure (q0 , key = "k" ), cirq .measure (q1 , key = "k" )]
5098+ c .insert (0 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5099+
5100+ assert c == cirq .Circuit (
5101+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5102+ cirq .Moment (cirq .measure (q1 , key = "k" )),
5103+ cirq .Moment (cirq .X (q0 ), cirq .X (q1 )),
5104+ )
5105+
5106+
5107+ def test_insert_earliest_batch_with_measurement_key_dependency_reversed () -> None :
5108+ q0 , q1 = cirq .LineQubit .range (2 )
5109+ c = cirq .Circuit (cirq .X (q0 ), cirq .X (q1 ))
5110+ ops_to_insert = [cirq .X (q1 ).with_classical_controls ("k" ), cirq .measure (q0 , key = "k" )]
5111+ c .insert (0 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5112+
5113+ assert c == cirq .Circuit (
5114+ cirq .Moment (cirq .X (q1 ).with_classical_controls ("k" )),
5115+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5116+ cirq .Moment (cirq .X (q0 ), cirq .X (q1 )),
5117+ )
5118+
5119+
5120+ def test_insert_earliest_batch () -> None :
5121+ q0 , q1 = cirq .LineQubit .range (2 )
5122+ # Create a circuit with every operation in a separate moment
5123+ c = cirq .Circuit (
5124+ cirq .X (q0 ), cirq .measure (q0 , key = "k" ), cirq .X (q1 ), strategy = cirq .InsertStrategy .NEW
5125+ )
5126+ ops_to_insert = [cirq .X (q0 ).with_classical_controls ("k" ), cirq .measure (q0 , key = "k" )]
5127+ c .insert (2 , ops_to_insert , strategy = cirq .InsertStrategy .EARLIEST )
5128+
5129+ assert c == cirq .Circuit (
5130+ cirq .Moment (cirq .X (q0 )),
5131+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5132+ cirq .Moment (cirq .X (q0 ).with_classical_controls ("k" ), cirq .X (q1 )),
5133+ cirq .Moment (cirq .measure (q0 , key = "k" )),
5134+ )
5135+
5136+
5137+ def test_insert_in_existing_moment_same_measurement_key_different_qubits () -> None :
5138+ q0 , q1 = cirq .LineQubit .range (2 )
5139+ c = cirq .Circuit (cirq .measure (q0 , key = "k" ))
5140+
5141+ op_to_add = cirq .measure (q1 , key = "k" )
5142+
5143+ c .insert (0 , [op_to_add ], strategy = cirq .InsertStrategy .EARLIEST )
5144+
5145+ assert c == cirq .Circuit (
5146+ cirq .Moment (cirq .measure (q1 , key = "k" )), cirq .Moment (cirq .measure (q0 , key = "k" ))
5147+ )
5148+
5149+
5150+ def test_insert_in_existing_moment_measurement_control_key_conflict () -> None :
5151+ c = cirq .Circuit (cirq .measure (q0 , key = "k" ))
5152+
5153+ op_to_add = cirq .X (q1 ).with_classical_controls ("k" )
5154+
5155+ c .insert (0 , [op_to_add ], strategy = cirq .InsertStrategy .EARLIEST )
5156+
5157+ assert c == cirq .Circuit (
5158+ cirq .Moment (cirq .X (q1 ).with_classical_controls ("k" )), cirq .Moment (cirq .measure (q0 , key = "k" ))
5159+ )
0 commit comments