@@ -294,15 +294,15 @@ def test_repeat(add_measurements: bool, use_default_ids_for_initial_rep: bool) -
294294 op_with_reps : Optional [cirq .CircuitOperation ] = None
295295 rep_ids = []
296296 if use_default_ids_for_initial_rep :
297- op_with_reps = op_base .repeat (initial_repetitions )
298297 rep_ids = ['0' , '1' , '2' ]
299- assert op_base ** initial_repetitions == op_with_reps
298+ op_with_reps = op_base . repeat ( initial_repetitions , use_repetition_ids = True )
300299 else :
301300 rep_ids = ['a' , 'b' , 'c' ]
302301 op_with_reps = op_base .repeat (initial_repetitions , rep_ids )
303- assert op_base ** initial_repetitions != op_with_reps
304- assert (op_base ** initial_repetitions ).replace (repetition_ids = rep_ids ) == op_with_reps
302+ assert op_base ** initial_repetitions != op_with_reps
303+ assert (op_base ** initial_repetitions ).replace (repetition_ids = rep_ids ) == op_with_reps
305304 assert op_with_reps .repetitions == initial_repetitions
305+ assert op_with_reps .use_repetition_ids
306306 assert op_with_reps .repetition_ids == rep_ids
307307 assert op_with_reps .repeat (1 ) is op_with_reps
308308
@@ -436,6 +436,7 @@ def test_parameterized_repeat_side_effects():
436436 op = cirq .CircuitOperation (
437437 cirq .FrozenCircuit (cirq .X (q ).with_classical_controls ('c' ), cirq .measure (q , key = 'm' )),
438438 repetitions = sympy .Symbol ('a' ),
439+ use_repetition_ids = True ,
439440 )
440441
441442 # Control keys can be calculated because they only "lift" if there's a matching
@@ -689,7 +690,6 @@ def test_string_format():
689690 ),
690691 ),
691692 ]),
692- use_repetition_ids=False,
693693)"""
694694 )
695695 op7 = cirq .CircuitOperation (
@@ -706,7 +706,6 @@ def test_string_format():
706706 cirq.measure(cirq.LineQubit(0), key=cirq.MeasurementKey(name='a')),
707707 ),
708708 ]),
709- use_repetition_ids=False,
710709 repeat_until=cirq.KeyCondition(cirq.MeasurementKey(name='a')),
711710)"""
712711 )
@@ -737,6 +736,7 @@ def test_json_dict():
737736 'param_resolver' : op .param_resolver ,
738737 'parent_path' : op .parent_path ,
739738 'repetition_ids' : None ,
739+ 'use_repetition_ids' : False ,
740740 }
741741
742742
@@ -843,6 +843,26 @@ def test_decompose_loops_with_measurements():
843843 circuit = cirq .FrozenCircuit (cirq .H (a ), cirq .CX (a , b ), cirq .measure (a , b , key = 'm' ))
844844 base_op = cirq .CircuitOperation (circuit )
845845
846+ op = base_op .with_qubits (b , a ).repeat (3 )
847+ expected_circuit = cirq .Circuit (
848+ cirq .H (b ),
849+ cirq .CX (b , a ),
850+ cirq .measure (b , a , key = cirq .MeasurementKey .parse_serialized ('m' )),
851+ cirq .H (b ),
852+ cirq .CX (b , a ),
853+ cirq .measure (b , a , key = cirq .MeasurementKey .parse_serialized ('m' )),
854+ cirq .H (b ),
855+ cirq .CX (b , a ),
856+ cirq .measure (b , a , key = cirq .MeasurementKey .parse_serialized ('m' )),
857+ )
858+ assert cirq .Circuit (cirq .decompose_once (op )) == expected_circuit
859+
860+
861+ def test_decompose_loops_with_measurements_use_rep_ids ():
862+ a , b = cirq .LineQubit .range (2 )
863+ circuit = cirq .FrozenCircuit (cirq .H (a ), cirq .CX (a , b ), cirq .measure (a , b , key = 'm' ))
864+ base_op = cirq .CircuitOperation (circuit , use_repetition_ids = True )
865+
846866 op = base_op .with_qubits (b , a ).repeat (3 )
847867 expected_circuit = cirq .Circuit (
848868 cirq .H (b ),
@@ -999,7 +1019,9 @@ def test_keys_under_parent_path():
9991019 op3 = cirq .with_key_path_prefix (op2 , ('C' ,))
10001020 assert cirq .measurement_key_names (op3 ) == {'C:B:A' }
10011021 op4 = op3 .repeat (2 )
1002- assert cirq .measurement_key_names (op4 ) == {'C:B:0:A' , 'C:B:1:A' }
1022+ assert cirq .measurement_key_names (op4 ) == {'C:B:A' }
1023+ op4_rep = op3 .repeat (2 ).replace (use_repetition_ids = True )
1024+ assert cirq .measurement_key_names (op4_rep ) == {'C:B:0:A' , 'C:B:1:A' }
10031025
10041026
10051027def test_mapped_circuit_preserves_moments ():
@@ -1077,12 +1099,8 @@ def test_mapped_circuit_allows_repeated_keys():
10771099def test_simulate_no_repetition_ids_both_levels (sim ):
10781100 q = cirq .LineQubit (0 )
10791101 inner = cirq .Circuit (cirq .measure (q , key = 'a' ))
1080- middle = cirq .Circuit (
1081- cirq .CircuitOperation (inner .freeze (), repetitions = 2 , use_repetition_ids = False )
1082- )
1083- outer_subcircuit = cirq .CircuitOperation (
1084- middle .freeze (), repetitions = 2 , use_repetition_ids = False
1085- )
1102+ middle = cirq .Circuit (cirq .CircuitOperation (inner .freeze (), repetitions = 2 ))
1103+ outer_subcircuit = cirq .CircuitOperation (middle .freeze (), repetitions = 2 )
10861104 circuit = cirq .Circuit (outer_subcircuit )
10871105 result = sim .run (circuit )
10881106 assert result .records ['a' ].shape == (1 , 4 , 1 )
@@ -1092,10 +1110,10 @@ def test_simulate_no_repetition_ids_both_levels(sim):
10921110def test_simulate_no_repetition_ids_outer (sim ):
10931111 q = cirq .LineQubit (0 )
10941112 inner = cirq .Circuit (cirq .measure (q , key = 'a' ))
1095- middle = cirq .Circuit (cirq .CircuitOperation (inner .freeze (), repetitions = 2 ))
1096- outer_subcircuit = cirq .CircuitOperation (
1097- middle .freeze (), repetitions = 2 , use_repetition_ids = False
1113+ middle = cirq .Circuit (
1114+ cirq .CircuitOperation (inner .freeze (), repetitions = 2 , use_repetition_ids = True )
10981115 )
1116+ outer_subcircuit = cirq .CircuitOperation (middle .freeze (), repetitions = 2 )
10991117 circuit = cirq .Circuit (outer_subcircuit )
11001118 result = sim .run (circuit )
11011119 assert result .records ['0:a' ].shape == (1 , 2 , 1 )
@@ -1106,10 +1124,10 @@ def test_simulate_no_repetition_ids_outer(sim):
11061124def test_simulate_no_repetition_ids_inner (sim ):
11071125 q = cirq .LineQubit (0 )
11081126 inner = cirq .Circuit (cirq .measure (q , key = 'a' ))
1109- middle = cirq .Circuit (
1110- cirq .CircuitOperation (inner .freeze (), repetitions = 2 , use_repetition_ids = False )
1127+ middle = cirq .Circuit (cirq .CircuitOperation (inner .freeze (), repetitions = 2 ))
1128+ outer_subcircuit = cirq .CircuitOperation (
1129+ middle .freeze (), repetitions = 2 , use_repetition_ids = True
11111130 )
1112- outer_subcircuit = cirq .CircuitOperation (middle .freeze (), repetitions = 2 )
11131131 circuit = cirq .Circuit (outer_subcircuit )
11141132 result = sim .run (circuit )
11151133 assert result .records ['0:a' ].shape == (1 , 2 , 1 )
@@ -1124,7 +1142,6 @@ def test_repeat_until(sim):
11241142 cirq .X (q ),
11251143 cirq .CircuitOperation (
11261144 cirq .FrozenCircuit (cirq .X (q ), cirq .measure (q , key = key )),
1127- use_repetition_ids = False ,
11281145 repeat_until = cirq .KeyCondition (key ),
11291146 ),
11301147 )
@@ -1139,7 +1156,6 @@ def test_repeat_until_sympy(sim):
11391156 q1 , q2 = cirq .LineQubit .range (2 )
11401157 circuitop = cirq .CircuitOperation (
11411158 cirq .FrozenCircuit (cirq .X (q2 ), cirq .measure (q2 , key = 'b' )),
1142- use_repetition_ids = False ,
11431159 repeat_until = cirq .SympyCondition (sympy .Eq (sympy .Symbol ('a' ), sympy .Symbol ('b' ))),
11441160 )
11451161 c = cirq .Circuit (cirq .measure (q1 , key = 'a' ), circuitop )
@@ -1159,7 +1175,6 @@ def test_post_selection(sim):
11591175 c = cirq .Circuit (
11601176 cirq .CircuitOperation (
11611177 cirq .FrozenCircuit (cirq .X (q ) ** 0.2 , cirq .measure (q , key = key )),
1162- use_repetition_ids = False ,
11631178 repeat_until = cirq .KeyCondition (key ),
11641179 )
11651180 )
@@ -1175,14 +1190,13 @@ def test_repeat_until_diagram():
11751190 c = cirq .Circuit (
11761191 cirq .CircuitOperation (
11771192 cirq .FrozenCircuit (cirq .X (q ) ** 0.2 , cirq .measure (q , key = key )),
1178- use_repetition_ids = False ,
11791193 repeat_until = cirq .KeyCondition (key ),
11801194 )
11811195 )
11821196 cirq .testing .assert_has_diagram (
11831197 c ,
11841198 """
1185- 0: ───[ 0: ───X^0.2───M('m')─── ](no_rep_ids, until=m)───
1199+ 0: ───[ 0: ───X^0.2───M('m')─── ](until=m)───
11861200""" ,
11871201 use_unicode_characters = True ,
11881202 )
@@ -1199,7 +1213,6 @@ def test_repeat_until_error():
11991213 with pytest .raises (ValueError , match = 'Infinite loop' ):
12001214 cirq .CircuitOperation (
12011215 cirq .FrozenCircuit (cirq .measure (q , key = 'm' )),
1202- use_repetition_ids = False ,
12031216 repeat_until = cirq .KeyCondition (cirq .MeasurementKey ('a' )),
12041217 )
12051218
0 commit comments