Description:
I’m encountering an issue where importing PyTorch causes PennyLane to throw a KeyError
. When I use PennyLane on its own, everything works fine, but after installing PyTorch, I receive an error when running a simple PennyLane script.
Steps to Reproduce:
- Create a conda environment and install packages:
conda create --name pennylane_test python=3.9
conda activate pennylane_test
conda install pennylane
conda install pytorch torchvision lightning -c pytorch
Pennylane on his own will work fine, I tested this on a dummy pennylane python exemple as well as my own project. The problems start to arise after I installed pytorch…
- Create a Test Script (
test.py
):
import pennylane as qml
# Define a simple quantum device
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev)
def circuit():
qml.PauliX(wires=0)
return qml.expval(qml.PauliZ(0))
print(circuit())
- Run the Script:
Expected Behavior:
The script should output -1.0
without any errors.
Actual Behavior:
I receive the following error when running the script:
File "______", line 1, in <module>
import pennylane as qml
File "______\pennylane\lib\site-packages\pennylane\__init__.py", line 29, in <module>
import pennylane.kernels
File "______\pennylane\lib\site-packages\pennylane\kernels\__init__.py", line 18, in <module>
from .cost_functions import (
File "______\pennylane\lib\site-packages\pennylane\kernels\cost_functions.py", line 19, in <module>
File "______\pennylane\lib\site-packages\pennylane\math\__init__.py", line 36, in <module>
from .multi_dispatch import (
File "______\pennylane\lib\site-packages\pennylane\math\multi_dispatch.py", line 24, in <module>
from . import single_dispatch # pylint:disable=unused-import
File "______\pennylane\lib\site-packages\pennylane\math\single_dispatch.py", line 329, in <module>
del ar.autoray._FUNCS["torch", "linalg.eigh"]
KeyError: ('torch', 'linalg.eigh')
Additional Information:
- Operating System: Windows 10
- Python Version: 3.9.20
I tried different version of python: 3.9, 3.10, 3.12, 3.13 - Conda Version: 24.9.2
- PennyLane Version: 0.23.0
- Pytorch Version: 2.3.1
Is this a known issue with compatibility between PennyLane and PyTorch? Also, do you have any suggestions on how to resolve this issue?
Thank you for your assistance!