# Compare ground state overlap of DMRG "psi" with ED ground state.

+1 vote

Dear ITensor

I need to compare the ground state "psi" output of itensor "DMRG" code, with my ED ground state!!

let's assume a chain with N=3 SpinHlaf=1/2 with dim=8. I save my ED result for these 8 numbers in an ascii file. I am looking a way to save DMRG "psi" result in a ascii file as ED ones.

Many thanks for any help or suggestion.

selected by

Hi, good question. Here's a sample code that can do this for you where I chose the case of four sites generated like auto sites = SpinHalf(4);. In the code below, psi is an MPS which could be from a DMRG calculation, say:

auto T = ITensor(1.0);
for(auto j : range1(N))
{
T *= psi(j);
}
auto P = permute(T,sites(1),sites(2),sites(3),sites(4));
Print(P);

for(auto n1 : range1(sites(1)))
for(auto n2 : range1(sites(2)))
for(auto n3 : range1(sites(3)))
for(auto n4 : range1(sites(4)))
{
printfln("%d %d %d %d %.12f",n1,n2,n3,n4,P.elt(n1,n2,n3,n4));
}


Does the above code do what you are looking for?

commented by (46.8k points)
If you want to skip the permute step, you can also just get the elements of T directly, like this:

T.elt(sites(1)(n1),sites(2)(n2),sites(3)(n3),sites(4)(n4));

and use that above piece of code in the printing statement instead. It's slower, though, because each time it has to check what order T's indices are in, whereas the call to permute guarantees that P's indices are in a certain order.
commented by (190 points)
Dear Miles
It is very useful and works well. Many thanks!!!
But the way to access bigger system size (e.g N=24) is tedious!!, Is there any trick to circumvent nested "for" loops?
commented by (46.8k points)
Yes, good question. So we have a feature called "iterInds" which is an iteration helper for iterating over ITensor elements. Here's how you can use it within the code above:

for(auto it : iterInds(P))
{
printfln("%.12f",P.elt(it));
}

What your question is making me realize we also need is:
(1) a version of T.permute which takes a site set such as the "sites" object above, or similar
(2) a way of printing  the "it" object in the iterInds loop above
commented by (190 points)
It is not working!!  please see below:
====================

dmrg.cc:220:20: error: use of undeclared identifier 'iterInds'; did you mean 'siteInds'?
for(auto it : iterInds(P))
^~~~~~~~
siteInds