Hi,
I got a negative value when returning qml.expval(PauliZ(0)) from a quantum circuit function. I then looked at the qml.expval documentation and was surprised to see the following example:
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
qml.Hadamard(wires=1)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliY(0))
And,
>>>>>circuit(0.5)
-0.4794255386042029
How can the expectation value of a measurement be negative? Is it not always positive according to Expectation value (quantum mechanics) - Wikipedia
Consider the projective measurement of some quantum circuit B, <B>; would <B> be -0.479 or (-0.479)^2 in this case?
Hey @Nikhil_Narayanan! Great question. When you measure an expectation value of an operator in a real experiment, the set of outcomes you can get are the eigenvalues of that operator. So, for any of the Pauli operators, their eigenvalues are 1 and -1. We can see this in a fake experiment by setting shots=1
and using qml.sample
, which is sampling the eigenvalues of the operator of interest.
import pennylane as qml
dev = qml.device("default.qubit", wires=2, shots=1)
@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
qml.Hadamard(wires=1)
qml.CNOT(wires=[0, 1])
return qml.sample(qml.PauliY(0))
num_executions = 4
for _ in range(num_executions):
print(circuit(0.123))
'''
1
-1
-1
1
'''
What’s happening when we call qml.expval
is that we are taking a weighted average of the possible measurement outcomes: \pm 1. So, the result can be negative!
I encourage you to watch this video: Measurements and quantum circuits | PennyLane Tutorial - YouTube
Also, it might help to write out what an expectation value is analytically. E.g., try writing out what \langle \psi \vert Y \vert \psi \rangle is for \vert \psi \rangle = \frac{1}{2} \left( \vert 0 \rangle + \sqrt{3} \vert 1 \rangle \right) and a bunch of other normalized states!
1 Like
I see, thank you very much Isaac! It is very clear now, I had previously thought measuring the expectation value of a quantum state collapses it to one of the bases of the state - for example consider |psi> = 1/sqrt(2) |0> + 1/sqrt(2) |1> ; measurement would collapse the wavefunction into |psi> = |0> or |psi> = |1> . How does Pennylane describe this idea (or what is the more formal way to describe it in words)?
1 Like
The idea of wavefunction collapse is kinda cool! There’s a few interpretations — read more here under History and Context. I don’t believe there’s a preferred interpretation stated anywhere in the PennyLane docs
.
1 Like