pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

Implement `VarianceMP.process_counts`

Open albi3ro opened this issue 1 year ago • 0 comments

Context

Currently, our sample-based measurement processes that inherit from SampleMeasurement implement a process_samples method. This method is capable of turning any samples (array of int64) into the required measurement result. This method is currently used internally by default.qubit.

@qml.qnode(qml.device('default.qubit'))
def circuit():
    qml.Hadamard(0)
    return qml.sample(wires=(0,1))

samples = circuit(shots=100)
qml.expval(qml.Z(0)).process_samples(samples, wire_order=qml.wires.Wires((0,1)))

But external devices and plugins may want to store sampling information in the form of a counts dictionary instead, like {'00': 45, '01': 55}. This representation is much more condensed and memory efficient. Because of this, we have recently added a SampleMeasurement.process_counts method, and implemented it for ProbabilityMP.

Implementation Details

The task is to implement VarianceMP.process_counts(counts, wire_order) in such a way that expectation values can be computed from a counts dictionary.

>>> counts = {'000': 100, '100': 100 }
>>> wire_order = qml.wires.Wires((0,1,2))
>>> qml.var(qml.Z(0)).process_counts(counts, wire_order)
1.0
>>> qml.var(qml.Z(1)).process_counts(counts, wire_order)
0.0

Note that the counts are assumed to be taken in the basis for the provided observable already, with any diagonalizing gates already applied.

Requirements

A completed PR should:

albi3ro avatar Feb 22 '24 14:02 albi3ro