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)¶
A simulator for density matrices and noisy quantum circuits.This simulator can be applied on circuits that are made up of operationsthat have:* a
_mixture_method for a probabilistic combination of unitary gates.* a
_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.SupportsDecomposeor 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 return
TrialResults 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 (see
DensityMatrixTrialResultfor 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 a
DensityMatrixTrialResultwhich contains, in addition to measurementresults and information about the parameters that were used in thesimulation, access to the density matrix via the
density_matrixmethod.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 a
Circuitis done on a
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)¶
Density matrix simulator.
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.
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.
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.
Returns an iterator of StepResults for each moment simulated.
simulate_sweep(program, params[, …])
Simulates the supplied Circuit.