Description
qml.measurements.Shots
is our class for keeping track of how many shots to use in calculating measurements.
One of the reasons we need a class like this is to better interact with and support shot vectors. A shot vector is when the same measurements as calculated multiple times using subsets of the total shots. For example, shots=(1, 10, 100, 100)
requests that measurements are done with 1
shot, 10
shots, 100
shots, and 100
shots again:
@qml.qnode(qml.device('default.qubit'))
def circuit():
return qml.counts(wires=0)
circuit(shots=(1, 10, 100,100))
({'0': tensor(1, requires_grad=True)},
{'0': tensor(10, requires_grad=True)},
{'0': tensor(100, requires_grad=True)},
{'0': tensor(100, requires_grad=True)})
If we calculated 211 total shots, measurement 1 would be calculated from shots 0:1
, measurement 2 from 1:11
, measurement 3 from 11:111
, and 3 from 111:211
.
This task is to add a property that makes it easy and convenient to access these ranges as "bins", a tuple of (lower_bound, upper_bound)
.
For example:
>>> shots = Shots((1, 10, 100, 100))
>>> shots.bins()
<generator object <genexpr> at 0x3095a5630>
>>> list(shots.bins())
[(0,1), (1,11), (11,111), (111,211)]
>>> for bin in shots.bins():
... print(bin)
(0,1)
(1,11)
(11,111)
(111,211)
To complete this task, the contributor should:
- Add the
bins
method toShots
inpennylane/measurements/shots.py
- Add tests for the new method in
tests/measurements/test_shots.py
- Add a changelog entry under
Community Contributions
- Add your name to the list of contributors at the bottom of the changelog
- Make sure that all tests pass and the code adheres to our black and pylint standards