# cirq.DensityMatrixSimulator¶

class cirq.DensityMatrixSimulator(*, dtype: Type[numpy.number] = <class 'numpy.complex64'>, noise: cirq.NOISE_MODEL_LIKE = None, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None, ignore_measurement_results: bool = False)[source]

A simulator for density matrices and noisy quantum circuits.

This simulator can be applied on circuits that are made up of operations
that have:
* a _channel_ method
* a _mixture_ method for a probabilistic combination of unitary gates.
* a _unitary_ method
* a _has_unitary_ and _apply_unitary_ method.
* measurements
* a _decompose_ that eventually yields one of the above
That is, the circuit must have elements that follow on of the protocols:
* cirq.SupportsChannel
* cirq.SupportsMixture
* cirq.SupportsConsistentApplyUnitary
* cirq.SupportsUnitary
* cirq.SupportsDecompose
or is a measurement.

This simulator supports three types of simulation.

Run simulations which mimic running on actual quantum hardware. These
simulations do not give access to the density matrix (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)

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 density
matrix of the simulation at the end of the simulation of the circuit.
Note that if the circuit contains measurements then the density matrix
is that result for those particular measurement results. For example
if there is one measurement, then the simulation may result in the
measurement result for this measurement, and the density matrix will
be that conditional on that result. It will not be the density matrix formed
by summing over the different measurements and their probabilities.
The simulate 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
DensityMatrixTrialResult for details of this ordering). The initial
state can be either the full density matrix, the full wave function (for
pure states), 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 simulations 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
DensityMatrixTrialResult which contains, in addition to measurement
results and information about the parameters that were used in the
simulation, access to the density matrix via the density_matrix method.
If one wishes to perform simulations that have access to the
density matrix 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 density matrix. 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 density matrix via
# step_result.density_matrix()

__init__(*, dtype: Type[numpy.number] = <class 'numpy.complex64'>, noise: cirq.NOISE_MODEL_LIKE = None, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None, ignore_measurement_results: bool = False)[source]

Density matrix simulator.

Parameters
• dtype – The numpy.dtype used by the simulation. One of numpy.complex64 or numpy.complex128

• noise – A noise model to apply while simulating.

• seed – The random seed to use for this simulator.

• ignore_measurement_results

if True, then the simulation will treat measurement as dephasing instead of collapsing process.

Example: >>> (q0,) = cirq.LineQubit.range(1) >>> circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))

Default case (ignore_measurement_results = False): >>> simulator = cirq.DensityMatrixSimulator() >>> result = simulator.run(circuit)

The measurement result will be strictly one of 0 or 1.

In the other case: >>> simulator = cirq.DensityMatrixSimulator( … ignore_measurement_results = True) >>> result = simulator.run(circuit)

The measurement result will be the maximally mixed state with equal probability for 0 and 1.