Skip to content

Add Shots.bins() generator method #5433

Closed
@albi3ro

Description

@albi3ro

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:

  1. Add the bins method to Shots in pennylane/measurements/shots.py
  2. Add tests for the new method in tests/measurements/test_shots.py
  3. Add a changelog entry under Community Contributions
  4. Add your name to the list of contributors at the bottom of the changelog
  5. Make sure that all tests pass and the code adheres to our black and pylint standards

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions