How to program a large number of sweeps with the same maxdim more conveniently

Hi itensor

Sometimes l need to keep bond dimesion=10 to sweeps 10 or 20 times, which I find very effective. But if I use something like maxdim!(sweeps,10,10,10,10,10........10,100,200,...) Repeatedly inputing 10 doesn’t seem very convenient. It seems that only Int can be input in maxdim! function. I don’t know if it can be written separately as follows

sweeps = Sweeps(20)
maxdim!(sweeps,10)
energy, psi = @time dmrg( H,psi0,sweeps)

sweeps = Sweeps(20)
maxdim!(sweeps,100,200,200,400,800,1000)
energy, psi = @time dmrg(H,psi,sweeps)

Or the itensor provides a better way to write the parameters need to repeat input.

Thanks for your enthusiastic answer in the itensor discourse group.

Good question. A newer interface to the ITensor dmrg function lets you use arrays for maxdim and cutoff and other parameters instead of the Sweeps object. Using arrays combined with built-in Julia features for making them lets you conveniently define things like DMRG calculations where many sweeps in a row have the same maxdim setting.

Below is a complete working example. Note how it doesn’t use the Sweeps type but instead just passes nsweeps, cutoff, maxdim as named arguments to the dmrg function. If one of these arguments, such as cutoff is a number then that number is used for every sweep. If it is an array then element n of that array is used for sweep number n.

using ITensors

let
  N = 100
  sites = siteinds("S=1", N)

  hterms = OpSum()
  for j in 1:(N - 1)
    hterms += "Sz", j, "Sz", j + 1
    hterms += 0.5, "S+", j, "S-", j + 1
    hterms += 0.5, "S-", j, "S+", j + 1
  end
  H = MPO(hterms, sites)

  psi0 = randomMPS(sites, 10)

  # Define DMRG accuracy parameters
  cutoff = 1E-10

  # Do 8 sweeps at maxdim=10, then 8 more at maxdim=20,
  # followed by maxdim of 40,80,200,400
  maxdim_10 = [10 for j=1:8]
  maxdim_20 = [20 for j=1:8]
  maxdim_rest = [40,80,200,400]
  # vcat is a Julia function that merges vectors:
  maxdim = vcat(maxdim_10,maxdim_20,maxdim_rest)
  @show maxdim
  nsweeps = length(maxdim)

  energy, psi = dmrg(H, psi0; nsweeps, cutoff, maxdim)
  println("Final energy = ", energy)
end

To do the thing you were asking about and repeat a number like 10 many times, I used an “array comprehension”

[10 for j=1:8]

or you could do fill(10,8) to make an array with the number 10 over and over.
Then I did a similar thing for the value 20 and finally merged the three arrays together using the Julia function vcat (vertical concatenate).

Thank you very much for your reply, this method is simple and effective!

1 Like