# std::bad_alloc when measuring two point correlation functions with periodic boundary condition.

Hi Miles,

I am working with a 20-site electron system with periodic boundary condition. I obtained the ground state with dmrg and measure the spin correlation functions following this formula. However, when the largest bond dim @@\gtrsim 500@@, I always got the error message

terminate called after throwing an instance of 'std::bad_alloc'


If I run the code in gdb, I got the following error message:

Program received signal SIGABRT, Aborted.
0x00007ffff640570f in raise () from /lib64/libc.so.6


This seems to be an issue of running out of memory. However, I have ~400G memory and this program seems only using @@\gtrsim@@10G memory. I am not sure whether this is an issue of my code or I really run out of memory. The relevant part of the code is the following function:

Real TwopointCorrelation(const Electron& sites, MPS& psi, string Opi, string Opj, int site_i, int site_j)
{
int N = length(sites);

auto op_i = op(sites,Opi,site_i);
auto op_j = op(sites,Opj,site_j);

//'gauge' the MPS to site i
//any 'position' between i and j, inclusive, would work here
psi.position(site_i);

//Create the bra/dual version of the MPS psi
auto psidag = dag(psi);

//Prime the link indices to make them distinct from

auto C = prime(psi(site_i),li_1) * op_i * prime(psidag(site_i),"Site");
for(int k = site_i+1; k < site_j; ++k)
{
C *= psi(k) * psidag(k);
}

C *= prime(psi(site_j),lj) * op_j * prime(psidag(site_j),"Site");

auto result = elt(C); //or eltC(C) if expecting complex

return result;
}


Thank you so much!
WunderNatur

commented by (70.1k points)
Hi, when you say you are working with periodic boundary conditions, do you mean just your Hamiltonian has PBC (site 1 connecting to site N) ? Or is your MPS also periodic too, as in having the topology of a circle instead of a chain? Both are valid approaches: I just ask because periodic MPS can incur a lot of extra memory costs.

Also did you print out the indices of the "C" tensor at each step? That could give really useful information about what's happening. For example there could be some indices that aren't matching correctly so that C is ending up with many indices instead of just 3.

Thanks,
Miles
commented by (70.1k points)
Let me also add that every time I've encounteed this error it's been because my system ran out of memory. So even if you don't think you are running out of memory, it's still possible that you are but it's just happening too suddenly for your system monitor (or whichever program you are using to track the memory usage) to report this correctly to you.

A good idea would be to see if the code produces a large tensor with many indices by mistake, as usually this is why tensor network codes suddenly run out of memory.