Closed eddddddy closed 1 year ago
After doing some digging, I have updates regarding the cause of this error and some suggestions for fixes.
The error message is raised in ~/tape/tape.py
when we do some validation checks to determine if the tape can be executed correctly:
Looking at the observables one would agree that this error message is correct. Things get a little more complicated when we remove the qml.probs
measurement. After removing it, we are somehow able to correctly execute the tape and return a tuple of the results.
This happens because we apply a batch transform to our tapes before we execute them (line 338 in execution.py
):
There is logic in the device batch transform which tries to address simultaneous measurements of non-commuting observables by splitting up the tape into multiple tapes with each measurement grouped (line 759 in _device.py
):
Im not sure why exactly, but as long as probs
, counts
, sample
, all counts
are not measured in the tape, we are allowed to do this splitting (maybe @josh146 has some ideas as to why this is the case). Once the tapes are split, the error message does not get triggered because each tape is executed separately, and so we don't run into the issue of simultaneously measuring non-commuting observables.
It is also important to note, @Qottmann has worked on a tape transform to allow simultaneous measurements of non-commuting observables. This seems like a case where multiple people wanted to include this functionality in, and we don't have a standard way of dealing with it yet.
Now the question is, what makes sense for the correct behaviour here?
In the long term, I would love to see a push to standardize approach to how we validate tapes and handle non-commuting measurements (both on hardware and on simulator).
In the short-term, I think we can provide more detail by listing out the measurements in the error message so users can see what exactly is being measured together would be useful.
Let me know what you think about this. We could also add a check that loops through the tape and determines if we are measuring any one of(probs
, counts
, sample
, all counts
) with expectation values, and use that info to trigger a different error message.
Expected behavior
The below code should print out the quantities:
Actual behavior
An error is raised instead:
Additional information
The above error only occurs when two non-commuting observables are returned in conjunction with probabilities.
If we instead return non-commuting observables, e.g.
return qml.expval(qml.PauliX(0) @ qml.PauliZ(1)), qml.expval(qml.PauliZ(0) @ qml.PauliY(1))
, the error disappears.If we instead return a single observable and probabilities, e.g.
return qml.expval(qml.PauliX(0) @ qml.PauliZ(1)), qml.probs(wires=[0, 1])
, the error also disappears.This occurs in the old return type system.
Source code
Tracebacks
System information
Existing GitHub issues