qml.set_shots

set_shots(qnode, shots=None)[source]

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

Parameters:
  • qnode (QNode) – The QNode to transform.

  • 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 the specified shots.

Return type:

QNode

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:

from functools import partial

@partial(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))

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.]))

Contents

Using PennyLane

Release news

Development

API

Internals