Hi everyone, I’m trying to follow the kernel based training demo, but I want to use the lightning.gpu device since my use case is computationally more demanding. I found out that, unfortunately, this device does not support the Hermitian observable:

984
985 if not self.supports_observable(observable_name):
--> 986 raise DeviceError(
987 f"Observable {observable_name} not supported on device {self.short_name}"
988 )
DeviceError: Observable Hermitian not supported on device lightning.gpu

Is there an alternative for this observable that allows me to run my experiment on the lightning.gpu device?

Unfortunately we do not enable the Hermitian observable support right now due to performance impacts at runtime in the adjoint gradient execution pipeline. Though, recent supports were improved for the v0.30.0 release. You can try upgrading all your packages with

Thank you for your response! . As I mentioned, I’m trying to implement the kernel based training demo with another dataset requiring a larger number of qubits. In the demo implementing the quantum kernel requires the measurement of a projector of the form |0..0\rangle \langle 0..0| which is done with qml.Hermitian. The code I was trying to run is:

Actually, one alternative might be to do a bit of post-processing. If all you want is the |00…0> coefficient, you can just access it via measuring qml.probs(wires=range(n_qubits)) and accessing the zeroth element:

Lovely! Glad that this works . It’s not very efficient, as you’re calculating all2^N probabilities when you really only need 1 . Just be aware of that!

Having thought a little more about this, I may have a path that can give you a decent performance boost, without explicitly building the probs.

lightning.gpu explicitly supports qml.SparseHamiltonian through the adjoint differentiation pipeline (diff_method="adjoint"), which should be much faster than the current parameter-shift one used by default. You may be able to unlock this by taking the following path:

Create your Hermitian projector :h

Create a sparse CSR matrix from h using scipy.sparse.csr_matrix as:

import scipy.sparse as sp
h = qml.Hermitian(projector, wires=range(n_qubits))
sparse_m = sp.csr_matrix(h.data[0])

Finally, create a qml.SparseHamiltonian from the above CSR as: