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

**URL:** <https://itensor.discourse.group/t/how-to-program-a-large-number-of-sweeps-with-the-same-maxdim-more-conveniently/109>\
**Category:** ITensor Julia Questions\
**Created:** [May 3, 2022, 2:24pm UTC](https://itensor.discourse.group/t/how-to-program-a-large-number-of-sweeps-with-the-same-maxdim-more-conveniently/109 "2022-05-03T14:24:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinh](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kevinh/32/139_2.png) [@kevinh](https://itensor.discourse.group/u/kevinh)\
**Post date:** [May 3, 2022, 2:24pm UTC](https://itensor.discourse.group/t/how-to-program-a-large-number-of-sweeps-with-the-same-maxdim-more-conveniently/109/1 "2022-05-03T14:24:01Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [May 3, 2022, 3:23pm UTC](https://itensor.discourse.group/t/how-to-program-a-large-number-of-sweeps-with-the-same-maxdim-more-conveniently/109/2 "2022-05-03T15:23:27Z")

</div>

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`.

```julia
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”

```auto
[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).

---

<div class="post-metadata">

**Author:** ![kevinh](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kevinh/32/139_2.png) [@kevinh](https://itensor.discourse.group/u/kevinh)\
**Post date:** [May 4, 2022, 7:47am UTC](https://itensor.discourse.group/t/how-to-program-a-large-number-of-sweeps-with-the-same-maxdim-more-conveniently/109/3 "2022-05-04T07:47:33Z")

</div>

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