2525 results = sim.run(circuit)
2626"""
2727
28- import functools
29- import math
30-
3128from collections import defaultdict , Iterable
32- from typing import Tuple # pylint: disable=unused-import
29+ import math
3330from typing import Dict , Iterator , List , Sequence , Union , cast
31+ from typing import Tuple # pylint: disable=unused-import
3432
33+ import functools
3534import numpy as np
3635
37- from cirq .circuits import Circuit , ExpandComposite
36+ from cirq .circuits import Circuit
3837from cirq .circuits .drop_empty_moments import DropEmptyMoments
39- from cirq .ops import raw_types
40- from cirq .schedules import Schedule
4138from cirq .google import xmon_gates
4239from cirq .google .convert_to_xmon_gates import ConvertToXmonGates
4340from cirq .google .sim .xmon_stepper import Stepper
41+ from cirq .ops import raw_types
42+ from cirq .schedules import Schedule
4443from cirq .study import ParamResolver , Sweep , Sweepable , TrialResult
4544
4645
@@ -85,7 +84,7 @@ class SimulatorTrialResult(TrialResult):
8584 Attributes:
8685 measurements: A dictionary from measurement gate key to measurement
8786 results ordered by the qubits acted upon by the measurement gate.
88- final_state : The final state (wave function) of the system after
87+ final_states : The final states (wave function) of the system after
8988 the trial finishes.
9089 """
9190
@@ -133,10 +132,10 @@ def run(
133132 a canonical ordering of the qubits. This canonical ordering
134133 is used to define the wave function.
135134 initial_state: If an int, the state is set to the computational
136- basis state corresponding corresponding to this state. Otherwise
137- if this is a np.ndarray it is the full initial state. In this
138- case it must be the correct size, be normalized (an L2 norm of
139- 1), and have a dtype of np.complex64.
135+ basis state corresponding corresponding to this state.
136+ Otherwise if this is a np.ndarray it is the full initial
137+ state. In this case it must be the correct size, be normalized
138+ (an L2 norm of 1), and have a dtype of np.complex64.
140139
141140 Returns:
142141 Results for this run.
@@ -164,10 +163,10 @@ def run_sweep(
164163 a canonical ordering of the qubits. This canonical ordering
165164 is used to define the wave function.
166165 initial_state: If an int, the state is set to the computational
167- basis state corresponding corresponding to this state. Otherwise
168- if this is a np.ndarray it is the full initial state. In this
169- case it must be the correct size, be normalized (an L2 norm of
170- 1), and have a dtype of np.complex64.
166+ basis state corresponding corresponding to this state.
167+ Otherwise if this is a np.ndarray it is the full initial state.
168+ In this case it must be the correct size, be normalized (an L2
169+ norm of 1), and have a dtype of np.complex64.
171170
172171 Returns:
173172 List of trial results for this run, one for each possible parameter
@@ -230,10 +229,10 @@ def moment_steps(
230229 a canonical ordering of the qubits. This canonical ordering
231230 is used to define the wave function.
232231 initial_state: If an int, the state is set to the computational
233- basis state corresponding corresponding to this state. Otherwise
234- if this is a np.ndarray it is the full initial state. In this
235- case it must be the correct size, be normalized (an L2 norm of
236- 1), and have a dtype of np.complex64.
232+ basis state corresponding corresponding to this state.
233+ Otherwise if this is a np.ndarray it is the full initial state.
234+ In this case it must be the correct size, be normalized (an L2
235+ norm of 1), and have a dtype of np.complex64.
237236 param_resolver: A ParamResolver for determining values of
238237 Symbols.
239238
@@ -285,21 +284,16 @@ def simulator_iterator(
285284 qubit_map = {q : i for i , q in enumerate (qubits )}
286285
287286 # TODO: Use one optimization pass.
288- expand = ExpandComposite ()
289- convert = ConvertToXmonGates (ignore_cast_failures = False )
290- drop = DropEmptyMoments ()
291-
292287 circuit_copy = Circuit (circuit .moments )
293- expand .optimize_circuit (circuit_copy )
294- convert .optimize_circuit (circuit_copy )
295- drop .optimize_circuit (circuit_copy )
288+ ConvertToXmonGates ().optimize_circuit (circuit_copy )
289+ DropEmptyMoments ().optimize_circuit (circuit_copy )
296290 validate_unique_measurement_keys (circuit_copy )
297291
298- with Stepper (
299- num_qubits = len ( qubits ) ,
300- num_prefix_qubits = options . num_prefix_qubits ,
301- initial_state = initial_state ,
302- min_qubits_before_shard = options . min_qubits_before_shard ) as stepper :
292+ with Stepper (num_qubits = len ( qubits ),
293+ num_prefix_qubits = options . num_prefix_qubits ,
294+ initial_state = initial_state ,
295+ min_qubits_before_shard = options . min_qubits_before_shard
296+ ) as stepper :
303297 for moment in circuit_copy .moments :
304298 measurements = defaultdict (list ) # type: Dict[str, List[bool]]
305299 phase_map = {} # type: Dict[Tuple[int, ...], float]
@@ -330,9 +324,8 @@ def simulator_iterator(
330324 result = not result
331325 measurements [gate .key ].append (result )
332326 else :
333- raise TypeError (
334- 'Gate %s is not a gate supported by the xmon simulator.'
335- % gate )
327+ raise TypeError ('{!r} is not supported by the '
328+ 'xmon simulator.' .format (gate ))
336329 stepper .simulate_phases (phase_map )
337330 yield StepResult (stepper , qubit_map , measurements )
338331
@@ -414,9 +407,9 @@ def set_state(self, state: Union[int, np.ndarray]):
414407 def merge (a : 'StepResult' , b : 'StepResult' ) -> 'StepResult' :
415408 """Merges measurement results of last_result into a new Result.
416409
417- The measurement results are merges such that measurements with duplicate
418- keys have the results of last_result before those of this objects
419- results.
410+ The measurement results are merges such that measurements with
411+ duplicate keys have the results of last_result before those of this
412+ objects' results.
420413
421414 Args:
422415 a: First result to merge.
@@ -425,7 +418,7 @@ def merge(a: 'StepResult', b: 'StepResult') -> 'StepResult':
425418 Returns:
426419 A new StepResult with merged measurements.
427420 """
428- new_measurements = {} # type: Dict[str, np.ndarray ]
421+ new_measurements = {} # type: Dict[str, list ]
429422 for d in [a .measurements , b .measurements ]:
430423 for key , results in d .items ():
431424 if key not in new_measurements :
0 commit comments