Question about documentations of Numpy interface

Hi, I’m a beginner in Pennylane and I’m reading the documentation. In Numpy interface, I try to run the code below,

dev = qml.device('default.qubit', wires=5)

@qml.qnode(dev)
def circuit(data, weights):
    qml.AmplitudeEmbedding(data, wires=[0, 1, 2], normalize=True)
    qml.RX(weights[0], wires=0)
    qml.RY(weights[1], wires=1)
    qml.RZ(weights[2], wires=2)
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[0, 2])
    return qml.expval(qml.PauliZ(0))

rng = np.random.default_rng(seed=42)  # make the results reproducable
data = rng.random([2 ** 3], requires_grad=True)
weights = np.array([0.1, 0.2, 0.3], requires_grad=False)

print(qml.grad(circuit)(data, weights=weights))

but get a TypeError:

TypeError: must be real number, not ArrayBox

I find that if I just change the wires in qml.device to 3, which is the number of wires used in qml.AmplitudeEmbedding, then the code works with a UserWarning:

[ 0.38170533  0.21644929  0.42344963  0.34393308 -0.04337338 -0.44932293 -0.35054294 -0.36202196]
ComplexWarning: Casting complex values to real discards the imaginary part
return A.astype(dtype, order, casting, subok, copy)

It seems like the question is related to the qml.AmplitudeEmbedding function, but in qml.AmplitudeEmbedding’s documentation it is said that

At the moment, the features argument is not differentiable when using the template, and gradients with respect to the features cannot be computed by PennyLane.

Due to non-trivial classical processing to construct the state preparation circuit, the features argument is in general not differentiable.

So what’s the output above? I’m confused about them.

I was wondering if there’s something wrong with the code in “Numpy interface” documentation, or if there’s something I ignored. Could it be possible to explain these questions?

Thanks in advance for your help!

Best regards,
Henry

Hi @Henry, thank you for asking this question. This seems to be a bug with the default.qubit device because if you try with lightning.qubit it works well with 5 wires.

Regarding the differentiability, the docs say that the features argument is “in general” not differentiable, but there does seem to be a gradient every time so I’ll get back to you on this one.

Hi @CatalinaAlbornoz, thank you very much for your reply! That’s almost enough for me to know it seems to be a bug, and I’ll try to avoid the situation in the future.

However, I feel so sorry to say that when I use lightning.qubit or import pennylane_lightning it raises an ImportError :disappointed:

ImportError: DLL load failed while importing lightning_qubit_ops: The specified module could not be found.

I just follow the installation manual to install the latest modules…it seems that I might be the first one to meet this error. I would be very grateful if you could provide any suggestions.

Anyway, thanks again for your kind help!

Hi @Henry,

lightning.qubit already comes installed with the latest versions of PennyLane. My suggestion would be to create a new virtual environment on your terminal using Python 3.7 or higher: conda create --name <your_env_name> python=3.10

The only imports you should need are import pennylane as qml and
from pennylane import numpy as np

If you’re using an M1 mac this will not work. You will need to set up an x86 environment, which you can do by following the instructions here.

Another option is to go to pennylane.xanadu.ai and set up an account for our cloud, where you will be able to run your circuits without having to install anything in your computer. This is still in beta, so please let us know if you have any feedback on the platform in case you decide to use it.

Please let me know if you manage to solve this issue!

Hi, @CatalinaAlbornoz,

sorry to reply so late. It still raises the ImportError about lightning.qubit on my desktop computer, but it works well with 5 wires when using lightning.qubit on my laptop. It’s a little strange, as both of them are Win10 and I installed pennylane in the same way. Luckily, this doesn’t affect my work too much, and I’ll try to solve the problem later.

Thanks so much for your kind help!

Hi @Henry,

I’m glad it’s working on your laptop. The only explanation I can find for it not working on your desktop computer is that something in the virtual environment is causing problems. You can try creating a new virtual environment and install only pennylane and jupyter. You can then see if it works or if not you can use import pennylane as qml;qml.about() and you can see if there’s something different from the environment you have on your laptop.

I hope this is useful in making it work on your desktop computer!

Hi @CatalinaAlbornoz,
I encountered the same problem when I tried to use lightning.qubit, it raised the same ImportError as:

ImportError: DLL load failed while importing lightning_qubit_ops: The specified module could not be found.

Here is my pennylane version:

Platform info: Windows-10-10.0.19041-SP0
Python version: 3.10.0
Numpy version: 1.21.5
Scipy version: 1.7.3
Installed devices:

  • default.gaussian (PennyLane-0.22.1)
  • default.mixed (PennyLane-0.22.1)
  • default.qubit (PennyLane-0.22.1)
  • default.qubit.autograd (PennyLane-0.22.1)
  • default.qubit.jax (PennyLane-0.22.1)
  • default.qubit.tf (PennyLane-0.22.1)
  • default.qubit.torch (PennyLane-0.22.1)
  • lightning.qubit (PennyLane-Lightning-0.22.0)

Thank you in advance for any possible help!
BestRegards,
Wenhao

Hi @CHU_WENHAO,

could you update the PennyLane to 0.23? pip install PennyLane==0.23 will do this.

@CHU_WENHAO and @CY_Park

Running python -m pip install PennyLane==0.23.1 would be even better :grin:

The error indicates that certain “dependencies” required for the desired libraries were missing. This is a frequently encountered issue during the installation of Python packages, particularly on Windows. Prior to utilizing any library, it is advisable to verify if it necessitates additional libraries within the Python ecosystem.

The solution is to provide the interpreter with the python path to your module/library. The simplest solution is to append that path to your sys.path list. In your notebook, first try:

import sys
sys.path.append('my/path/to/module/folder')

This is not a persistent modification to the sys.path, as your environment is reset when you log out, causing any variables you may have set to be lost.

A more effective (and enduring) approach to resolve this issue is by configuring your PYTHONPATH, which enables the interpreter to search for Python packages/modules in additional directories.

from BASH type: export PYTHONPATH=/path/to/new/folder:/another/path/...../ 

#each path must be separated by a colon