/
simulator.py
997 lines (854 loc) · 40.9 KB
/
simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Abstract base classes for different types of simulators.
Simulator types include:
SimulatesSamples: mimics the interface of quantum hardware.
SimulatesAmplitudes: computes amplitudes of desired bitstrings in the
final state of the simulation.
SimulatesFinalState: allows access to the final state of the simulation.
SimulatesIntermediateState: allows for access to the state of the simulation
as the simulation iterates through the moments of a cirq.
"""
import abc
import collections
from typing import (
Any,
Callable,
cast,
Dict,
Generic,
Iterator,
List,
Mapping,
Sequence,
Set,
Tuple,
TYPE_CHECKING,
TypeVar,
Union,
)
import numpy as np
from cirq import circuits, ops, protocols, study, value, work
from cirq.sim.simulation_state_base import SimulationStateBase
if TYPE_CHECKING:
import cirq
TStepResult = TypeVar('TStepResult', bound='StepResult')
TSimulationTrialResult = TypeVar('TSimulationTrialResult', bound='SimulationTrialResult')
TSimulatorState = TypeVar('TSimulatorState', bound=Any)
class SimulatesSamples(work.Sampler, metaclass=abc.ABCMeta):
"""Simulator that mimics running on quantum hardware.
Implementors of this interface should implement the _run method.
"""
def run_sweep(
self, program: 'cirq.AbstractCircuit', params: 'cirq.Sweepable', repetitions: int = 1
) -> Sequence['cirq.Result']:
return list(self.run_sweep_iter(program, params, repetitions))
def run_sweep_iter(
self, program: 'cirq.AbstractCircuit', params: 'cirq.Sweepable', repetitions: int = 1
) -> Iterator['cirq.Result']:
"""Runs the supplied Circuit, mimicking quantum hardware.
In contrast to run, this allows for sweeping over different parameter
values.
Args:
program: The circuit to simulate.
params: Parameters to run with the program.
repetitions: The number of repetitions to simulate.
Returns:
Result list for this run; one for each possible parameter
resolver.
Raises:
ValueError: If the circuit has no measurements.
"""
if not program.has_measurements():
raise ValueError("Circuit has no measurements to sample.")
for param_resolver in study.to_resolvers(params):
records = {}
if repetitions == 0:
for _, op, _ in program.findall_operations_with_gate_type(ops.MeasurementGate):
records[protocols.measurement_key_name(op)] = np.empty([0, 1, 1])
else:
records = self._run(
circuit=program, param_resolver=param_resolver, repetitions=repetitions
)
yield study.ResultDict(params=param_resolver, records=records)
@abc.abstractmethod
def _run(
self,
circuit: 'cirq.AbstractCircuit',
param_resolver: 'cirq.ParamResolver',
repetitions: int,
) -> Dict[str, np.ndarray]:
"""Run a simulation, mimicking quantum hardware.
Args:
circuit: The circuit to simulate.
param_resolver: Parameters to run with the program.
repetitions: Number of times to repeat the run. It is expected that
this is validated greater than zero before calling this method.
Returns:
A dictionary from measurement gate key to measurement
results. Measurement results are stored in a 3-dimensional
numpy array, the first dimension corresponding to the repetition.
the second to the instance of that key in the circuit, and the
third to the actual boolean measurement results (ordered by the
qubits being measured.)
"""
raise NotImplementedError()
class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
"""Simulator that computes final amplitudes of given bitstrings.
Given a circuit and a list of bitstrings, computes the amplitudes
of the given bitstrings in the state obtained by applying the circuit
to the all zeros state. Implementors of this interface should implement
the compute_amplitudes_sweep_iter method.
"""
def compute_amplitudes(
self,
program: 'cirq.AbstractCircuit',
bitstrings: Sequence[int],
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
) -> Sequence[complex]:
"""Computes the desired amplitudes.
The initial state is assumed to be the all zeros state.
Args:
program: The circuit to simulate.
bitstrings: The bitstrings whose amplitudes are desired, input
as an integer array where each integer is formed from measured
qubit values according to `qubit_order` from most to least
significant qubit, i.e. in big-endian ordering. If inputting
a binary literal add the prefix 0b or 0B.
For example: 0010 can be input as 0b0010, 0B0010, 2, 0x2, etc.
param_resolver: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
Returns:
List of amplitudes.
"""
return self.compute_amplitudes_sweep(
program, bitstrings, study.ParamResolver(param_resolver), qubit_order
)[0]
def compute_amplitudes_sweep(
self,
program: 'cirq.AbstractCircuit',
bitstrings: Sequence[int],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
) -> Sequence[Sequence[complex]]:
"""Wraps computed amplitudes in a list.
Prefer overriding `compute_amplitudes_sweep_iter`.
"""
return list(self.compute_amplitudes_sweep_iter(program, bitstrings, params, qubit_order))
def _compute_amplitudes_sweep_to_iter(
self,
program: 'cirq.AbstractCircuit',
bitstrings: Sequence[int],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
) -> Iterator[Sequence[complex]]:
if type(self).compute_amplitudes_sweep == SimulatesAmplitudes.compute_amplitudes_sweep:
raise RecursionError(
"Must define either compute_amplitudes_sweep or compute_amplitudes_sweep_iter."
)
yield from self.compute_amplitudes_sweep(program, bitstrings, params, qubit_order)
@value.alternative(
requires='compute_amplitudes_sweep', implementation=_compute_amplitudes_sweep_to_iter
)
def compute_amplitudes_sweep_iter(
self,
program: 'cirq.AbstractCircuit',
bitstrings: Sequence[int],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
) -> Iterator[Sequence[complex]]:
"""Computes the desired amplitudes.
The initial state is assumed to be the all zeros state.
Args:
program: The circuit to simulate.
bitstrings: The bitstrings whose amplitudes are desired, input
as an integer array where each integer is formed from measured
qubit values according to `qubit_order` from most to least
significant qubit, i.e. in big-endian ordering. If inputting
a binary literal add the prefix 0b or 0B.
For example: 0010 can be input as 0b0010, 0B0010, 2, 0x2, etc.
params: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
Returns:
An Iterator over lists of amplitudes. The outer dimension indexes
the circuit parameters and the inner dimension indexes bitstrings.
"""
raise NotImplementedError()
def sample_from_amplitudes(
self,
circuit: 'cirq.AbstractCircuit',
param_resolver: 'cirq.ParamResolver',
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE',
repetitions: int = 1,
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
) -> Dict[int, int]:
"""Uses amplitude simulation to sample from the given circuit.
This implements the algorithm outlined by Bravyi, Gosset, and Liu in
https://arxiv.org/abs/2112.08499 to more efficiently calculate samples
given an amplitude-based simulator.
Simulators which also implement SimulatesSamples or SimulatesFullState
should prefer `run()` or `simulate()`, respectively, as this method
only accelerates sampling for amplitude-based simulators.
Args:
circuit: The circuit to simulate.
param_resolver: Parameters to run with the program.
seed: Random state to use as a seed. This must be provided
manually - if the simulator has its own seed, it will not be
used unless it is passed as this argument.
repetitions: The number of repetitions to simulate.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
Returns:
A dict of bitstrings sampled from the final state of `circuit` to
the number of occurrences of that bitstring.
Raises:
ValueError: if 'circuit' has non-unitary elements, as differences
in behavior between sampling steps break this algorithm.
"""
prng = value.parse_random_state(seed)
qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(circuit.all_qubits())
base_circuit = circuits.Circuit(ops.I(q) for q in qubits) + circuit.unfreeze()
qmap = {q: i for i, q in enumerate(qubits)}
current_samples = {(0,) * len(qubits): repetitions}
solved_circuit = protocols.resolve_parameters(base_circuit, param_resolver)
if not protocols.has_unitary(solved_circuit):
raise ValueError("sample_from_amplitudes does not support non-unitary behavior.")
if protocols.is_measurement(solved_circuit):
raise ValueError("sample_from_amplitudes does not support intermediate measurement.")
for m_id, moment in enumerate(solved_circuit[1:]):
circuit_prefix = solved_circuit[: m_id + 1]
for t, op in enumerate(moment.operations):
new_samples: Dict[Tuple[int, ...], int] = collections.defaultdict(int)
qubit_indices = {qmap[q] for q in op.qubits}
subcircuit = circuit_prefix + circuits.Moment(moment.operations[: t + 1])
for current_sample, count in current_samples.items():
sample_set = [current_sample]
for idx in qubit_indices:
sample_set = [
target[:idx] + (result,) + target[idx + 1 :]
for target in sample_set
for result in [0, 1]
]
bitstrings = [int(''.join(map(str, sample)), base=2) for sample in sample_set]
amps = self.compute_amplitudes(subcircuit, bitstrings, qubit_order=qubit_order)
weights = np.abs(np.square(np.array(amps))).astype(np.float64)
weights /= np.linalg.norm(weights, 1)
subsample = prng.choice(len(sample_set), p=weights, size=count)
for sample_index in subsample:
new_samples[sample_set[sample_index]] += 1
current_samples = new_samples
return {int(''.join(map(str, k)), base=2): v for k, v in current_samples.items()}
class SimulatesExpectationValues(metaclass=value.ABCMetaImplementAnyOneOf):
"""Simulator that computes exact expectation values of observables.
Given a circuit and an observable map, computes exact (to float precision)
expectation values for each observable at the end of the circuit.
Implementors of this interface should implement the
simulate_expectation_values_sweep_iter method.
"""
def simulate_expectation_values(
self,
program: 'cirq.AbstractCircuit',
observables: Union['cirq.PauliSumLike', List['cirq.PauliSumLike']],
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
permit_terminal_measurements: bool = False,
) -> List[float]:
"""Simulates the supplied circuit and calculates exact expectation
values for the given observables on its final state.
This method has no perfect analogy in hardware. Instead compare with
Sampler.sample_expectation_values, which calculates estimated
expectation values by sampling multiple times.
Args:
program: The circuit to simulate.
observables: An observable or list of observables.
param_resolver: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
permit_terminal_measurements: If the provided circuit ends with
measurement(s), this method will generate an error unless this
is set to True. This is meant to prevent measurements from
ruining expectation value calculations.
Returns:
A list of expectation values, with the value at index `n`
corresponding to `observables[n]` from the input.
Raises:
ValueError if 'program' has terminal measurement(s) and
'permit_terminal_measurements' is False.
"""
return self.simulate_expectation_values_sweep(
program,
observables,
study.ParamResolver(param_resolver),
qubit_order,
initial_state,
permit_terminal_measurements,
)[0]
def simulate_expectation_values_sweep(
self,
program: 'cirq.AbstractCircuit',
observables: Union['cirq.PauliSumLike', List['cirq.PauliSumLike']],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
permit_terminal_measurements: bool = False,
) -> List[List[float]]:
"""Wraps computed expectation values in a list.
Prefer overriding `simulate_expectation_values_sweep_iter`.
"""
return list(
self.simulate_expectation_values_sweep_iter(
program,
observables,
params,
qubit_order,
initial_state,
permit_terminal_measurements,
)
)
def _simulate_expectation_values_sweep_to_iter(
self,
program: 'cirq.AbstractCircuit',
observables: Union['cirq.PauliSumLike', List['cirq.PauliSumLike']],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
permit_terminal_measurements: bool = False,
) -> Iterator[List[float]]:
if (
type(self).simulate_expectation_values_sweep
== SimulatesExpectationValues.simulate_expectation_values_sweep
):
raise RecursionError(
"Must define either simulate_expectation_values_sweep or "
"simulate_expectation_values_sweep_iter."
)
yield from self.simulate_expectation_values_sweep(
program, observables, params, qubit_order, initial_state, permit_terminal_measurements
)
@value.alternative(
requires='simulate_expectation_values_sweep',
implementation=_simulate_expectation_values_sweep_to_iter,
)
def simulate_expectation_values_sweep_iter(
self,
program: 'cirq.AbstractCircuit',
observables: Union['cirq.PauliSumLike', List['cirq.PauliSumLike']],
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
permit_terminal_measurements: bool = False,
) -> Iterator[List[float]]:
"""Simulates the supplied circuit and calculates exact expectation
values for the given observables on its final state, sweeping over the
given params.
This method has no perfect analogy in hardware. Instead compare with
Sampler.sample_expectation_values, which calculates estimated
expectation values by sampling multiple times.
Args:
program: The circuit to simulate.
observables: An observable or list of observables.
params: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
permit_terminal_measurements: If the provided circuit ends in a
measurement, this method will generate an error unless this
is set to True. This is meant to prevent measurements from
ruining expectation value calculations.
Returns:
An Iterator over expectation-value lists. The outer index determines
the sweep, and the inner index determines the observable. For
instance, results[1][3] would select the fourth observable measured
in the second sweep.
Raises:
ValueError if 'program' has terminal measurement(s) and
'permit_terminal_measurements' is False.
"""
raise NotImplementedError
class SimulatesFinalState(
Generic[TSimulationTrialResult], metaclass=value.ABCMetaImplementAnyOneOf
):
"""Simulator that allows access to the simulator's final state.
Implementors of this interface should implement the simulate_sweep_iter
method. This simulator only returns the state of the quantum system
for the final step of a simulation. This simulator state may be a state
vector, the density matrix, or another representation, depending on the
implementation. For simulators that also allow stepping through
a circuit see `SimulatesIntermediateState`.
"""
def simulate(
self,
program: 'cirq.AbstractCircuit',
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> TSimulationTrialResult:
"""Simulates the supplied Circuit.
This method returns a result which allows access to the entire
simulator's final state.
Args:
program: The circuit to simulate.
param_resolver: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
Returns:
SimulationTrialResults for the simulation. Includes the final state.
"""
return self.simulate_sweep(
program, study.ParamResolver(param_resolver), qubit_order, initial_state
)[0]
def simulate_sweep(
self,
program: 'cirq.AbstractCircuit',
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> List[TSimulationTrialResult]:
"""Wraps computed states in a list.
Prefer overriding `simulate_sweep_iter`.
"""
return list(self.simulate_sweep_iter(program, params, qubit_order, initial_state))
def _simulate_sweep_to_iter(
self,
program: 'cirq.AbstractCircuit',
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> Iterator[TSimulationTrialResult]:
if type(self).simulate_sweep == SimulatesFinalState.simulate_sweep:
raise RecursionError("Must define either simulate_sweep or simulate_sweep_iter.")
yield from self.simulate_sweep(program, params, qubit_order, initial_state)
@value.alternative(requires='simulate_sweep', implementation=_simulate_sweep_to_iter)
def simulate_sweep_iter(
self,
program: 'cirq.AbstractCircuit',
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> Iterator[TSimulationTrialResult]:
"""Simulates the supplied Circuit.
This method returns a result which allows access to the entire final
simulator state. In contrast to simulate, this allows for sweeping
over different parameter values.
Args:
program: The circuit to simulate.
params: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
Returns:
Iterator over SimulationTrialResults for this run, one for each
possible parameter resolver.
"""
raise NotImplementedError()
class SimulatesIntermediateState(
Generic[TStepResult, TSimulationTrialResult, TSimulatorState],
SimulatesFinalState[TSimulationTrialResult],
metaclass=abc.ABCMeta,
):
"""A SimulatesFinalState that simulates a circuit by moments.
Whereas a general SimulatesFinalState may return the entire simulator
state at the end of a circuit, a SimulatesIntermediateState can
simulate stepping through the moments of a circuit.
Implementors of this interface should implement the _core_iterator
method.
Note that state here refers to simulator state, which is not necessarily
a state vector.
"""
def simulate_sweep_iter(
self,
program: 'cirq.AbstractCircuit',
params: 'cirq.Sweepable',
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> Iterator[TSimulationTrialResult]:
"""Simulates the supplied Circuit.
This method returns a result which allows access to the entire
state vector. In contrast to simulate, this allows for sweeping
over different parameter values.
Args:
program: The circuit to simulate.
params: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. This can be
either a raw state or an `SimulationStateBase`. The form of the
raw state depends on the simulation implementation. See
documentation of the implementing class for details.
Returns:
List of SimulationTrialResults for this run, one for each
possible parameter resolver.
"""
qubit_order = ops.QubitOrder.as_qubit_order(qubit_order)
resolvers = list(study.to_resolvers(params))
for i, param_resolver in enumerate(resolvers):
state = (
initial_state.copy()
if isinstance(initial_state, SimulationStateBase) and i < len(resolvers) - 1
else initial_state
)
all_step_results = self.simulate_moment_steps(
program, param_resolver, qubit_order, state
)
measurements: Dict[str, np.ndarray] = {}
for step_result in all_step_results:
for k, v in step_result.measurements.items():
measurements[k] = np.array(v, dtype=np.uint8)
yield self._create_simulator_trial_result(
params=param_resolver,
measurements=measurements,
final_simulator_state=step_result._simulator_state(),
)
def simulate_moment_steps(
self,
circuit: 'cirq.AbstractCircuit',
param_resolver: 'cirq.ParamResolverOrSimilarType' = None,
qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
initial_state: Any = None,
) -> Iterator[TStepResult]:
"""Returns an iterator of StepResults for each moment simulated.
If the circuit being simulated is empty, a single step result should
be returned with the state being set to the initial state.
Args:
circuit: The Circuit to simulate.
param_resolver: A ParamResolver for determining values of Symbols.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. This can be
either a raw state or a `TSimulationState`. The form of the
raw state depends on the simulation implementation. See
documentation of the implementing class for details.
Returns:
Iterator that steps through the simulation, simulating each
moment and returning a StepResult for each moment.
"""
param_resolver = study.ParamResolver(param_resolver)
resolved_circuit = protocols.resolve_parameters(circuit, param_resolver)
check_all_resolved(resolved_circuit)
actual_initial_state = 0 if initial_state is None else initial_state
qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(circuit.all_qubits())
return self._base_iterator(resolved_circuit, qubits, actual_initial_state)
@abc.abstractmethod
def _base_iterator(
self, circuit: 'cirq.AbstractCircuit', qubits: Tuple['cirq.Qid', ...], initial_state: Any
) -> Iterator[TStepResult]:
"""Iterator over StepResult from Moments of a Circuit.
Args:
circuit: The circuit to simulate.
qubits: Specifies the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
Yields:
StepResults from simulating a Moment of the Circuit.
"""
@abc.abstractmethod
def _create_simulator_trial_result(
self,
params: 'cirq.ParamResolver',
measurements: Dict[str, np.ndarray],
final_simulator_state: TSimulatorState,
) -> TSimulationTrialResult:
"""This method can be implemented to create a trial result.
Args:
params: The ParamResolver for this trial.
measurements: The measurement results for this trial.
final_simulator_state: The final state of the simulation.
Returns:
The SimulationTrialResult.
"""
raise NotImplementedError()
class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta):
"""Results of a step of a SimulatesIntermediateState.
Attributes:
measurements: A dictionary from measurement gate key to measurement
results, ordered by the qubits that the measurement operates on.
"""
def __init__(self, sim_state: TSimulatorState) -> None:
self._sim_state = sim_state
self._measurements = sim_state.log_of_measurement_results
@property
def measurements(self) -> Mapping[str, Sequence[int]]:
return self._measurements
def _simulator_state(self) -> TSimulatorState:
"""Returns the simulator state of the simulator after this step.
This method starts with an underscore to indicate that it is private.
To access public state, see public methods on StepResult.
The form of the simulator_state depends on the implementation of the
simulation,see documentation for the implementing class for the form of
details.
"""
return self._sim_state
@abc.abstractmethod
def sample(
self,
qubits: List['cirq.Qid'],
repetitions: int = 1,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> np.ndarray:
"""Samples from the system at this point in the computation.
Note that this does not collapse the state vector.
Args:
qubits: The qubits to be sampled in an order that influence the
returned measurement results.
repetitions: The number of samples to take.
seed: A seed for the pseudorandom number generator.
Returns:
Measurement results with True corresponding to the ``|1⟩`` state.
The outer list is for repetitions, and the inner corresponds to
measurements ordered by the supplied qubits. These lists
are wrapped as a numpy ndarray.
"""
raise NotImplementedError()
def sample_measurement_ops(
self,
measurement_ops: List['cirq.GateOperation'],
repetitions: int = 1,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
*,
_allow_repeated=False,
) -> Dict[str, np.ndarray]:
"""Samples from the system at this point in the computation.
Note that this does not collapse the state vector.
In contrast to `sample` which samples qubits, this takes a list of
`cirq.GateOperation` instances whose gates are `cirq.MeasurementGate`
instances and then returns a mapping from the key in the measurement
gate to the resulting bit strings. Different measurement operations must
not act on the same qubits.
Args:
measurement_ops: `GateOperation` instances whose gates are
`MeasurementGate` instances to be sampled form.
repetitions: The number of samples to take.
seed: A seed for the pseudorandom number generator.
_allow_repeated: If True, adds extra dimension to the result,
corresponding to the number of times a key is repeated.
Returns: A dictionary from measurement gate key to measurement
results. Measurement results are stored in a 2-dimensional
numpy array, the first dimension corresponding to the repetition
and the second to the actual boolean measurement results (ordered
by the qubits being measured.)
Raises:
ValueError: If the operation's gates are not `MeasurementGate`
instances or a qubit is acted upon multiple times by different
operations from `measurement_ops`.
"""
# Sanity checks.
for op in measurement_ops:
gate = op.gate
if not isinstance(gate, ops.MeasurementGate):
raise ValueError(f'{op.gate} was not a MeasurementGate')
result = collections.Counter(
key for op in measurement_ops for key in protocols.measurement_key_names(op)
)
if result and not _allow_repeated:
duplicates = [k for k, v in result.most_common() if v > 1]
if duplicates:
raise ValueError(f"Measurement key {','.join(duplicates)} repeated")
# Find measured qubits, ensuring a consistent ordering.
measured_qubits = []
seen_qubits: Set[cirq.Qid] = set()
for op in measurement_ops:
for q in op.qubits:
if q not in seen_qubits:
seen_qubits.add(q)
measured_qubits.append(q)
# Perform whole-system sampling of the measured qubits.
indexed_sample = self.sample(measured_qubits, repetitions, seed=seed)
# Extract results for each measurement.
results: Dict[str, Any] = {}
qubits_to_index = {q: i for i, q in enumerate(measured_qubits)}
for op in measurement_ops:
gate = cast(ops.MeasurementGate, op.gate)
key = gate.key
out = np.zeros(shape=(repetitions, len(op.qubits)), dtype=np.int8)
inv_mask = gate.full_invert_mask()
cmap = gate.confusion_map
for i, q in enumerate(op.qubits):
out[:, i] = indexed_sample[:, qubits_to_index[q]]
if inv_mask[i]:
out[:, i] ^= out[:, i] < 2
self._confuse_results(out, op.qubits, cmap, seed)
if _allow_repeated:
if key not in results:
results[key] = []
results[key].append(out)
else:
results[gate.key] = out
return (
results
if not _allow_repeated
else {k: np.array(v).swapaxes(0, 1) for k, v in results.items()}
)
def _confuse_results(
self,
bits: np.ndarray,
qubits: Sequence['cirq.Qid'],
confusion_map: Dict[Tuple[int, ...], np.ndarray],
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> None:
"""Mutates `bits` using the confusion_map.
Compare with _confuse_result in cirq-core/cirq/sim/simulation_state.py.
"""
prng = value.parse_random_state(seed)
for rep in bits:
dims = [q.dimension for q in qubits]
for indices, confuser in confusion_map.items():
mat_dims = [dims[k] for k in indices]
row = value.big_endian_digits_to_int((rep[k] for k in indices), base=mat_dims)
new_val = prng.choice(len(confuser), p=confuser[row])
new_bits = value.big_endian_int_to_digits(new_val, base=mat_dims)
for i, k in enumerate(indices):
rep[k] = new_bits[i]
@value.value_equality(unhashable=True)
class SimulationTrialResult(Generic[TSimulatorState]):
"""Results of a simulation by a SimulatesFinalState.
Unlike `cirq.Result`, a SimulationTrialResult contains the final
simulator_state of the system. This simulator_state is dependent on the
simulation implementation and may be, for example, the state vector
or the density matrix of the system.
Attributes:
params: A ParamResolver of settings used for this result.
measurements: A dictionary from measurement gate key to measurement
results. Measurement results are a numpy ndarray of actual boolean
measurement results (ordered by the qubits acted on by the
measurement gate.)
"""
def __init__(
self,
params: 'cirq.ParamResolver',
measurements: Mapping[str, np.ndarray],
final_simulator_state: TSimulatorState,
) -> None:
"""Initializes the `SimulationTrialResult` class.
Args:
params: A ParamResolver of settings used for this result.
measurements: A mapping from measurement gate key to measurement
results. Measurement results are a numpy ndarray of actual
boolean measurement results (ordered by the qubits acted on by
the measurement gate.)
final_simulator_state: The final simulator state.
"""
self._params = params
self._measurements = measurements
self._final_simulator_state = final_simulator_state
@property
def params(self) -> 'cirq.ParamResolver':
return self._params
@property
def measurements(self) -> Mapping[str, np.ndarray]:
return self._measurements
def __repr__(self) -> str:
return (
f'cirq.SimulationTrialResult(params={self.params!r}, '
f'measurements={self.measurements!r}, '
f'final_simulator_state={self._final_simulator_state!r})'
)
def __str__(self) -> str:
def bitstring(vals):
separator = ' ' if np.max(vals) >= 10 else ''
return separator.join(str(int(v)) for v in vals)
results = sorted([(key, bitstring(val)) for key, val in self.measurements.items()])
if not results:
return '(no measurements)'
return ' '.join([f'{key}={val}' for key, val in results])
def _repr_pretty_(self, p: Any, cycle: bool) -> None:
"""Text output in Jupyter."""
if cycle:
# There should never be a cycle. This is just in case.
p.text('SimulationTrialResult(...)')
else:
p.text(str(self))
def _value_equality_values_(self) -> Any:
measurements = {k: v.tolist() for k, v in sorted(self.measurements.items())}
return self.params, measurements, self._final_simulator_state
@property
def qubit_map(self) -> Mapping['cirq.Qid', int]:
"""A map from Qid to index used to define the ordering of the basis in
the result.
"""
return self._final_simulator_state.qubit_map
def _qid_shape_(self) -> Tuple[int, ...]:
return _qubit_map_to_shape(self.qubit_map)
def _qubit_map_to_shape(qubit_map: Mapping['cirq.Qid', int]) -> Tuple[int, ...]:
qid_shape: List[int] = [-1] * len(qubit_map)
try:
for q, i in qubit_map.items():
qid_shape[i] = q.dimension
except IndexError:
raise ValueError(f'Invalid qubit_map. Qubit index out of bounds. Map is <{qubit_map!r}>.')
if -1 in qid_shape:
raise ValueError(f'Invalid qubit_map. Duplicate qubit index. Map is <{qubit_map!r}>.')
return tuple(qid_shape)
def check_all_resolved(circuit):
"""Raises if the circuit contains unresolved symbols."""
if protocols.is_parameterized(circuit):
unresolved = [op for moment in circuit for op in moment if protocols.is_parameterized(op)]
raise ValueError(
'Circuit contains ops whose symbols were not specified in '
f'parameter sweep. Ops: {unresolved}'
)
def split_into_matching_protocol_then_general(
circuit: 'cirq.AbstractCircuit', predicate: Callable[['cirq.Operation'], bool]
) -> Tuple['cirq.AbstractCircuit', 'cirq.AbstractCircuit']:
"""Splits the circuit into a matching prefix and non-matching suffix.
The splitting happens in a per-qubit fashion. A non-matching operation on
qubit A will cause later operations on A to be part of the non-matching
suffix, but later operations on other qubits will continue to be put into
the matching part (as long as those qubits have had no non-matching operation
up to that point).
"""
blocked_qubits: Set[cirq.Qid] = set()
matching_prefix = circuits.Circuit()
general_suffix = circuits.Circuit()
for moment in circuit:
matching_part = []
general_part = []
for op in moment:
qs = set(op.qubits)
if not predicate(op) or not qs.isdisjoint(blocked_qubits):
blocked_qubits |= qs
if qs.isdisjoint(blocked_qubits):
matching_part.append(op)
else:
general_part.append(op)
if matching_part:
matching_prefix.append(circuits.Moment(matching_part))
if general_part:
general_suffix.append(circuits.Moment(general_part))
return matching_prefix, general_suffix