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 operationsthat 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 aboveThat 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. Thesesimulations 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 whichtakes in a list or sweep of parameter resolver:run(circuit, param_resolver, repetitions) run_sweep(circuit, params, repetitions)
These methods returnTrialResult
s which contain both the measurementresults, 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 the densitymatrix of the simulation at the end of the simulation of the circuit.Note that if the circuit contains measurements then the density matrixis that result for those particular measurement results. For exampleif there is one measurement, then the simulation may result in themeasurement result for this measurement, and the density matrix willbe that conditional on that result. It will not be the density matrix formedby summing over the different measurements and their probabilities.The simulate 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 (seeDensityMatrixTrialResult
for details of this ordering). The initialstate can be either the full density matrix, the full wave function (forpure states), or an integer which represents the initial state of beingin a computational basis state for the binary representation of thatinteger. Similar to run methods, there are two simulate methods that runfor 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 performrepetitions. The result of these simulations is aDensityMatrixTrialResult
which contains, in addition to measurementresults and information about the parameters that were used in thesimulation, access to the density matrix via thedensity_matrix
method.If one wishes to perform simulations that have access to thedensity matrix as one steps through running the circuit there is a generatorwhich can be iterated over and each step is an object that gives accessto the density matrix. This stepping through aCircuit
is done on aMoment
byMoment
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.
