cirq.Simulator¶

class
cirq.
Simulator
(*, dtype: Type[numpy.number] = <class 'numpy.complex64'>, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None)[source]¶ A sparse matrix wave function simulator that uses numpy.
This simulator can be applied on circuits that are made up of operationsthat have a_unitary_
method, or_has_unitary_
and_apply_unitary_
,_mixture_
methods, are measurements, or support a_decompose_
method that returns operations satisfying these sameconditions. That is to say, the operations should follow thecirq.SupportsConsistentApplyUnitary
protocol, thecirq.SupportsUnitary
protocol, thecirq.SupportsMixture
protocol, or thecirq.CompositeOperation
protocol. It is also permitted for the circuitto contain measurements which are operations that supportcirq.SupportsChannel
andcirq.SupportsMeasurementKey
This simulator supports three types of simulation.
Run simulations which mimic running on actual quantum hardware. Thesesimulations do not give access to the wave function (like actual hardware).There are two variations of run methods, one which takes in a single(optional) way to resolve parameterized circuits, and a second whichtakes in a list or sweep of parameter resolver:run(circuit, param_resolver, repetitions) run_sweep(circuit, params, repetitions)
The simulation performs optimizations if the number of repetitions isgreater than one and all measurements in the circuit are terminal (at theend of the circuit). These methods returnTrialResult
s which contain boththe measurement results, but also the parameters used for the parameterizedcircuit operations. The initial state of a run is always the all 0s statein the computational basis.By contrast the simulate methods of the simulator give access to thewave function of the simulation at the end of the simulation of the circuit.These methods take in two parameters that the run methods do not: aqubit order and an initial state. The qubit order is necessary because anordering must be chosen for the kronecker product (seeSparseSimulationTrialResult
for details of this ordering). The initialstate can be either the full wave function, or an integer which representsthe initial state of being in a computational basis state for the binaryrepresentation of that integer. Similar to run methods, there are twosimulate methods that run for single runs or for sweeps across different Parameters
simulate (circuit, param_resolver, qubit_order, initial_state) –
simulate_sweep (circuit, params, qubit_order, initial_state) –
The simulate methods in contrast to the run methods do not perform repetitions. The result of these simulations is a SparseSimulationTrialResult which contains, in addition to measurement results and information about the parameters that were used in the simulation,access to the state via the state method and StateVectorMixin methods.
If one wishes to perform simulations that have access to the wave function as one steps through running the circuit there is a generator which can be iterated over and each step is an object that gives access to the wave function. This stepping through a Circuit is done on a Moment by Moment manner.
 simulate_moment_steps(circuit, param_resolver, qubit_order,
initial_state)
One can iterate over the moments via
 for step_result in simulate_moments(circuit):
# do something with the wave function via step_result.state
Note also that simulations can be stochastic, i.e. return different results for different runs. The first version of this occurs for measurements, where the results of the measurement are recorded. This can also occur when the circuit has mixtures of unitaries.
See Simulator for the definitions of the supported methods.

__init__
(*, dtype: Type[numpy.number] = <class 'numpy.complex64'>, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None)[source]¶ A sparse matrix simulator.
 Parameters
dtype – The numpy.dtype used by the simulation. One of numpy.complex64 or numpy.complex128.
seed – The random seed to use for this simulator.
Methods
compute_amplitudes
(program, bitstrings[, …])Computes the desired amplitudes.
compute_amplitudes_sweep
(program, …[, …])Computes the desired amplitudes.
run
(program[, param_resolver, repetitions])Samples from the given Circuit.
run_async
(program, *, repetitions)Asynchronously samples from the given Circuit.
run_sweep
(program, params[, repetitions])Runs the supplied Circuit, mimicking quantum hardware.
run_sweep_async
(program, params[, repetitions])Asynchronously sweeps and samples from the given Circuit.
sample
(program, *[, repetitions, params])Samples the given Circuit, producing a pandas data frame.
simulate
(program[, param_resolver, …])Simulates the supplied Circuit.
simulate_moment_steps
(circuit[, …])Returns an iterator of StepResults for each moment simulated.
simulate_sweep
(program, params[, …])Simulates the supplied Circuit.