# Multi qubit expectation values

Hi everyone!

I have a bit of theoretical curiosity relating to multi-qubit projective measurement (let’s say with PauliZ). For a two-qubit circuit, from my understanding, the measurement matrix will look something like this: Z⊗Z, and the shape of the output is (1,1) or a singular value. In PennyLane, there’s an option to measure each qubit separately, so for this particular circuit, the output shape will be something like (1,2) or two distinct values for the two qubits.

For the second case (i.e., measuring multiple qubits separately), what is the math underneath, especially in terms of matrices?

Is it something like this:

• For qubit 1: Z⊗I
• For qubit 2: I⊗Z

Also, if the measurement happens one qubit after the other, how does PennyLane handle “wavefunction collapse”?

Could you please share the code you’re using? I just want to make sure I’m understanding your question correctly.

Please visit the following post for more clarification:

Explanation post and code

A dummy code:

def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0, 1])
qml.RY(y, wires=1)
return [qml.expval(qml.PauliZ(i)) for i in range(2)]


Here, how are the two expvals are calculated theoretically?

Thanks for adding the code here @Nabil_Anan_Orka . Let me get back to you on this very soon.

Really good question

Theoretically what is done is to generate the state from the gates of the circuit. Imagine then that you want to calculate the expected value with respect to the observable Z in the ith qubit. What we do is to calculate the probability of obtaining 0 and 1 respectively in that qubit and we calculate P(0) - P(1).
Why P(0)- P(1)? Because the eigenvalue of |0\rangle is 1 and that of |1\rangle is -1.

If you had the observable ZxZ, the expected value would be P(00) - P(01) - P(10) + P(11), since the eigenvalues of |00\rangle, |01\rangle, |10\rangle and |11\rangle are 1,-1,-1 and 1 respectively.

if you define an arbitrary state |\phi\rangle = (\alpha_1, \alpha_2, \alpha_3, \alpha_4) and do the math (\langle \phi | Z\otimes Z |\phi \rangle ), you will see that this is what I said before

I hope that helps!

2 Likes

However, I still have some questions regarding the circuit:

After performing the quantum operations, the resulting state is |ψ⟩ = a|00⟩ + b|01⟩ + c|10⟩ + d|11⟩.

Here, |ψ⟩ is a column vector (matrix) of shape (4,1). I need to calculate the probabilities:

1. Pr(0) = ⟨0|ψ⟩
2. Pr(1) = ⟨1|ψ⟩

How do I calculate these probabilities and then find the difference Pr(0) - Pr(1) in this context, especially considering ⟨0| and ⟨1| are row vectors (matrices) of shape (1,2)?

Given |\psi\rangle we can calculate P(|00\rangle) = a^2, P(|01\rangle) = b^2, P(|10\rangle) = c^2 and P(|11\rangle) = d^2