qml.set_shots

set_shots(*args, shots=<object object>)[source]

Transform used to set or update a circuit’s shots.

Parameters:
  • qnode (QNode) – The QNode to transform. If not provided, set_shots can be used as a decorator directly.

  • shots (None or int or Sequence[int] or Sequence[tuple[int, int]] or pennylane.shots.Shots) – The number of shots (or a shots vector) that the transformed circuit will execute.

Returns:

The transformed QNode with updated shots, or a wrapper function if qnode is not provided.

Return type:

QNode or callable

There are three ways to specify shot values (see qml.measurements.Shots for more details):

  • The value None: analytic mode, no shots

  • A positive integer: a fixed number of shots

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

Examples

Set the number of shots as a decorator with either a positional or keyword argument:

@qml.set_shots(1_000)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit_sample():
    qml.RX(1.23, wires=0)
    return qml.sample(qml.Z(0))
>>> result = circuit_sample()
>>> result.shape
(1000,)

Set analytic mode as a decorator (positional argument):

@qml.set_shots(None)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit_expval():
    qml.RX(1.23, wires=0)
    return qml.expval(qml.Z(0))
>>> result = circuit_expval()
>>> np.allclose(result, np.cos(1.23))
True

The shots can be updated in-line for an existing circuit:

>>> new_circ = qml.set_shots(circuit_sample, shots=(4, 10)) # shot vector
>>> result = new_circ()
>>> a, b = result
>>> a.shape
(4,)
>>> b.shape
(10,)
>>> result 
(array([-1.,  1., -1.,  1.]), array([ 1.,  1.,  1., -1.,  1.,  1., -1., -1.,  1.,  1.]))

Contents

Using PennyLane

Release news

Development

API

Internals