Skip to content

Commit

Permalink
Add warning message to Gradients and training documentation about `Co…
Browse files Browse the repository at this point in the history
…mplexWarnings` (#6543)

**Context:**
- This PR addresses a recurring ComplexWarning issue that appears when
using differentiable workflows involving both complex and float types,
particularly during backpropagation. Users have reported this issue (see
an example
[here](https://discuss.pennylane.ai/t/casting-of-complex-to-float-when-using-phaseshift-in-circuit-for-qml/7439)),
and [internal
discussions](https://xanaduhq.slack.com/archives/C0743CNS9E3/p1730329623028289)
have concluded that while the warning is generally harmless, it can
still be confusing for users.

**Description of the Change:**
- A warning note has been added to the Gradients and training page in
the documentation. The note explains the origin of the ComplexWarning,
assures users that it is non-critical in this context, and provides a
code snippet to suppress the warning.

**Benefits:**
- Clarifies to users that the ComplexWarning does not indicate a
calculation error, which can help avoid unnecessary troubleshooting.
- Offers an easy method to suppress the warning

**Possible Drawbacks:**
- Suppressing warnings could potentially hide other, less common issues
involving complex numbers in more advanced workflows
- Users may overlook the warning's informational value if they suppress
it without fully understanding the implications

**Related GitHub Issues:**

---------

Co-authored-by: Mudit Pandey <[email protected]>
  • Loading branch information
justinpickering and mudit2812 authored Nov 8, 2024
1 parent 73a617a commit b158f92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions doc/introduction/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ a :class:`QNode <pennylane.QNode>`, e.g.,
If no interface is specified, PennyLane will automatically determine the interface based on provided arguments and keyword arguments.
See ``qml.workflow.SUPPORTED_INTERFACES`` for a list of all accepted interface strings.

.. warning::

``ComplexWarning`` messages may appear when running differentiable workflows involving both complex and float types, particularly
with certain interfaces. These warnings are common in backpropagation due to the nature of complex casting and do not
indicate an error in computation. If desired, you can suppress these warnings by adding the following code:

.. code-block:: python
import warnings
warnings.filterwarnings("ignore", category=np.ComplexWarning)
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
Expand Down
9 changes: 7 additions & 2 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@

<h3>Documentation 📝</h3>

* Add a warning message to Gradients and training documentation about ComplexWarnings
[(#6543)](https://github.com/PennyLaneAI/pennylane/pull/6543)

<h3>Bug fixes 🐛</h3>

<h3>Contributors ✍️</h3>
Expand All @@ -60,5 +63,7 @@ This release contains contributions from (in alphabetical order):

Shiwen An
Astral Cai,
Pietropaolo Frisoni,
Andrija Paurevic
Andrija Paurevic,
Justin Pickering
Pietropaolo Frisoni

0 comments on commit b158f92

Please sign in to comment.