Closed timmysilv closed 5 months ago
I feel like I'm forgetting something, but why isn't qml.transforms.split_non_commuting
called before tape expansion?
In this particular case, it's because there's a Hamiltonian measurement. If there wasn't, we'd have called it
Ahhhhhhhh. Interesting. cc @trbromley, because this is definitely something we could support if we want to. That is, split_non_commuting
could be updated to work with op_sum
/Hamiltonians. I feel like this is the very high-level 'feature issue' underlying this bug if we want to address it.
Another case where this behaviour becomes relevant is when creating and executing tapes manually. In this case, no error will be raised when reusing wires in non-qwc measurements, but the diagonalizing gates of one measurement affect the outcome of other measurements:
with qml.queuing.AnnotatedQueue() as q:
qml.expval(qml.PauliZ(0) @ qml.PauliX(1))
qml.probs(wires=[0, 1])
tape = qml.tape.QuantumScript.from_queue(q)
>>> dev.execute(tape)
(array(0.), array([0.5, 0.5, 0. , 0. ]))
The state in which we measure the probabilities should be [1, 0, 0, 0]
but the diagonalizing gates of qml.PauliZ(0)@qml.Paulix(1)
, which are applied to the state to compute its expectation value, alter it into [1, 1, 0, 0]/np.sqrt(2)
, yielding a wrong result.
This bug seems to be resolved at the latest by #5424. :) Good to close @timmysilv @josh146 ?
confirmed the example passes for both now! lgtm
Expected behavior
Originally found by @albi3ro
When running a circuit that returns non-commuting observables, I expect it to either fail every time, or succeed every time. This should likely be decided by the device that executes the circuit, but the details can be determined as a result of this bug report.
Actual behavior
In the source code, only
circuit2
fails.circuit2
fails because it does the following:default.qubit
. It sees thatPermute
is not supportedcircuit.expand
(which callsqml.tape.tape.expand_tape(circuit)
)expand_tape
tries to diagonalize the circuit observables but fails because they are non-commutingcircuit1
succeeds because all its operators are supported, so it never does the expansion in step 2 above.Additional information
Lots of possible results here. I'll propose a few:
expand_measurements=True
The fourth option seems most sensible but might require a bit more effort.
Source code
Tracebacks
System information
Existing GitHub issues