PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.34k stars 601 forks source link

[BUG] default.mixed does not cast to interface on non-parametrized circuits #4194

Closed tnemoz closed 1 year ago

tnemoz commented 1 year ago

Expected behavior

When using the device default.mixed, a QNode defined on the torch interface returning using the qml.state() function should return a torch tensor.

Actual behavior

Additional information

No response

Source code

import pennylane as qml
import torch

dev_qubit = qml.device("default.qubit", wires=1)

@qml.qnode(dev_qubit, interface="torch")
def circuit0(x):
    qml.RX(x, wires=0)
    return qml.state()

@qml.qnode(dev_qubit, interface="torch")
def circuit1():
    qml.PauliZ(wires=0)
    return qml.state()

print(circuit0(1))
print(circuit1())
print("=" * 50)

dev_mixed = qml.device("default.mixed", wires=1)

@qml.qnode(dev_mixed, interface="torch")
def circuit2(x):
    qml.RX(x, wires=0)
    return qml.state()

@qml.qnode(dev_mixed, interface="torch")
def circuit3():
    qml.PauliZ(wires=0)
    return qml.state()

print(circuit2(1))
print(circuit2(torch.tensor(1)))
print(circuit3(), type(circuit3()))

print(circuit2(torch.tensor(1)) - circuit3())
# The following throws an error
print(circuit3() - circuit2(torch.tensor(1)))

Tracebacks

Traceback (most recent call last):
  File "/home/tristan/bigfiles/UnitaryHack/pennylane/issue.py", line 38, in <module>
    print(circuit3() - circuit2(torch.tensor(1)))
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'Tensor'

System information

Name: PennyLane
Version: 0.31.0.dev0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/tristan/bigfiles/UnitaryHack/venv/lib/python3.10/site-packages
Editable project location: /home/tristan/bigfiles/UnitaryHack/pennylane
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           Linux-5.10.180-1-MANJARO-x86_64-with-glibc2.37
Python version:          3.10.10
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- default.gaussian (PennyLane-0.31.0.dev0)
- default.mixed (PennyLane-0.31.0.dev0)
- default.qubit (PennyLane-0.31.0.dev0)
- default.qubit.autograd (PennyLane-0.31.0.dev0)
- default.qubit.jax (PennyLane-0.31.0.dev0)
- default.qubit.tf (PennyLane-0.31.0.dev0)
- default.qubit.torch (PennyLane-0.31.0.dev0)
- default.qutrit (PennyLane-0.31.0.dev0)
- null.qubit (PennyLane-0.31.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.30.0)

Existing GitHub issues

isaacdevlugt commented 1 year ago

Hey @tnemoz, thanks for reporting that. I can replicate the behaviour on v0.30. We'll look into it!