# Possible error in ITensor Library v3 - mpoalgs.cc(?)

edited

I was noticing some strange results after using the function nmultMPO and so I decided to look directly the mpoalgs.cc file.

It looks to me that, in line 60 there should be
auto lB = linkInds(B);
insted of
auto lB = linkInds(A);

am I wrong?

commented by (8.5k points)
Indeed, that does look like a mistake on that line, thanks for pointing it out. I'll push a fix for that.

Could you please post a minimal code example using nmultMPO that is leading to incorrect results for you, so that we can make sure it fixes any problems you are seeing?
commented by (220 points)

I was computing the variance of the Hamiltonian H measured on a state psi. I think it would be too long to post the details, but the part of interest is:

MPO H2 = MPO(sites);
H2 = nmultMPO(prime(H),H,{"MaxDim",1000,"Cutoff",1E-13});
double variance = inner(psi, H2, psi)  -  inner(psi, H, psi) *  inner(psi, H, psi)  ;
commented by (8.5k points)
I'm a bit confused about what is going on here. I ran the following minimal code on v3:

int N = 5;
auto sites = SpinHalf(N,{"ConserveQNs",false});
auto ampo = AutoMPO(sites);
for(auto j : range1(N-1))
{
ampo += 0.5,"S+",j,"S-",j+1;
ampo += 0.5,"S-",j,"S+",j+1;
ampo +=     "Sz",j,"Sz",j+1;
}
auto H = toMPO(ampo);
auto psi = randomMPS(sites);
auto H2 = nmultMPO(prime(H), H);
PrintData(inner(psi, H2, psi) - inner(H, psi, H, psi));

and it runs fine (note that inner(H, psi, H, psi) is an alternative way to calculate inner(psi, H2, psi) without using nmultMPO). After I change that line in mpoalgs.cc as you suggested (which I believe is correct), the code above also runs correctly. Either way, all of the unit tests pass.

Could you give a more complete minimal code that is showing issues? Before making the change, I would like to test if it fixes the issue you are seeing (or if perhaps you have another issue).
commented by (8.5k points)
Alternatively, if it is difficult to create a minimal example, could you checkout the branch nmultmpo_bugfix I created that makes the suggested change (or just make the change locally) and see if it fixes the problem you are seeing?
commented by (220 points)
I have just discovered that my issue was related to another error (not related to the library itself).

Nevertheless, I think that in this case there would be no problem because the two operators that you are multiplying are the same, and so the (possible) error doesn't arise.

I think that a possible check can be to calculate the expectation value of an operator C, given by C=A*B with A different from B.
commented by (8.5k points)
Thanks for the update.

Oddly enough, the unit tests already have checks for cases like that: https://github.com/ITensor/ITensor/blob/v3/unittest/mpo_test.cc#L516

I think I understand why it might not be causing issues. The only place those link indices are used are to initialize an input ITensor for a later call to denmatDecomp. I think denmatDecomp is only using the indices of the other ITensor that is input, so those indices aren't really being used at all. Anyway, it is probably good to fix it since at the very least it is confusing.