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

Fixes for new autoray version #4396

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 13 additions & 6 deletions pennylane/math/multi_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,19 @@ def add(*args, like=None, **kwargs):
"""Add arguments element-wise."""
if like == "scipy":
return onp.add(*args, **kwargs) # Dispatch scipy add to numpy backed specifically.
try:
return np.add(*args, **kwargs)
except TypeError:
# catch arg1 = torch, arg2=numpy error
# works fine with opposite order
return np.add(args[1], args[0], *args[2:], **kwargs)

if like == "torch":
# In autoray 0.6.5, np.add dispatches to torch instead of
# numpy if one parameter is a torch tensor and the other is
# a numpy array. torch.add raises an Exception if one of the
# arguments is a numpy array, so here we cast both arguments
# to be tensors.
dev = getattr(args[0], "device", None) or getattr(args[1], "device")
timmysilv marked this conversation as resolved.
Show resolved Hide resolved
arg0 = np.asarray(args[0], device=dev, like=like)
arg1 = np.asarray(args[1], device=dev, like=like)
return np.add(arg0, arg1, *args[2:], **kwargs)
eddddddy marked this conversation as resolved.
Show resolved Hide resolved

return np.add(*args, **kwargs)


@multi_dispatch()
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/qubit/test_apply_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_phaseshift(self, method, wire, ml_framework):
initial_state = qml.math.asarray(initial_state, like=ml_framework)

phase = qml.math.asarray(-2.3, like=ml_framework)
shift = np.exp(qml.math.multiply(1j, phase))
shift = qml.math.exp(1j * qml.math.cast(phase, np.complex128))

new_state = method(qml.PhaseShift(phase, wire), initial_state)

Expand Down
Loading