qml.QNode

QNode(func, device, *, interface='autograd', mutable=True, diff_method='best', properties=None)[source]

QNode constructor for creating QNodes.

When applied to a quantum function and device, converts it into a QNode instance.

Example:

>>> def circuit(x):
>>>     qml.RX(x, wires=0)
>>>     return qml.expval(qml.PauliZ(0))
>>> dev = qml.device("default.qubit", wires=1)
>>> qnode = QNode(circuit, dev)
Parameters
  • func (callable) – a quantum function

  • device (Device) – a PennyLane-compatible device

  • interface (str) –

    The interface that will be used for classical backpropagation. This affects the types of objects that can be passed to/returned from the QNode:

    • interface='autograd': Allows autograd to backpropogate through the QNode. The QNode accepts default Python types (floats, ints, lists) as well as NumPy array arguments, and returns NumPy arrays.

    • interface='torch': Allows PyTorch to backpropogate through the QNode.The QNode accepts and returns Torch tensors.

    • interface='tf': Allows TensorFlow in eager mode to backpropogate through the QNode.The QNode accepts and returns TensorFlow tf.Variable and tf.tensor objects.

    • None: The QNode accepts default Python types (floats, ints, lists) as well as NumPy array arguments, and returns NumPy arrays. It does not connect to any machine learning library automatically for backpropagation.

  • mutable (bool) – whether the QNode circuit is mutable

  • diff_method (str, None) –

    the method of differentiation to use in the created QNode.

    • "best": Best available method. Uses the device directly to compute the gradient if supported, otherwise will use the analytic parameter-shift rule where possible with finite-difference as a fallback.

    • "parameter-shift": Use the analytic parameter-shift rule where possible with finite-difference as a fallback.

    • "finite-diff": Uses numerical finite-differences.

    • None: a non-differentiable QNode is returned.

  • properties (dict[str->Any]) – additional keyword properties passed to the QNode