To measure total spin angular momentum S^2 on MPS

+1 vote
asked Sep 9 by Victor Chang (210 points)


Is there any possibility to measure total S^2 for a large system ?
Since the site operators can only be added with the same indexes, we would do expectations separately and sum over it. However, there are some cross terms which can be long range correlation measurement. Any suggestion for it?
Thank you so much!


1 Answer

+1 vote
answered Sep 9 by miles (16,920 points)

Hi Victor,
Interesting question. So yes, you can efficiently measure S^2 for a large system by representing the total S^2 operator as an MPO. (You can use the overlap(psi,S2,psi) function to efficiently compute the expectation value of an MPO S2 with respect to an MPS psi.)

Since this is something I've needed for projects in the past, I am pasting below a function which will generate an MPO for the total S^2 operator using a Hubbard site set to define the Hilbert space of the lattice. If you're doing a different kind of system, like a spin system, you should be able to adapt this code pretty straightforwardly for your case, but let me know if you encounter any problems. The main change you'd need to make is to define the QN objects going into the link/virtual indices to not have the "Nf" quantum number anymore.

The code below makes S2 as an IQMPO, but if you are not using quantum numbers you should be able just to convert the return value to an MPO like this:

MPO S2 = makeS2(sites);

Oh, and if this solves your problem, could I recruit you for a task? This would be a nice function to turn into a "code formula" to go on the ITensor website ( Could you do a brief writeup of it (see the source for the other code formulas here:
Of course you don't have to if you're too busy. Just an idea & I'll put your name on the contributors page with a badge.

Ok here is the function:

makeS2(Hubbard const& sites)
    auto N = sites.N();

    auto S2 = IQMPO(sites);

    auto links = std::vector<IQIndex>(N+1);
    for(auto n : range(N+1))
        { = IQIndex(nameint("L",n),

    for(auto n : range1(N))
        auto row = dag(;
        auto col =;
        auto& W = S2.Aref(n);
        W = IQTensor(row,col,dag(sites(n)),prime(sites(n)));

        W += sites.op("Id",n) * row(1) * col(1);
        W += sites.op("Id",n) * row(2) * col(2);

        W += sites.op("S2",n) * row(2) * col(1);

        W += 2*sites.op("Sz",n) * row(2) * col(3);
        W += sites.op("Id",n) * row(3) * col(3);
        W += sites.op("Sz",n) * row(3) * col(1);

        W += sites.op("S+",n) * row(2) * col(4);
        W += sites.op("Id",n) * row(4) * col(4);
        W += sites.op("S-",n) * row(4) * col(1);

        W += sites.op("S-",n) * row(2) * col(5);
        W += sites.op("Id",n) * row(5) * col(5);
        W += sites.op("S+",n) * row(5) * col(1);


    S2.Aref(1) *= setElt(;
    S2.Aref(N) *= setElt(dag(;

    return S2;
commented Sep 10 by Victor Chang (210 points)
Hi , Miles.
Thanks for your quick reply. I will try to figure it out. Yeah, I am willing to contribute.
Thank you so much!
commented 5 days ago by Victor Chang (210 points)
Hi, Miles.
Since I'm doing spin half and spin one systems. Is it too naive to get what I need by deleting all "Nf=",0 and changing Hubbard const& sites to SiteSet const& sites ?
commented 4 days ago by Victor Chang (210 points)
Hi, Miles.
I checked with my spin one and spin half small systems. It's correct.
commented 2 days ago by miles (16,920 points)
Hi Victor, I meant to write back to say yes just removing "Nf=",0 and changing the argument to a general SiteSet ought to do the trick. So I'm glad to hear it worked.

I may try to upgrade the QN system a bit (backwards compatible) to make it work more broadly across different system types. Currently if you had left in the "Nf=",0 it would have caused a runtime bug. Right now the QN class design is a trade off between convenience/robustness and speed.
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.