From 6d98c51a5eb3df545d5fe0bccd34dcc5330a454e Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Fri, 20 Sep 2024 16:57:03 -0400 Subject: [PATCH] Update casting for `apply_operation` (#6268) * Update casting for matrices in `qml.devices.qubit.apply_operation`. For some reason the old casting was making jax happy but not autograd. * Remove the 0.38.1 changelog. @josh146 confirmed that we should do this now that the release has been yanked from PyPi. --- doc/development/release_notes.md | 2 -- doc/releases/changelog-0.38.1.md | 14 -------------- doc/releases/changelog-dev.md | 4 ++++ pennylane/devices/qubit/apply_operation.py | 17 +++++++++++++++-- 4 files changed, 19 insertions(+), 18 deletions(-) delete mode 100644 doc/releases/changelog-0.38.1.md diff --git a/doc/development/release_notes.md b/doc/development/release_notes.md index 07ad0d279f7..a1f5587597c 100644 --- a/doc/development/release_notes.md +++ b/doc/development/release_notes.md @@ -5,8 +5,6 @@ This page contains the release notes for PennyLane. .. mdinclude:: ../releases/changelog-dev.md -.. mdinclude:: ../releases/changelog-0.38.1.md - .. mdinclude:: ../releases/changelog-0.38.0.md .. mdinclude:: ../releases/changelog-0.37.0.md diff --git a/doc/releases/changelog-0.38.1.md b/doc/releases/changelog-0.38.1.md deleted file mode 100644 index aab0ef38311..00000000000 --- a/doc/releases/changelog-0.38.1.md +++ /dev/null @@ -1,14 +0,0 @@ -:orphan: - -# Release 0.38.1 (current release) - -

Bug fixes 🐛

- -* Fix float-to-complex casting in various places across PennyLane. - [(#6260)](https://github.com/PennyLaneAI/pennylane/pull/6260) - -

Contributors ✍️

- -This release contains contributions from (in alphabetical order): - -Mudit Pandey \ No newline at end of file diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 0a7fb5f2c1a..53969525724 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -163,6 +163,10 @@

Bug fixes 🐛

+* Fix float-to-complex casting in various places across PennyLane. + [(#6260)](https://github.com/PennyLaneAI/pennylane/pull/6260) + [(#6268)](https://github.com/PennyLaneAI/pennylane/pull/6268) + * Fix a bug where zero-valued JVPs were calculated wrongly in the presence of shot vectors. [(#6219)](https://github.com/PennyLaneAI/pennylane/pull/6219) diff --git a/pennylane/devices/qubit/apply_operation.py b/pennylane/devices/qubit/apply_operation.py index dccf49ef11b..bef7cfd1472 100644 --- a/pennylane/devices/qubit/apply_operation.py +++ b/pennylane/devices/qubit/apply_operation.py @@ -71,7 +71,13 @@ def apply_operation_einsum(op: qml.operation.Operator, state, is_state_batched: Returns: array[complex]: output_state """ - mat = qml.math.cast_like(op.matrix(), 1j) + # We use this implicit casting strategy as autograd raises ComplexWarnings + # when backpropagating if casting explicitly. Some type of casting is needed + # to prevent ComplexWarnings with backpropagation with other interfaces + if qml.math.get_interface(state) == "tensorflow": + mat = qml.math.cast_like(op.matrix(), state) + else: + mat = op.matrix() + 0j total_indices = len(state.shape) - is_state_batched num_indices = len(op.wires) @@ -114,7 +120,14 @@ def apply_operation_tensordot(op: qml.operation.Operator, state, is_state_batche Returns: array[complex]: output_state """ - mat = qml.math.cast_like(op.matrix(), 1j) + # We use this implicit casting strategy as autograd raises ComplexWarnings + # when backpropagating if casting explicitly. Some type of casting is needed + # to prevent ComplexWarnings with backpropagation with other interfaces + if qml.math.get_interface(state) == "tensorflow": + mat = qml.math.cast_like(op.matrix(), state) + else: + mat = op.matrix() + 0j + total_indices = len(state.shape) - is_state_batched num_indices = len(op.wires)