qml.measurements.Shots

class Shots(shots=None)[source]

Bases: object

A data class that stores shot information.

Parameters

shots (Union[None, int, Sequence[int, Tuple[int, int]]]) – Raw shot information

Defining shots enables users to specify circuit executions, and the Shots class standardizes the internal representation of shots. There are three ways to specify shot values:

  • The value None

  • A positive integer

  • A sequence consisting of either positive integers or a tuple-pair of positive integers of the form (shots, copies)

The tuple-pair of the form (shots, copies) is represented internally by a NamedTuple called ShotCopies. The first value is the number of shots to execute, and the second value is the number of times to repeat a circuit with that number of shots.

The Shots class exposes two properties:

  • total_shots, the total number of shots to be executed

  • shot_vector, the tuple of ShotCopies to be executed

Instances of this class are static. If an instance is passed to the constructor, that same instance is returned. If an instance is constructed with a None value, total_shots will be None. This indicates analytic execution. A Shots object created with a None value is Falsy, while any other value results in a Truthy object:

>>> bool(Shots(None)), bool(Shots(1))
(False, True)

Examples

Example constructing a Shots instance with None:

>>> shots = Shots(None)
>>> shots.total_shots, shots.shot_vector
(None, ())

Example constructing a Shots instance with an int:

>>> shots = Shots(100)
>>> shots.total_shots, shots.shot_vector
(100, (ShotCopies(100 shots),))

Example constructing a Shots instance with another instance:

>>> shots = Shots(100)
>>> Shots(shots) is shots
True

Example constructing a Shots instance with a sequence of ints:

>>> shots = Shots([100, 200])
>>> shots.total_shots, shots.shot_vector
(300, (ShotCopies(100 shots x 1), ShotCopies(200 shots x 1)))

Example constructing a Shots instance with a sequence of tuple-pairs:

>>> shots = Shots(((100, 3), (200, 4),))
>>> shots.total_shots, shots.shot_vector
(1100, (ShotCopies(100 shots x 3), ShotCopies(200 shots x 4)))

Example constructing a Shots instance with a sequence of both ints and tuple-pairs. Note that the first stand-alone 100 gets absorbed into the subsequent tuple because the shot value matches:

>>> shots = Shots((10, 100, (100, 3), (200, 4),))
>>> shots.total_shots, shots.shot_vector
(1210, (ShotCopies(10 shots x 1), ShotCopies(100 shots x 4), ShotCopies(200 shots x 4)))

Example constructing a Shots instance by multiplying an existing one by an int or float:

>>> Shots(100) * 2
Shots(total_shots=200, shot_vector=(ShotCopies(200 shots x 1),))
>>> Shots([7, (100, 2)]) * 1.5
Shots(total_shots=310, shot_vector=(ShotCopies(10 shots x 1), ShotCopies(150 shots x 2)))

One should also note that specifying a single tuple of length 2 is considered two different shot values, and not a tuple-pair representing shots and copies to avoid special behaviour depending on the iterable type:

>>> shots = Shots((100, 2))
>>> shots.total_shots, shots.shot_vector
(102, (ShotCopies(100 shots x 1), ShotCopies(2 shots x 1)))
>>> shots = Shots(((100, 2),))
>>> shots.total_shots, shots.shot_vector
(200, (ShotCopies(100 shots x 2),))

has_partitioned_shots

Evaluates to True if this instance represents either multiple shot quantities, or the same shot quantity repeated multiple times.

num_copies

The total number of copies of any shot quantity.

shot_vector

The tuple of ShotCopies to be executed.

total_shots

The total number of shots to be executed.

has_partitioned_shots

Evaluates to True if this instance represents either multiple shot quantities, or the same shot quantity repeated multiple times.

Returns

whether shots are partitioned

Return type

bool

num_copies

The total number of copies of any shot quantity.

shot_vector = None

The tuple of ShotCopies to be executed. Each element is of the form (shots, copies).

total_shots = None

The total number of shots to be executed.

bins()

Yields

tuple -- A tuple containing the lower and upper bounds for each shot quantity in shot_vector.

bins()[source]
Yields

tuple – A tuple containing the lower and upper bounds for each shot quantity in shot_vector.

Example

>>> shots = Shots((1, 1, 2, 3))
>>> list(shots.bins())
[(0,1), (1,2), (2,4), (4,7)]