I am attempting to sample a few shots through the two observables M_1 = (\sigma_x \otimes \sigma_z) and M_2 = (\sigma_z \otimes \sigma_x) simultaneously with two qubits.
The simplest circuit I can come up with is this one I believe:
QuantumFunctionError: Only observables that are qubit-wise commuting Pauli words can be returned on the same wire
I understand the commuting requirement of M_1 and M_2, and I believe this is checked since M_1 M_2 = (\sigma_x \sigma_z) \otimes (\sigma_z \sigma_x) = (-\sigma_z \sigma_x) \otimes (-\sigma_x \sigma_z) = M_2 M_1
But a similar error seems to be raised even with the following function:
qml.grouping.diagonalize_qwc_pauli_words(obs)
So I believe I am not supposed to use the framework in this way for the result I intend to obtain. Could you help me figure out what I should do?
I was puzzled by your issue, since it was also not clear to me why you were getting this error. You are correct, these two observables commute and this error shouldn’t be appearing.
I am working from the latest version of PennyLane directly from master on github, and I don’t get any error when running your example QNode. Perhaps you can try installing directly from the latest version and checking if the issue persists? You can do this easily using
and I do get an error stating the observables don’t commute. I can potentially dig deeper there to see what is happening. But maybe let’s check first if your main problems disappear if you work from the latest version?
So, I confirm: using version 0.21.0-dev “solves” my problem.
However, I also get the same error for “qml.grouping.diagonalize_qwc_pauli_words”.
So I don’t need this function anyway, but I am quite surprised in fact - it made sense to me that some diagonalization functions might be called in the background in any case, so I would have expected it to be called even in the latest version.
I would guess the qnode __ call __ method used to call qtape in 0.19.1:
which may be the reason why it might raise this error at some point:
This all has to do with the concept of qubit-wise commutation, as epxlained in this tutorial
Essentially, it’s challenging to check if arbitrary Pauli words commute, so a shortcut taken in the grouping module is to simply check if the operators acting on each qubit commute. Essentially, this will happen only if there is an identity acting on at least one of the qubits. If the operators are qubit-wise commuting, then they commute!
The operators in your example are not in fact qubit-wise commuting, so the error is actually being correctly flagged.
You’re right, I also just realized there was a misunderstanding on my side with this “non qubit-wise commutativity error” when I discovered that “pennylane.grouping.utils.is_commuting” is actually returning true while .is_qwc is false.