diff --git a/doc/introduction/interfaces.rst b/doc/introduction/interfaces.rst index faf2fe6a777..81249ee206c 100644 --- a/doc/introduction/interfaces.rst +++ b/doc/introduction/interfaces.rst @@ -10,12 +10,121 @@ PennyLane offers seamless integration between classical and quantum computations circuits in PennyLane, compute :doc:`gradients of quantum circuits `, and connect them easily to the top scientific computing and machine learning libraries. +Training and interfaces +----------------------- + +The bridge between the quantum and classical worlds is provided in PennyLane via interfaces to +automatic differentiation libraries. +Currently, four libraries are supported: :doc:`NumPy `, :doc:`PyTorch +`, :doc:`JAX `, and :doc:`TensorFlow `. PennyLane makes +each of these libraries quantum-aware, allowing quantum circuits to be treated just +like any other operation. Any automatic differentiation framework can be chosen with any device. + +In PennyLane, an automatic differentiation framework is declared using the ``interface`` argument when creating +a :class:`QNode `, e.g., + +.. code-block:: python + + @qml.qnode(dev, interface="tf") + def my_quantum_circuit(...): + ... + +.. note:: + If no interface is specified, PennyLane will default to the NumPy interface (powered by the + `autograd `_ library). + +This will allow native numerical objects of the specified library (NumPy arrays, JAX arrays, Torch Tensors, +or TensorFlow Tensors) to be passed as parameters to the quantum circuit. It also makes +the gradients of the quantum circuit accessible to the classical library, enabling the +optimization of arbitrary hybrid circuits. + +When specifying an interface, the objects of the chosen framework are converted +into NumPy objects and are passed to a device in most cases. Exceptions include +cases when the devices support end-to-end computations in a framework. Such +devices may be referred to as backpropagation or passthru devices. + +See the links below for walkthroughs of each specific interface: + +.. raw:: html + + + + +In addition to the core automatic differentiation frameworks discussed above, +PennyLane also provides higher-level classes for converting QNodes into both Keras and ``torch.nn`` layers: + + +:html:`
` + +.. autosummary:: + + pennylane.qnn.KerasLayer + pennylane.qnn.TorchLayer + + +.. note:: + + QNodes that allow for automatic differentiation will always incur a small overhead on evaluation. + If you do not need to compute quantum gradients of a QNode, specifying ``interface=None`` will remove + this overhead and result in a slightly faster evaluation. However, gradients will no + longer be available. + + Gradients --------- +The interface between PennyLane and automatic differentiation libraries relies on PennyLane's ability +to compute or estimate gradients of quantum circuits. There are different strategies to do so, and they may +depend on the device used. + When creating a QNode, you can specify the :doc:`differentiation method -` that PennyLane should use whenever the gradient of -that QNode is requested. +` like this: .. code-block:: python @@ -127,109 +236,6 @@ support gradients of QNodes. For more details on available gradient transforms, as well as learning how to define your own gradient transform, please see the :mod:`qml.gradients ` documentation. -Training and interfaces ------------------------ - -The bridge between the quantum and classical worlds is provided in PennyLane via *interfaces*. -Currently, there are four built-in interfaces: :doc:`NumPy `, :doc:`PyTorch -`, :doc:`JAX `, and :doc:`TensorFlow `. These -interfaces make each of these libraries quantum-aware, allowing quantum circuits to be treated just -like any other operation. Any interface can be chosen with any device. - -In PennyLane, an interface is declared when creating a :class:`QNode `, e.g., - -.. code-block:: python - - @qml.qnode(dev, interface="tf") - def my_quantum_circuit(...): - ... - -.. note:: - If no interface is specified, PennyLane will default to the NumPy interface (powered by the - `autograd `_ library). - -This will allow native numerical objects of the specified library (NumPy arrays, Torch Tensors, -or TensorFlow Tensors) to be passed as parameters to the quantum circuit. It also makes -the gradients of the quantum circuit accessible to the classical library, enabling the -optimization of arbitrary hybrid circuits. - -When specifying an interface, the objects of the chosen framework are converted -into NumPy objects and are passed to a device in most cases. Exceptions include -cases when the devices support end-to-end computations in a framework. Such -devices may be referred to as backpropagation or passthru devices. - -See the links below for walkthroughs of each specific interface: - -.. raw:: html - - - - -In addition to the core interfaces discussed above, PennyLane also provides higher-level classes for -converting QNodes into both Keras and ``torch.nn`` layers: - - -:html:`
` - -.. autosummary:: - - pennylane.qnn.KerasLayer - pennylane.qnn.TorchLayer - - -.. note:: - - QNodes with an interface will always incur a small overhead on evaluation. If you do not - need to compute quantum gradients of a QNode, specifying ``interface=None`` will remove - this overhead and result in a slightly faster evaluation. However, gradients will no - longer be available. - :html:`
`