pennylane
pennylane copied to clipboard
Implement `VarianceMP.process_counts`
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:
- Add the
process_counts
method toVarianceMP
- Add unit tests to pennylane/tests/measurements/test_varpy
- Pass black and pylint checks
- Add a changelog entry to pennylane/releases/changelog-dev.md under Improvements. Don't forget to add your name to the list of contributors at the bottom :)