PennyLightning StatePrep Import Error

I made this work on 28/08/23 (using 0.30.0) but now I am receiving the below error now. Ideally my code runs on Pennylane==0.26.0 with the default.qiskit (I believe it works with 0.30.0 and even 0.31.0 with default.qiskit) but lightning.gpu plugin does not exist for Pennylane==0.26.0 so have to upgrade it to 0.30.0. I am using a google colab setup with a T4 GPU

Fri Sep  1 00:47:51 2023       
| NVIDIA-SMI 525.105.17   Driver Version: 525.105.17   CUDA Version: 12.0     |
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|   0  Tesla T4            Off  | 00000000:00:04.0 Off |                    0 |
| N/A   41C    P8     9W /  70W |      0MiB / 15360MiB |      0%      Default |
|                               |                      |                  N/A |
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|  No running processes found                                                 |


class QuantumModel(nn.Module):
    def __init__(self,
        super(QLSTM, self).__init__()
        self.n_inputs = input_size
        self.hidden_size = hidden_size
        self.concat_size = self.n_inputs + self.hidden_size
        self.n_qubits = n_qubits
        self.n_qlayers = n_qlayers
        self.n_vrotations = n_vrotations
        self.backend = backend  # "default.qubit", "qiskit.basicaer", ""

        self.batch_first = batch_first
        self.return_sequences = return_sequences
        self.return_state = return_state

        self.wires_forget = [f"wire_forget_{i}" for i in range(self.n_qubits)]
        self.wires_input = [f"wire_input_{i}" for i in range(self.n_qubits)]
        self.wires_update = [f"wire_update_{i}" for i in range(self.n_qubits)]
        self.wires_output = [f"wire_output_{i}" for i in range(self.n_qubits)]

        self.dev_forget = qml.device(self.backend, wires=self.wires_forget)
        self.dev_input = qml.device(self.backend, wires=self.wires_input)
        self.dev_update = qml.device(self.backend, wires=self.wires_update)
        self.dev_output = qml.device(self.backend, wires=self.wires_output)
ImportError                               Traceback (most recent call last)
<ipython-input-2-9669f10f6bc6> in <cell line: 311>()
    309 num_hidden_units = 16
--> 311 Qmodel = QModel(num_sensors=train_X.shape[-1], hidden_units=num_hidden_units, n_qubits=4)
    312 loss_function = nn.MSELoss()
    313 optimizer = torch.optim.Adam(Qmodel.parameters(), lr=learning_rate)

6 frames
/usr/local/lib/python3.10/dist-packages/pennylane_lightning_gpu/ in <module>
     24 import concurrent.futures
---> 26 from pennylane import (
     27     math,
     28     QubitDevice,

ImportError: cannot import name 'StatePrep' from 'pennylane' (/usr/local/lib/python3.10/dist-packages/pennylane/

My Setup

import pennylane as qml
Name: PennyLane
Version: 0.30.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-Lightning-GPU

Platform info:           Linux-5.15.109+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- default.gaussian (PennyLane-0.30.0)
- default.mixed (PennyLane-0.30.0)
- default.qubit (PennyLane-0.30.0)
- default.qubit.autograd (PennyLane-0.30.0)
- default.qubit.jax (PennyLane-0.30.0)
- (PennyLane-0.30.0)
- default.qubit.torch (PennyLane-0.30.0)
- default.qutrit (PennyLane-0.30.0)
- null.qubit (PennyLane-0.30.0)
- lightning.qubit (PennyLane-Lightning-0.31.0)
- lightning.gpu (PennyLane-Lightning-GPU-0.32.0)

Note my code is not supported with the pennylane==0.32.0. This is the error I get. The 32 is the number of features. I am unable to share my code as it is for a research project.

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-c4458618c807> in <cell line: 14>()
     12 print("Untrained test\n--------")
     13 start = time.time()
---> 14 test_loss = test_model(test_loader, Qmodel, loss_function)
     15 end = time.time()
     16 print("Execution time", end - start)

8 frames
/usr/local/lib/python3.10/dist-packages/pennylane/qnn/ in <listcomp>(.0)
    434         if len(x.shape) > 1:
--> 435             res = [torch.reshape(r, (x.shape[0], -1)) for r in res]
    437         return torch.hstack(res).type(x.dtype)

RuntimeError: shape '[32, -1]' is invalid for input of size 4

Hi @saadz-khan

The version of lightning.gpu and lightning.qubit you have installed are newer than your version of PennyLane. We tend to fix the version number for the lightning packages as with PennyLane to match the feature support. In this case, the StatePrep operation is a breaking change we added in 0.32 of PennyLane, which has its support also available in Lightning devices (see the breaking changes section of Release notes — PennyLane 0.32.0 documentation). Since your installed Pennylane is 0.30, lightning.gpu cannot find the operation and is raising an error.

In this case, you can install a pinned version of lightning.qubit and lightning.gpu that matches your PennyLane version using:

python -m pip install pennylane-lightning~=0.30.0 pennylane-lightning-gpu~=0.30.0

Hopefully this should fix the error you observe.

As an aside, lightning.gpu has existed since PennyLane version 0.22. If you wish to download an older version, you can use the pip-install any of the version strings listed here: PennyLane-Lightning-GPU · PyPI

Though, older versions may not be as performant or as featureful as the latest releases.

As for upgrading your code to work with PennyLane 0.32, if you can provide a minimum working example that reproduces the error, we may be able to advise you on what is needed. Feel free to reach out if we can be any further help.


Amazing this works now. Sure, I can tell show you the part where the issue arises with 0.32.0 (latest as of now). I will reply about this in a few days. Have a great day. Thank you so much for your amazing input.

Hey @saadz-khan , we’re super glad that helped! :slight_smile:
It would be awesome if you could take a minute and tell us how you’re using PennyLane and give some feedback to our team in the v0.32 survey.