Sz of each site for a spin model

Hi all
I want to obtain the value of S_z at each site of a spin (SpinOne) model. The following
is what I did which is wrong since I get 1.0 as the S_z value for every site. Any help will be appreciated greatly.

auto state = InitState(sites);
auto psi0 = randomMPS(state);
auto [energy, psi] = dmrg(H, psi0, sweeps, “Quiet”);

fptr = fopen(argv[4],“a”);
for (int i = 1; i <= L; i++)
{
auto ampo_a = AutoMPO(sites);
ampo_a += 1, “Sz”, i;
auto ope = toMPO(ampo_a);
fprintf(fptr,"%.8f\n ",inner(psi,ope,psi));
}
fclose(fptr);

It’s hard to say why you are getting a different answer from what you expect, since your measurement code looks like it should give the correct value of S^z_i for the MPS you have. So most likely there is an error in your Hamiltonian definition, or your DMRG calculation is not finding the ground state (stuck in a local minimum) or not converged.

Separate from that, I would recommend that you use the function expect to measure local properties like S^z at each site. It is much more efficient than using an MPO for this purpose. You can use expect like this:

auto Sz = expect(psi,sites,"Sz");

and the returned value Sz will be a std::vector of real numbers.