qml.measurements.CountsMP

class CountsMP(obs=None, wires=None, eigvals=None, id=None, all_outcomes=False)[source]

Bases: pennylane.measurements.measurements.SampleMeasurement

Measurement process that samples from the supplied observable and returns the number of counts for each sample.

Please refer to pennylane.counts() for detailed documentation.

Parameters
  • obs (Union[Operator, MeasurementValue]) – The observable that is to be measured as part of the measurement process. Not all measurement processes require observables (for example Probability); this argument is optional.

  • wires (Wires) – The wires the measurement process applies to. This can only be specified if an observable was not provided.

  • eigvals (array) – A flat array representing the eigenvalues of the measurement. This can only be specified if an observable was not provided.

  • id (str) – custom label given to a measurement instance, can be useful for some applications where the instance has to be identified

  • all_outcomes (bool) – determines whether the returned dict will contain only the observed outcomes (default), or whether it will display all possible outcomes for the system

has_decomposition

Whether or not the MeasurementProcess returns a defined decomposition when calling expand.

hash

returns an integer hash uniquely representing the measurement process

numeric_type

The Python numeric type of the measurement result.

raw_wires

The wires the measurement process acts on.

return_type

Measurement return type.

samples_computational_basis

Whether or not the MeasurementProcess measures in the computational basis.

wires

The wires the measurement process acts on.

has_decomposition

Whether or not the MeasurementProcess returns a defined decomposition when calling expand.

Type

Bool

hash

returns an integer hash uniquely representing the measurement process

Type

int

numeric_type

The Python numeric type of the measurement result.

Returns

The output numeric type; int, float or complex.

Return type

type

Raises

QuantumFunctionError – the return type of the measurement process is unrecognized and cannot deduce the numeric type

raw_wires

The wires the measurement process acts on.

For measurements involving more than one set of wires (such as mutual information), this is a list of the Wires objects. Otherwise, this is the same as wires()

return_type
samples_computational_basis

Whether or not the MeasurementProcess measures in the computational basis.

Type

Bool

wires

The wires the measurement process acts on.

This is the union of all the Wires objects of the measurement.

diagonalizing_gates()

Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.

eigvals()

Eigenvalues associated with the measurement process.

expand()

Expand the measurement of an observable to a unitary rotation and a measurement in the computational basis.

map_wires(wire_map)

Returns a copy of the current measurement process with its wires changed according to the given wire map.

process_counts(counts, wire_order)

Calculate the measurement given a counts histogram dictionary.

process_samples(samples, wire_order[, …])

Process the given samples.

queue([context])

Append the measurement process to an annotated queue.

shape([shots, num_device_wires])

Calculate the shape of the result object tensor.

simplify()

Reduce the depth of the observable to the minimum.

diagonalizing_gates()

Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.

Returns

the operations that diagonalize the observables

Return type

List[Operation]

eigvals()

Eigenvalues associated with the measurement process.

If the measurement process has an associated observable, the eigenvalues will correspond to this observable. Otherwise, they will be the eigenvalues provided when the measurement process was instantiated.

Note that the eigenvalues are not guaranteed to be in any particular order.

Example:

>>> m = MeasurementProcess(Expectation, obs=qml.X(1))
>>> m.eigvals()
array([1, -1])
Returns

eigvals representation

Return type

array

expand()

Expand the measurement of an observable to a unitary rotation and a measurement in the computational basis.

Returns

a quantum tape containing the operations required to diagonalize the observable

Return type

QuantumTape

Example:

Consider a measurement process consisting of the expectation value of an Hermitian observable:

>>> H = np.array([[1, 2], [2, 4]])
>>> obs = qml.Hermitian(H, wires=['a'])
>>> m = MeasurementProcess(Expectation, obs=obs)

Expanding this out:

>>> tape = m.expand()

We can see that the resulting tape has the qubit unitary applied, and a measurement process with no observable, but the eigenvalues specified:

>>> print(tape.operations)
[QubitUnitary(array([[-0.89442719,  0.4472136 ],
      [ 0.4472136 ,  0.89442719]]), wires=['a'])]
>>> print(tape.measurements[0].eigvals())
[0. 5.]
>>> print(tape.measurements[0].obs)
None
map_wires(wire_map)

Returns a copy of the current measurement process with its wires changed according to the given wire map.

Parameters

wire_map (dict) – dictionary containing the old wires as keys and the new wires as values

Returns

new measurement process

Return type

MeasurementProcess

process_counts(counts, wire_order)[source]

Calculate the measurement given a counts histogram dictionary.

Parameters
  • counts (dict) – a dictionary matching the format returned by CountsMP

  • wire_order (Wires) – the wire order used in producing the counts

Note that the input dictionary may only contain states with non-zero entries (all_outcomes=False).

process_samples(samples, wire_order, shot_range=None, bin_size=None)[source]

Process the given samples.

Parameters
  • samples (Sequence[complex]) – computational basis samples generated for all wires

  • wire_order (Wires) – wires determining the subspace that samples acts on

  • shot_range (tuple[int]) – 2-tuple of integers specifying the range of samples to use. If not specified, all samples are used.

  • bin_size (int) – Divides the shot range into bins of size bin_size, and returns the measurement statistic separately over each bin. If not provided, the entire shot range is treated as a single bin.

queue(context=<class 'pennylane.queuing.QueuingManager'>)

Append the measurement process to an annotated queue.

shape(shots=None, num_device_wires=0)

Calculate the shape of the result object tensor.

Parameters
  • shots (Optional[int]) – the number of shots used execute the circuit. None indicates an analytic simulation. Shot vectors are handled by calling this method multiple times.

  • num_device_wires (int) – The number of wires that will be used if the measurement is broadcasted across all available wires (len(mp.wires) == 0). If the device itself doesn’t provide a number of wires, the number of tape wires will be provided here instead:

Returns

An arbitrary length tuple of ints. May be an empty tuple.

Return type

tuple[int,..]

>>> qml.probs(wires=(0,1)).shape()
(4,)
>>> qml.sample(wires=(0,1)).shape(shots=50)
(50, 2)
>>> qml.state().shape(num_device_wires=4)
(16,)
>>> qml.expval(qml.Z(0)).shape()
()
simplify()

Reduce the depth of the observable to the minimum.

Returns

A measurement process with a simplified observable.

Return type

MeasurementProcess