# Sz of each site for a spin model

**URL:** <https://itensor.discourse.group/t/sz-of-each-site-for-a-spin-model/99>\
**Category:** DMRG and Numerical Methods\
**Tags:** dmrg\
**Created:** [April 30, 2022, 11:17pm UTC](https://itensor.discourse.group/t/sz-of-each-site-for-a-spin-model/99 "2022-04-30T23:17:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fjjiang](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@fjjiang](https://itensor.discourse.group/u/fjjiang)\
**Post date:** [April 30, 2022, 11:17pm UTC](https://itensor.discourse.group/t/sz-of-each-site-for-a-spin-model/99/1 "2022-04-30T23:17:17Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [May 1, 2022, 4:03am UTC](https://itensor.discourse.group/t/sz-of-each-site-for-a-spin-model/99/2 "2022-05-01T04:03:01Z")

</div>

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
auto Sz = expect(psi,sites,"Sz");

```

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