# Out Of Memory Error()when using correlation_matrix()

+1 vote

Hi all,

I found that the built in function correlation_matrix() can lead to a Out Of Memory Error easyily even for a small size system with a moderate bond dimensions.

The minimal code can be like

ITensors.Strided.set_num_threads(1)

Nx = 4
Ny = 4
N = Nx * Ny
Npart = Int(Nx * Ny / 2)
t = 1.0
D = 600

Nsweep = 4
sweeps = Sweeps(Nsweep)
maxdim!(sweeps, D)
cutoff!(sweeps, 1E-8)

sites = siteinds("Electron", N; conserve_qns=true)
lattice = square_lattice(Nx, Ny; yperiodic=true)

ampo = AutoMPO()
for b in lattice
ampo .+= -t, "Cdagup", b.s1, "Cup", b.s2
ampo .+= -t, "Cdagup", b.s2, "Cup", b.s1
ampo .+= -t, "Cdagdn", b.s1, "Cdn", b.s2
ampo .+= -t, "Cdagdn", b.s2, "Cdn", b.s1
end

H = MPO(ampo, sites)

state = ["Emp" for n = 1:N]
p = Npart
for i = N:-1:1
if p > i
println("Doubly occupying site $i") state[i] = "UpDn" p -= 2 elseif p > 0 println("Singly occupying site$i")
state[i] = (isodd(i) ? "Up" : "Dn")
p -= 1
end
end

psi0_ini = randomMPS(sites, state, 10)
energy0, psi0 = dmrg(H, psi0_ini, sweeps)

Cupij = correlation_matrix(psi0,"Cdagup","Cup")


So for a Hubbard model on a 4*4 cylinder with bond dimension D = 600, I can readily obtain the ground state wavefunction, but get a out of memory error report.

I am not sure whthere I miss something when using the built-in function correlation_matrix().

Xin-Chi

commented by (70.1k points)
Thanks for posting your code - I'm testing to see what happens on my machine.

How much memory does your machine have? Thanks
commented by (70.1k points)
To follow up, I can see on my machine that the code is indeed using a seemingly unecessary amount or very large amount (20+ Gb) of memory for this! So thanks for reporting this behavior. It could very well be an issue and I'll look into it more.

Hi Xin-Chi,
Thanks again for reporting this. I found that there was indeed a bug in the correlation_matrix code. The bug was that certain tensor contractions were happening in the wrong order, leading to some intermediate tensors which were very large. I just submitting a pull request (https://github.com/ITensor/ITensors.jl/pull/754) which reorders these contractions. Now the code is much faster than before and uses a reasonable amount of memory (I measured only 500Mb usage or so with the new code).

This updated code will be included in the next version of ITensors.jl which I'll register later today.

Best regards,
Miles

commented by (190 points)
Hi Miles,
Thank you for replying and fixing this in such a short time!

Best
Xin-Chi
commented by (1.2k points)
I also found this issue yesterday and wrote my own correlation_matrix function. Thanks for fixing this.
commented by (70.1k points)
Glad you were able to find your own fix - but yes the new version is a lot faster and more efficient than the other one now.
commented by (600 points)
Well, I actually encountered the same problem recently...
I was planning to submit a pull request, and just noticed that Miles already submitted one : )

Anyway, I guess advanced users usually will write their own codes on the measurement part.

Best,
Junsen