Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update casting for apply_operation #6268

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions doc/development/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions doc/releases/changelog-0.38.1.md

This file was deleted.

4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@

<h3>Bug fixes 🐛</h3>

* 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)
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved

* Fix a bug where zero-valued JVPs were calculated wrongly in the presence of shot vectors.
[(#6219)](https://github.com/PennyLaneAI/pennylane/pull/6219)

Expand Down
17 changes: 15 additions & 2 deletions pennylane/devices/qubit/apply_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Loading