"lingtning.qubit" not work for Snapshot

Hello! Glad to contact you!

The Snapshot function can work if my dev was “default.qubit”, but it cannot work for the “lightning.qubit”, and here is the ERROR info:

/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/debugging/snapshot.py:247: UserWarning: Snapshots are not supported for the given device. Therefore, a tape will be created for each snapshot, resulting in a total of n_snapshots + 1 executions.
  warnings.warn(
Traceback (most recent call last):
  File "/home/shaolun/PYTHON/QEncoderVis/4.py", line 29, in <module>
    print(qml.snapshots(circuit)()['1'].tolist())
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/workflow/qnode.py", line 1164, in __call__
    return self._impl_call(*args, **kwargs)
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/workflow/qnode.py", line 1150, in _impl_call
    res = self._execution_component(args, kwargs, override_shots=override_shots)
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/workflow/qnode.py", line 1103, in _execution_component
    res = qml.execute(
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/workflow/execution.py", line 650, in execute
    tapes, post_processing = transform_program(tapes)
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/transforms/core/transform_program.py", line 515, in __call__
    new_tapes, fn = transform(tape, *targs, **tkwargs)
  File "/home/shaolun/PYTHON/QEncoderVis/venv_test/lib/python3.10/site-packages/pennylane/devices/preprocess.py", line 490, in validate_measurements
    raise DeviceError(
pennylane._device.DeviceError: Measurement state(wires=[0, 1]) not accepted for analytic simulation on adjoint + lightning.qubit.

The original code is shown here:

import pennylane as qml
from pennylane import numpy as np
import torch

dev = qml.device('lightning.qubit', wires=2)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.Snapshot("1")
    qml.CNOT(wires=[0, 1])

    return qml.expval(qml.X(0))


circuit()


print(qml.snapshots(circuit)()['1'].tolist())

Can you help me with this?

The basic version info is as follows:

Python: 3.10.12
pennylane: 0.37.0
CUDA: 12.6

Here is the info for qml.about()

Hey @_VASTER,

This issue has already been reported here: Support for `qml.state` when `diff_method="adjoint"` · Issue #808 · PennyLaneAI/pennylane-lightning · GitHub

Essentially, diff_method="adjoint" is the default setting with lightning.qubit, and that differentiation method doesn’t support state-based measurements at this time. So, your call to qml.Snapshot fails.

If you aren’t doing any differentiation or if the adjoint differentiation method isn’t paramount to your code, then the simplest thing to do is to just change your diff_method to, e.g., parameter-shift:

@qml.qnode(dev, diff_method="parameter-shift")

Let me know if that helps!

Hi @isaacdevlugt,

Thank you for your quick reply!

Actually, we are the research team for interpreting the variational quantum circuit, so the point here is how to extract the intermediate quantum states before and after each rotation Pauli gates. So if that is the case, does it mean I can only use the default.qubit?

And by the way, what else should I pay attention to in order to extract the intermediate quantum states and use the Snapshot method successfully? Like the proper device or something?

Thank you!

Hi @_VASTER , you can indeed use lightning.qubit, you simply need to change your differentiation method. You can try setting your diff_method to "backprop" or "parameter-shift" as mentioned by Isaac.

You can also try using interactive debugging, which also works on both simulators.

Both default.qubit and lightning.qubit are valid devices for using Snapshots and interactive debugging so ideally they shouldn’t pose any other issues.

As a final recommendation, we released version v0.38 of PennyLane yesterday so you might want to update your PennyLane version to ensure that you’re using the latest one.

I hope this helps!