qml.workflow.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 (positional argument):

@qml.set_shots(500)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit():
    qml.RX(1.23, wires=0)
    return qml.expval(qml.Z(0))

Set analytic mode as a decorator (positional argument):

@qml.set_shots(None)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit():
    qml.RX(1.23, wires=0)
    return qml.expval(qml.Z(0))

Set the number of shots as a decorator (keyword argument):

@qml.set_shots(shots=2)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit():
    qml.RX(1.23, wires=0)
    return qml.sample(qml.Z(0))

Set analytic mode as a decorator (keyword argument):

@qml.set_shots(shots=None)
@qml.qnode(qml.device("default.qubit", wires=1))
def circuit():
    qml.RX(1.23, wires=0)
    return qml.expval(qml.Z(0))

Run the circuit:

>>> circuit()
array([1., -1.])

Update the shots in-line for an existing circuit:

>>> new_circ = qml.set_shots(circuit, shots=(4, 10)) # shot vector
>>> new_circ()
(array([-1.,  1., -1.,  1.]), array([ 1.,  1.,  1., -1.,  1.,  1., -1., -1.,  1.,  1.]))

Set analytic mode in-line for an existing circuit:

>>> analytic_circ = qml.set_shots(circuit, shots=None)
>>> analytic_circ()
0.5403023058681398