Hi,
Thanks a lot for the reply.
Let me try to clarify my point.
1) My codes are untouched. Before the latest update, they were giving different outputs. Of course, this doesn't necessarily mean there is a bug now.
2) What I do is to run DMRG with an Hamiltonian H, obtaining the groundstate wavefuntion psi0. Then, I want to compute <psi0 | S^2 |psi0>. In order to do so, I create an MPO of the S^2 operator and compute inner(psi0,S2,psi0).
3) The S^2 operator can be written as S^2 = Sp*Sm + Sz^2 - Sz. Of course, I need to sum over all sites, so that Sp = sum_i Sp(i), Sm = sum_i Sm(i) and Sz = sum_i Sz(i). Back in the day, it was not possible to multiply autoMPOs, so I was using the first option (check this reply from Miles,
http://itensor.org/support/546/possible-multiply-autompo-objects-before-converting-iqmpos, where I got this trick from). The second option that I sent is *wrong*. My bad, I am really sorry! You are correct, these are not the same operators and I messed it up.
4) Below, I send a code where I compute <psi0 | S^2 |psi0> for the S=1 antiferromagnetic Heisenberg dimer and I do not obtain 0 (EDIT: it should be 0 because the groundstate is a singlet). I also compute <psi0 | Sz | psi0> and I obtain 0, as expected. However, I also compute <psi0 | Sz^2 |psi0> (using the same trick) and I do not obtain 0, which is weird.
Code:
using ITensors
# Hamiltonian: Heisenberg, first neighbor
function H_Heis_1n(J,Nsites,sites)
ampo = AutoMPO()
#J
for i in 1:Nsites-1
ampo += -J,"Sz",i,"Sz",i+1
ampo += -J/2,"S+",i,"S-",i+1
ampo += -J/2,"S+",i+1,"S-",i
end
H = MPO(ampo,sites)
return H
end
# S^2 operator
function S2_op(Nsites,sites)
ampo = AutoMPO()
for i in 1:Nsites
for j in 1:Nsites
ampo += 1.,"S+",i,"S-",j
ampo += 1.,"Sz",i,"Sz",j
end
ampo += -1.,"Sz",i
end
S2 = MPO(ampo,sites)
return S2
end
# Sz operator
function Sz_op(Nsites,sites)
ampo = AutoMPO()
for i in 1:Nsites
ampo += 1.,"Sz",i
end
Sz = MPO(ampo,sites)
return Sz
end
# Sz^2 operator
function Sz2_op(Nsites,sites)
ampo = AutoMPO()
for i in 1:Nsites
for j in 1:Nsites
ampo += 1.,"Sz",i,"Sz",j
end
end
Sz2 = MPO(ampo,sites)
return Sz2
end
# physical parameters
N = 2
J = -1. #energy in units of |J|
# DMRG parameters
sweeps0 = Sweeps(10)
maxdim!(sweeps0, 20,50,100,100,200)
cutoff!(sweeps0, 1E-5,1E-5,1E-5,1E-8,1E-8)
# system
Nsites = N
sites = siteinds("S=1",Nsites)
# initial state
ψi = randomMPS(sites)
# Hamiltonian
H = H_Heis_1n(J,Nsites,sites)
# run DMRG
## groundstate
E0,ψ0 = dmrg(H,ψi,sweeps0)
# S^2 operator
S2 = S2_op(Nsites,sites)
# Sz operator
Sz = Sz_op(Nsites,sites)
# Sz^2 operator (TEST)
Sz2 = Sz2_op(Nsites,sites)
# outputs
println("S^2 for groundstate = ", inner(ψ0,S2,ψ0))
println("Sz for groundstate = ", inner(ψ0,Sz,ψ0))
println("Sz^2 for groundstate = ", inner(ψ0,Sz2,ψ0))