Qml.device error

Hi, I am having an error when calling the device:

import pennylane as qml
from pennylane import numpy as np

# Number of qubits (8 in total: 4 for nuclear spins, 4 for electronic states)
n_wires = 8

# Define initial random parameters for the circuit.
np.random.seed(42)
params = np.random.normal(0, np.pi, 3*n_wires)

# Define TN method
kwargs_tn = {
	"contract": False,
	"local_simplify": "DCRS",
        "contraction_optimizer": None,
}
 

# Define a device to simulate our quantum system with 8 qubits
dev = qml.device("default.tensor", method="tn", **kwargs_tn) 

full error message here:

ImportError                               Traceback (most recent call last)
Cell In[15], line 26
     18 kwargs_tn = {
     19 	"contract": False,
     20 	"local_simplify": "DCRS",
     21     "contraction_optimizer": None,
     22 }
     25 # Define a device to simulate our quantum system with 8 qubits
---> 26 dev = qml.device("default.tensor", method="tn", **kwargs_tn) 
     28 # Quantum circuit to model nuclear-electronic entanglement with hyperfine and magnetic field effects
     29 @qml.qnode(dev)
     30 def circuit_with_energy(params):

File ~\anaconda\Lib\site-packages\pennylane\devices\device_constructor.py:280, in device(name, *args, **kwargs)
    274         raise qml.DeviceError(
    275             f"The {name} plugin requires PennyLane versions {required_versions}, "
    276             f"however PennyLane version {qml.version()} is installed."
    277         )
    279 # Construct the device
--> 280 dev = plugin_device_class(*args, **options)
    282 # Once the device is constructed, we set its custom expansion function if
    283 # any custom decompositions were specified.
    284 if custom_decomps is not None:

File ~\anaconda\Lib\site-packages\pennylane\devices\default_tensor.py:376, in DefaultTensor.__init__(self, wires, method, c_dtype, **kwargs)
    368 def __init__(
    369     self,
    370     wires=None,
   (...)
    373     **kwargs,
    374 ) -> None:
    375     if not has_quimb:
--> 376         raise ImportError(
    377             "This feature requires quimb, a library for tensor network manipulations. "
    378             "It can be installed with:\n\npip install quimb"
    379         )  # pragma: no cover
    381     if not accepted_methods(method):
    382         raise ValueError(
    383             f"Unsupported method: {method}. Supported methods are 'mps' (Matrix Product State) and 'tn' (Exact Tensor Network)."
    384         )

ImportError: This feature requires quimb, a library for tensor network manipulations. It can be installed with:

pip install quimb

Please help to solve this error. I installed quimb, but it still shows the same error.

Hi @Iffat_Arisa, welcome to the Forum!

I would recommend that you start with a fresh virtual environment or use Google Colab.
I tested your code on Google Colab and see no issues. Let me know if you need guidance on how to make a new virtual environment.

If you still have trouble even in a fresh virtual environment, can you please post the output of qml.about()?

The output of qml.about() is:
Name: PennyLane
Version: 0.38.1
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: GitHub - 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.
Author:
Author-email:
License: Apache License 2.0
Location: C:\Users\iffat\anaconda\Lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: Windows-11-10.0.22631-SP0
Python version: 3.12.4
Numpy version: 1.26.4
Scipy version: 1.13.1
Installed devices:

  • default.clifford (PennyLane-0.38.1)
  • default.gaussian (PennyLane-0.38.1)
  • default.mixed (PennyLane-0.38.1)
  • default.qubit (PennyLane-0.38.1)
  • default.qubit.autograd (PennyLane-0.38.1)
  • default.qubit.jax (PennyLane-0.38.1)
  • default.qubit.legacy (PennyLane-0.38.1)
  • default.qubit.tf (PennyLane-0.38.1)
  • default.qubit.torch (PennyLane-0.38.1)
  • default.qutrit (PennyLane-0.38.1)
  • default.qutrit.mixed (PennyLane-0.38.1)
  • default.tensor (PennyLane-0.38.1)
  • null.qubit (PennyLane-0.38.1)
  • lightning.qubit (PennyLane_Lightning-0.38.0)

I need guidance on how to make a new virtual environment.

Hi @Iffat_Arisa

Here’s my recommendation for creating a new virtual environment with Conda and installing PennyLane and quimb:

  1. Install Miniconda following the instructions here.
  2. Open your terminal (mac) or command line (Windows).
  3. Create a new Conda environment with: conda create --name <name_of_your_environment> python=3.10
  4. Activate the environment with: conda activate <name_of_your_environment>
  5. Install PennyLane with: python -m pip install pennylane
  6. Install other useful packages with: python -m pip install jupyter matplotlib quimb

Note that you will be installing 4 packages here: PennyLane, Jupyter, Matplotlib, and quimb. Also, note that where it says <name_of_your_environment> you can choose any name that you want.

I’ve tested it on a mac and this works, let me know if it works for you too!

Before installing, would I uninstall the current conda, python, jupiter, matplotlib, quimb?

Hi @Iffat_Arisa, there’s no need. When you create a new virtual environment it will be like a blank page. When you install these libraries within the environment your program will look there first. If it doesn’t find them then sometimes it will look in other places (if something didn’t get installed correctly), but normally everything should be isolated in the environment.