# cirq.Simulator¶

class cirq.Simulator(*, dtype=<class 'numpy.complex64'>)[source]

A sparse matrix wave function simulator that uses numpy.

This simulator can be applied on circuits that are made up of operations
that have a _unitary_ method, or _has_unitary_ and
_apply_unitary_, _mixture_ methods, are measurements, or support a
_decompose_ method that returns operations satisfying these same
conditions. That is to say, the operations should follow the
cirq.SupportsApplyUnitary protocol, the cirq.SupportsUnitary protocol,
the cirq.SupportsMixture protocol, or the cirq.CompositeOperation
protocol. It is also permitted for the circuit to contain measurements
which are operations that support cirq.SupportsChannel and
cirq.SupportsMeasurementKey

This simulator supports three types of simulation.

Run simulations which mimic running on actual quantum hardware. These
simulations 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 which
takes 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 is
greater than one and all measurements in the circuit are terminal (at the
end of the circuit). These methods return TrialResults which contain both
the measurement results, but also the parameters used for the parameterized
circuit operations. The initial state of a run is always the all 0s state
in the computational basis.
By contrast the simulate methods of the simulator give access to the
wave 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: a
qubit order and an initial state. The qubit order is necessary because an
ordering must be chosen for the kronecker product (see
SparseSimulationTrialResult for details of this ordering). The initial
state can be either the full wave function, or an integer which represents
the initial state of being in a computational basis state for the binary
representation of that integer. Similar to run methods, there are two
simulate 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.

Finally, one can compute the values of displays (instances of SamplesDisplay or WaveFunctionDisplay) in the circuit:

compute_displays(circuit, param_resolver, qubit_order, initial_state)

compute_displays_sweep(circuit, params, qubit_order, initial_state)

The result of computing display values is stored in a ComputeDisplaysResult.

See Simulator for the definitions of the supported methods.

__init__(*, dtype=<class 'numpy.complex64'>)[source]

A sparse matrix simulator.

Parameters: dtype – The numpy.dtype used by the simulation. One of or numpy.complex128 (numpy.complex64) –

Methods

 compute_displays(program, …) Computes displays in the supplied Circuit or Schedule. compute_displays_sweep(program, …) Computes displays in the supplied Circuit or Schedule. compute_samples_displays(program, …) Computes SamplesDisplays in the supplied Circuit or Schedule. compute_samples_displays_sweep(program, …) Computes SamplesDisplays in the supplied Circuit or Schedule. run(program, …) Samples from the given Circuit or Schedule. run_sweep(program, …) Runs the supplied Circuit or Schedule, mimicking quantum hardware. simulate(program, …) Simulates the supplied Circuit or Schedule. simulate_moment_steps(circuit, …) Returns an iterator of StepResults for each moment simulated. simulate_sweep(program, …) Simulates the supplied Circuit or Schedule.