22
22
23
23
24
24
class CountingState (cirq .qis .QuantumStateRepresentation ):
25
- def __init__ (self , data , gate_count = 0 , measurement_count = 0 ):
25
+ def __init__ (self , data , gate_count = 0 , measurement_count = 0 , copy_count = 0 ):
26
26
self .data = data
27
27
self .gate_count = gate_count
28
28
self .measurement_count = measurement_count
29
+ self .copy_count = copy_count
29
30
30
31
def measure (
31
32
self , axes : Sequence [int ], seed : 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None
@@ -38,21 +39,22 @@ def kron(self: 'CountingState', other: 'CountingState') -> 'CountingState':
38
39
self .data ,
39
40
self .gate_count + other .gate_count ,
40
41
self .measurement_count + other .measurement_count ,
42
+ self .copy_count + other .copy_count ,
41
43
)
42
44
43
45
def factor (
44
46
self : 'CountingState' , axes : Sequence [int ], * , validate = True , atol = 1e-07
45
47
) -> Tuple ['CountingState' , 'CountingState' ]:
46
- return CountingState (self . data , self . gate_count , self . measurement_count ), CountingState (
47
- self .data
48
- )
48
+ return CountingState (
49
+ self .data , self . gate_count , self . measurement_count , self . copy_count
50
+ ), CountingState ( self . data )
49
51
50
52
def reindex (self : 'CountingState' , axes : Sequence [int ]) -> 'CountingState' :
51
- return self .copy ( )
53
+ return CountingState ( self .data , self . gate_count , self . measurement_count , self . copy_count )
52
54
53
55
def copy (self , deep_copy_buffers : bool = True ) -> 'CountingState' :
54
56
return CountingState (
55
- data = self .data , gate_count = self .gate_count , measurement_count = self .measurement_count
57
+ self .data , self .gate_count , self .measurement_count , self . copy_count + 1
56
58
)
57
59
58
60
@@ -79,6 +81,10 @@ def gate_count(self):
79
81
def measurement_count (self ):
80
82
return self ._state .measurement_count
81
83
84
+ @property
85
+ def copy_count (self ):
86
+ return self ._state .copy_count
87
+
82
88
83
89
class SplittableCountingSimulationState (CountingSimulationState ):
84
90
@property
@@ -171,19 +177,22 @@ def test_simulate_empty_circuit():
171
177
r = sim .simulate (cirq .Circuit ())
172
178
assert r ._final_simulator_state .gate_count == 0
173
179
assert r ._final_simulator_state .measurement_count == 0
180
+ assert r ._final_simulator_state .copy_count == 0
174
181
175
182
176
183
def test_simulate_one_gate_circuit ():
177
184
sim = CountingSimulator ()
178
185
r = sim .simulate (cirq .Circuit (cirq .X (q0 )))
179
186
assert r ._final_simulator_state .gate_count == 1
187
+ assert r ._final_simulator_state .copy_count == 0
180
188
181
189
182
190
def test_simulate_one_measurement_circuit ():
183
191
sim = CountingSimulator ()
184
192
r = sim .simulate (cirq .Circuit (cirq .measure (q0 )))
185
193
assert r ._final_simulator_state .gate_count == 0
186
194
assert r ._final_simulator_state .measurement_count == 1
195
+ assert r ._final_simulator_state .copy_count == 0
187
196
188
197
189
198
def test_empty_circuit_simulation_has_moment ():
@@ -196,13 +205,26 @@ def test_noise_applied():
196
205
sim = CountingSimulator (noise = cirq .X )
197
206
r = sim .simulate (cirq .Circuit (cirq .X (q0 )))
198
207
assert r ._final_simulator_state .gate_count == 2
208
+ assert r ._final_simulator_state .copy_count == 0
199
209
200
210
201
211
def test_noise_applied_measurement_gate ():
202
212
sim = CountingSimulator (noise = cirq .X )
203
213
r = sim .simulate (cirq .Circuit (cirq .measure (q0 )))
204
214
assert r ._final_simulator_state .gate_count == 1
205
215
assert r ._final_simulator_state .measurement_count == 1
216
+ assert r ._final_simulator_state .copy_count == 0
217
+
218
+
219
+ def test_parameterized_copies_all_but_last ():
220
+ sim = CountingSimulator ()
221
+ n = 4
222
+ rs = sim .simulate_sweep (cirq .Circuit (cirq .X (q0 ) ** 'a' ), [{'a' : i } for i in range (n )])
223
+ for i in range (n ):
224
+ r = rs [i ]
225
+ assert r ._final_simulator_state .gate_count == 1
226
+ assert r ._final_simulator_state .measurement_count == 0
227
+ assert r ._final_simulator_state .copy_count == 0 if i == n - 1 else 1
206
228
207
229
208
230
def test_cannot_act ():
@@ -382,14 +404,18 @@ def _has_unitary_(self):
382
404
op1 = TestOp (has_unitary = True )
383
405
op2 = TestOp (has_unitary = True )
384
406
circuit = cirq .Circuit (op1 , cirq .XPowGate (exponent = sympy .Symbol ('a' ))(q ), op2 )
385
- simulator .simulate_sweep (program = circuit , params = params )
407
+ rs = simulator .simulate_sweep (program = circuit , params = params )
408
+ assert rs [0 ]._final_simulator_state .copy_count == 1
409
+ assert rs [1 ]._final_simulator_state .copy_count == 0
386
410
assert op1 .count == 1
387
411
assert op2 .count == 2
388
412
389
413
op1 = TestOp (has_unitary = False )
390
414
op2 = TestOp (has_unitary = False )
391
415
circuit = cirq .Circuit (op1 , cirq .XPowGate (exponent = sympy .Symbol ('a' ))(q ), op2 )
392
- simulator .simulate_sweep (program = circuit , params = params )
416
+ rs = simulator .simulate_sweep (program = circuit , params = params )
417
+ assert rs [0 ]._final_simulator_state .copy_count == 1
418
+ assert rs [1 ]._final_simulator_state .copy_count == 0
393
419
assert op1 .count == 2
394
420
assert op2 .count == 2
395
421
0 commit comments