# Calculating Expectation Value And Correlator From MPS in ITensor Julia

Hi all,
I am trying to find expectation value of spin operator S_z in Heisenberg spin chain. Up until now, I have been using C++ ITensor format to get expectation value from MPS as described in following documentation: http://itensor.org/docs.cgivers=cppv3&page=formulas/correlator_mps

http://itensor.org/docs.cgi?vers=cppv3&page=formulas/measure_mps

I am not sure how do I define operators ( Ex: S_z acting at site j) acting at arbitrary sites and then take the expectation value in ITensor Julia version.
There is well defined way of defining operators in C++ ITensor like "auto Szjop = op(sites,"Sz",j) and gauging psi to measure at position j:
psi.position(j)
auto ket = psi(j);
auto bra = dag(prime(ket,"Site"))

I am having trouble in defining similar procedure in ITensor Julia version. I would like to know if it is possible to do same in ITensor Julia.
Thank you.

commented by (44.9k points)
Thanks, this is a great question. I’ll write a code formula soon about this and post a link to it in an answer below. The short version of the answer is that can do “Szjop = op(sites,”Sz”,j)” in Julia too (just remove the auto keyword); instead of “psi.position(j)” you do “orthogonalize!(psi,j)”; and finally instead of accessing an MPS tensor with “psi(j)” you do “psi[j]”.

Miles
commented by (220 points)
Hi Miles,
Thanks you so much. It worked. I used it following way:
psi=psi0;
orthogonalize!(psi,j)
psidag_j = dag(prime(psi[j], "Site"))
SPIN+ = scalar(psidag_j * op(sites, "S+", j) * psi[j])
SPIN- = scalar(psidag_j * op(sites, "S-", j) * psi[j])
SPINz= scalar(psidag_j * op(sites, "Sz", j) * psi[j])
Also, is there any command  to get complex output rather than scalar ( e.g.  to get expectation value of S_y or S_x) like "InnerC or eltC" defined in ITensor C++?
commented by (44.9k points)
Glad you figured it out! Also see the version I just posted below for a slightly different approach. (The main difference is that I retrieve the site index from the MPS instead of using the sites array.)

To answer your question about complex output, in the Julia version you can just use scalar and inner just as for real-valued ITensors and MPS. The reason is that in Julia you can have different return types of functions, whereas in C++ every function has to have a single return type. It’s one of many things we like better about Julia.