Error by computing expectation value of Sy in S=1/2 and S=1

Hi everyone,
I am trying to compute the expectation value of the operator Sy with respect to a random MPS both for S=1/2 and S=1 and in both cases I get an error of the type InexactError: Float64(0.0 + 0.7071067811865475im) .
This is my code:

using ITensors

N=5
sites=siteinds("S=1",N)
psi=randomMPS(sites,10)

expect(psi,"Sy", sites=3)

Moreover, if I try to compute the expectation value in a more direct way

orthogonalize!(psi,3)
inner(psi[3],apply(op("Sy",sites,3),psi[3]))

the result is always 0 plus some imaginary error of the order e-19, e.g. 0.0 - 2.700806352051885e-19im.

On the contrary, for the other standard spin operators (S_x, S_+, …) the results are reasonable and do coincide.

Thank you in advance for your help!

Thanks for reporting this issue. It’s a bug in the expect function and we will fix it soon. (I’ve filed an issue).

Please use your other approach of applying the operator to the MPS directly for now. It is actually giving the correct output, since \langle \psi| S^y |\psi\rangle is always zero for a real-valued state |\psi\rangle.

If you are planning to perform many expectation value calculations on a larger MPS, then there are more efficient ways than your approach using inner. (Such as what expect does internally.) Just wanted to mention that, in case you continued using that approach and it caused any performance issues.

1 Like