# 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 (51.7k 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 (51.7k 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.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3854282796382904, "perplexity": 2113.580687991819}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-10/segments/1614178385378.96/warc/CC-MAIN-20210308143535-20210308173535-00478.warc.gz"}