Applying down projection operator to Up spin site gives psi with "Sz" = NaN instead of 0.

,

Define the projection operators

using ITensors

function ITensors.op!(Op::ITensor,
                      ::OpName"Pup",
                      ::SiteType"S=1/2",
                      s::Index)
  Op[s'=>1,s=>1] = 1.0
end

function ITensors.op!(Op::ITensor,
                      ::OpName"Pdn",
                      ::SiteType"S=1/2",
                      s::Index)
  Op[s'=>2,s=>2] = 1.0
end

s = siteind("S=1/2")
Pup = op("Pup",s)
Pdn = op("Pdn",s)

Now if you apply the projection operators to psi with site that has expect(psi,“Sz”) that is not 0.5 or -0.5, it works as expected.

But if you apply Pdn to site with Sz=0.5, it gives psi with all entries NaN instead of flipping that spin to down.

For concrete example:

Consider

N = 100
s = siteinds("S=1/2",N)
psi_temp = MPS(s,"Up")
H1 = op("H",s[1])
psi1=apply(H1,psi_temp)
cnt12=op("CNOT", s[1:2]);
psi2=apply(cnt12,psi1);

Pup1 = op("Pup",s[1])
Pdn1 = op("Pdn",s[1])

Pup5 = op("Pup",s[5])
Pdn5 = op("Pdn",s[5])

Now,
expect(apply(Pup1,psi2),"Sz") or expect(apply(Pdn1,psi2),"Sz") gives correct result.

Also,
apply(Pup5,psi2) gives correct result.

But
Pdn5 = op("Pdn",s[5])
or any site other than 1 and 2 where spins are up does not give correct result in the sense when I do psin=apply(Pdn5,psi2) and expect(psin,"Sz"), it returns an array with all entries Nan.

How do I fix this?

Could you format your code? See Please Read: Make It Easier to Help You.

Thank you for the suggestion. I formatted it.

Hi, so I tried running your code but got an error around the line psi_temp = MPS(s,"Up"). What version of ITensors are you using, and can you please make sure you are upgraded to the latest version? If you do, does it fix the issue you are seeing?

Hi Miles,

I am sorry about this. I forgot to add N = 100 s = siteinds("S=1/2",N) in the code so you got error in line psi_temp = MPS(s,"Up").

I fixed the code. I am using version ITensors v0.3.51.

I am still getting NaN as the output for all sites when I do expect(psin,"Sz") where psin=apply(Pdn5,psi2).

Hi, so I found the problem. The norm of the state you are computing expected values within is zero. This is because the spin you act Pdn on is exactly in the up state, so projecting it to be down makes the state go to zero (even though the individual MPS tensors might have non-zero parameters in some of them).

So the expectation value here is not well-defined. I think what we should do on our end is check for this case inside the expect function and raise a warning or an error when it is the case.

I just created a PR that will put a check for this and throw an error inside expect if the norm is zero.