I am running the latest version of Pennylane 0.45.0 in a Jupyter notebook. However I am receiving Numpy errors (particularly regarding msort and linalg) when I run “import pennylane as qml”. This occurs when using numpy version 2.4.6. If I type "%pip install “numpy<2.0”, numpy version 1.26.4 is installed and I can import pennylane successfully. Information on the web says that the latest version of pennylane should be compatible with the latest version of numpy. Why am I getting errors? Here is the output of qml.about().
Name: pennylane
Version: 0.45.0
Summary: 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.
Home-page:
Author:
License:
Location: C:\Users\mlowe\anaconda3\envs\Pennylane\Lib\site-packages
Platform info: Windows-10-10.0.26200-SP0
Python version: 3.11.7
Numpy version: 1.26.4
Scipy version: 1.13.0
JAX version: None
Catalyst version: None
Installed devices:
- default.clifford (pennylane-0.45.0)
- default.gaussian (pennylane-0.45.0)
- default.mixed (pennylane-0.45.0)
- default.qubit (pennylane-0.45.0)
- default.qutrit (pennylane-0.45.0)
- default.qutrit.mixed (pennylane-0.45.0)
- default.tensor (pennylane-0.45.0)
- null.qubit (pennylane-0.45.0)
- reference.qubit (pennylane-0.45.0)
- lightning.qubit (pennylane_lightning-0.45.0)
- qulacs.simulator (pennylane-qulacs-0.44.0)
This is strange indeed. Could you please share the full error traceback that you’re getting? I would recommend creating a new virtual environment to avoid any conflicts between packages in your base environment.
I always work in a Pennylane virtual environment. This is the error message when I use Pennylane version 0.45.0 and numpy version 2.4.6. The Jupyter notebook was created when Anaconda was installed.
AttributeError Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pennylane as qml
File ~\anaconda3\envs\Pennylane\Lib\site-packages\pennylane\__init__.py:23
21 from pennylane import exceptions
22 from pennylane.boolean_fn import BooleanFn
---> 23 from pennylane import numpy
24 from pennylane.queuing import QueuingManager, apply
26 from pennylane import compiler
File ~\anaconda3\envs\Pennylane\Lib\site-packages\pennylane\numpy\__init__.py:86
14 """
15 Overview
16 --------
(...)
81
82 """
84 # pylint: disable=wrong-import-position,undefined-variable
---> 86 from autograd import numpy as _np
87 from autograd.numpy import *
89 from pennylane.exceptions import NonDifferentiableError
File ~\anaconda3\envs\Pennylane\Lib\site-packages\autograd\__init__.py:2
1 from __future__ import absolute_import
----> 2 from .differential_operators import (
3 make_vjp, grad, multigrad_dict, elementwise_grad, value_and_grad,
4 grad_and_aux, hessian_tensor_product, hessian_vector_product, hessian,
5 jacobian, tensor_jacobian_product, vector_jacobian_product, grad_named,
6 checkpoint, make_hvp, make_jvp, make_ggnvp, deriv, holomorphic_grad)
7 from .builtins import isinstance, type, tuple, list, dict
8 from autograd.core import primitive_with_deprecation_warnings as primitive
File ~\anaconda3\envs\Pennylane\Lib\site-packages\autograd\differential_operators.py:16
13 from .core import make_vjp as _make_vjp, make_jvp as _make_jvp
14 from .extend import primitive, defvjp_argnum, vspace
---> 16 import autograd.numpy as np
18 make_vjp = unary_to_nary(_make_vjp)
19 make_jvp = unary_to_nary(_make_jvp)
File ~\anaconda3\envs\Pennylane\Lib\site-packages\autograd\numpy\__init__.py:3
1 from __future__ import absolute_import
2 from .numpy_wrapper import *
----> 3 from . import numpy_boxes
4 from . import numpy_vspaces
5 from . import numpy_vjps
File ~\anaconda3\envs\Pennylane\Lib\site-packages\autograd\numpy\numpy_boxes.py:70
67 setattr(ArrayBox, 'flatten', anp.__dict__['ravel'])
69 if np.__version__ >= '1.25':
---> 70 SequenceBox.register(np.linalg.linalg.EigResult)
71 SequenceBox.register(np.linalg.linalg.EighResult)
72 SequenceBox.register(np.linalg.linalg.QRResult)
AttributeError: module 'numpy.linalg' has no attribute 'linalg'
Today, on another computer, I created a virtual environment with the same packages and ran the same commands. Anaconda was also used to create Jupyter and the environments. The error messages were different but I still could not import Pennylane.
My student installed the same packages but he runs VScode. He can import Pennylane. It seems that Anaconda is part of the problem with the incompatibility between Pennylane 0.45.0 and the current version of Numpy.
I use miniforge, which uses conda-forge, and I can’t replicate the issues you’re having. However, you should be able to use Miniconda without any issues. Here are the steps you can follow:
Install Miniconda following the instructions here.
Open the Anaconda Prompt (or Miniconda Prompt) from your Windows Start menu. The command line should start with (base) e.g.: (base) C:\Users\YourUsername>
Install JupyterLab in your base environment: pip install jupyterlab
Create a new Conda environment with: conda create --name <name_of_your_environment> python=3.11
Activate the environment with: conda activate <name_of_your_environment>
Create an ipykernel to use the environment in your notebook: python -m ipykernel install --user --name <name_of_your_environment> --display-name “Conda (<name_of_your_environment>)”
Launch JupyterLab: jupyter lab
Open or create a notebook and in the top right select the kernel with the name you just defined.
Note that where it says <name_of_your_environment> you can choose any name that you want.
If you want to have different packages for different projects it’s recommended that you follow steps 4-10 for each distinct project.
Let me know if this solves the issue or if it remains!
(Pennylane could be imported with this method) On a different computer, I installed Miniconda and followed your instructions. Step 9 did not work so I modified step 3 to “pip install notebook jupyterlab”. In step 9, these commands then worked: “python -m notebook” and “python -m jupyterlab”
(Pennylane could be imported with this method) On a different computer, I did a fresh installation of Anaconda, then used my usual procedure to create an environment for Pennylane. The procedure is similar to, but not exactly the same as, what you stated above for Miniconda.
(Pennylane 0.45.0 could not be imported with this method) On my normal computers, Anaconda had been previously installed. When I installed Pennylane version 0.45.0, there was an incompatibility with Numpy 2.4.6, and I had to backtrack to Numpy 1.24.6 to run code.
Good catch on modifying the Jupyter commands! Using python -m notebook is a way to ensure you’re executing the Jupyter instance tied to that specific environment.
Regarding the NumPy 2.4.6 issue on your normal computer, since your workflow succeeded on the fresh Anaconda and Miniconda installs, the issue on your normal computer is almost certainly due to the older Anaconda installation leaking into the new environment.
When you have an older Anaconda setup, pip often uses locally cached packages. It’s likely pulling a cached version of a dependency that was built against NumPy 1.x, which then fatally crashes when it encounters the NumPy 2.x architecture.
To get PennyLane running with NumPy 2.x on that computer without having to completely reinstall Anaconda, you could try forcing an update and clearing the cache. You could try running these steps from your Anaconda Prompt:
Update your base conda resolver: conda update -n base conda
Purge your local pip cache so it forgets the old NumPy 1.x wheels: python -m pip cache purge
Create and activate your environment as usual: conda create --name pennylane_np2 python=3.11 conda activate pennylane_np2
Install PennyLane, explicitly telling pip to ignore any lingering cache: python -m pip install pennylane --no-cache-dir
Install your Jupyter tools: python -m pip install notebook jupyterlab
Bypassing the cache and forcing a fresh download should pull the correct NumPy 2.x compatible binaries and get everything playing nicely together.
Please not that there are a few minor inconveniences to be aware of:
The next time you install a Python package or set up a new virtual environment with packages you previously used, pip will have to download them all from the internet again. This makes the installation process noticeably slower than installing from the local cache.
The cache allows you to install packages you’ve downloaded in the past even if you don’t currently have an internet connection. Purging the cache removes this ability until you download those packages again while online.