It looks like both approaches work fine. However, in the first approach, you are modifying psi
, and you are using the modified version of psi
in the second approach. To avoid this, you need to make a copy of the state before modifying it, for example:
using ITensors
N = 6
bond_dim=24
sites = siteinds("S=1/2",N)
psi0 = randomMPS(sites, bond_dim);
psi1 = randomMPS(sites, bond_dim);
j = 3
psi = copy(psi0)
s = siteind(psi,j)
newpsi= 2*op(s,"Sx") * psi[j]
noprime!(newpsi)
psi[j]= newpsi
println("First value is ", inner(psi1, psi))
sample = AutoMPO()
sample += 2,"Sx", j;
Operator= MPO(sample,sites);
println("Second value is ", inner(psi1, Operator, psi0))
which outputs for me:
First value is -0.14996338790894725
Second value is -0.14996338790894728
In the original post, in the second approach you are effectively applying the operator 2 Sz
twice to the original MPS psi
.
As I'm sure you know, it is best to use the first approach since it doesn't scale with the number of sites, whereas the second approach uses an MPO that scales with the number of sites.
-Matt