# How to add a constant term in Hamiltonian(julia)

+1 vote

I have a dumb question... When constructing ampo, how to add a constant term? For example, in a 1D spin model, that 1/4 in terms like 0.5(Szi Szi+1 -1/4)?

I tried:
ampo = OpSum()
for i in 1:(N - 1)
ampo += 0.5, "Sz", i, "Sz", i + 1
ampo += -0.5/4
end

But doesn't work... I know the overall constant is trivial, but just want to know...
Thank you!

+1 vote
selected

No that's a pretty good question actually!

The short answer is that if you want an overall constant shift, you can do this:

ampo += -0.5/4,"Id",1


and importantly do that outside the loop so it's only a single shift. If you put that inside the loop if would be equivalent to adding a shift

ampo += -(N-1)*0.5/4,"Id',1


unless that's actually the behavior you want.

On a more big picture level, recall that a constant shift to a Hamiltonian really means a term which is the tensor product of the identity operator on every site. The way OpSum works when converting to an MPO is that any sites not included in a term are assumed to have the identity operator acting on them. So a term like "Sz",3 is really Id1 Id2 Sz3 Id4 Id_5 ....

We should add as a feature, though, the ability to just add a constant by itself as you tried. It's a nice suggestion and could be convenient in many cases.

Thanks,
Miles