Pennylane device preprocess error for certain observables in V 0.38.0

Hi Pennylane Team,

While working to solve the Secrets in Spacetime Challenge, I found some interesting behavior for measurement functions like expval() and counts() in Version 0.38.0. I was playing around with some observables and found a solution that worked on my machine (which had Version 0.36.0 installed at the time), but failed on the challenge submission. When I updated to the latest version I saw it failing on my machine as well.

This would be a minimal reproductible example of the observable I was trying to use:

import pennylane as qml
import pennylane.numpy as np

dev = qml.device("default.qubit", wires=2, shots=4)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.counts(qml.prod(qml.RX(x,0),qml.RZ(x,0))@qml.prod(qml.RX(x,1),qml.RZ(x,1)))

circuit(0.5)

The full error message is as follows:

pennylane.DeviceError: Measurement CountsMP(RX(0.5, wires=[0]) @ RZ(0.5, wires=[0]) @ RX(0.5, wires=[1]) @ RZ(0.5, wires=[1]), all_outcomes=False) not accepted with finite shots on default.qubit

I was doing some more testing and the lightning.qubit device seems to work on Version 0.38.0.

I’ll look into the implementation of the preprocess.py script to better understand the latest changes, but wanted to let you guys know.

I appreciate any thoughts.

Thanks!

Packages info:

Name: PennyLane
Version: 0.38.0
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: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/fgranda/.local/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.26.4
Scipy version:           1.13.1
Installed devices:
- default.clifford (PennyLane-0.38.0)
- default.gaussian (PennyLane-0.38.0)
- default.mixed (PennyLane-0.38.0)
- default.qubit (PennyLane-0.38.0)
- default.qubit.autograd (PennyLane-0.38.0)
- default.qubit.jax (PennyLane-0.38.0)
- default.qubit.legacy (PennyLane-0.38.0)
- default.qubit.tf (PennyLane-0.38.0)
- default.qubit.torch (PennyLane-0.38.0)
- default.qutrit (PennyLane-0.38.0)
- default.qutrit.mixed (PennyLane-0.38.0)
- default.tensor (PennyLane-0.38.0)
- null.qubit (PennyLane-0.38.0)
- lightning.qubit (PennyLane_Lightning-0.38.0)

Hi @spacetraveler ,

I think the issue here is that you’re trying to measure an operator instead of an observable. For example if you get the counts for qml.Y(0) instead you’ll notice that the error goes away. I hope this helps!

Hi @CatalinaAlbornoz, thanks for your quick reply! I was also talking about this issue with Alvaro from the Pennylane Team this past weekend during the GenQ Hackathon. Alvaro was telling me about this new “Prod” (uppercase P) function in Pennylane and how it is different from the “prod” (lowercase p) function.

I thought it might solve the issue but it didn’t. I was looking into the Pennylane Source Code, more specifically the “preprocess.py” and “measurements.py” for some clues. It seems like it is a problem of trying to use a non-Hermitian operator with the measurement function. I think the handling of this exception could be improved, since the error message did show any relevant comments related to the actual problem.

Also, it is still very interesting that a non-Hermitian operator worked well with previous Pennylane versions (and the “lighting.qubit” device, which Alvaro pointed out might be behind in receiving the latest updates).

Thanks again! and I’ll keep looking at the older version to get some clues for fixing my code for the challenge :wink:

Thanks for the additional context @spacetraveler !

1 Like