Load error at sweep line

i was trying to implement a julia version of the C++ code given at DMRG · ITensors.jl. on running the code as a .jl file it kept giving an error at line 17 in the sweep section

using ITensors
using ITensors

function main()
    N = 4

    sites = SpinOne(N; conserve_qns=false)

    ampo = AutoMPO(sites)
    for j in 1:N-1
        ampo += 0.5, "S+", j, "S-", j+1
        ampo += 0.5, "S-", j, "S+", j+1
        ampo += "Sz", j, "Sz", j+1
    end
    H = toMPO(ampo)

    sweeps = Sweeps(5)
    sweeps.maxdim() = [10, 20, 30, 40, 50]
    sweeps.cutoff() = 1E-10

    psi0 = randomMPS(sites)

    energy, psi = dmrg(H, psi0, sweeps, Dict("Quiet" => true))

    println("\nj Sz = ")
    for j in 1:N
        position!(psi, j)
        ket = psi[j]
        bra = dag(prime(ket, "Site"))
        Szjop = op(sites, "Sz", j)
        szj = elt(bra * Szjop * ket)
        println("$j $szj")
    end

    totalSdS = 0.0

    println("\nj S.S = ")
    for b in 1:N-1
        position!(psi, b)
        bondket = psi[b] * psi[b+1]
        bondbra = dag(prime(bondket, "Site"))

        zzop = op(sites, "Sz", b) * op(sites, "Sz", b+1)
        pmop = 0.5 * op(sites, "S+", b) * op(sites, "S-", b+1)
        mpop = 0.5 * op(sites, "S-", b) * op(sites, "S+", b+1)

        zz = elt(bondbra * zzop * bondket)
        pm = elt(bondbra * pmop * bondket)
        mp = elt(bondbra * mpop * bondket)

        println("$b $(zz + pm + mp)")
        totalSdS += zz + pm + mp
    end

    println("\nSum of S.S = $totalSdS")
    println("Ground state energy from DMRG = $energy")

    return 0
end

main()

the code shows the following load error
ERROR: LoadError: syntax: Global method definition around /home/dennil/Desktop/Master’s thesis/four_spin.jl:17 needs to be placed at the top level, or use “eval”.
Stacktrace:
[1] top-level scope
@ ~/Desktop/Master’s thesis/four_spin.jl:3
in expression starting at /home/dennil/Desktop/Master’s thesis/four_spin.jl:3

could someone please help me resolve this error asap?

The problem is the syntax you’ve used for the sweeps object. In Julia, to modify something there is generally a ! in the function, and here to change the sweep parameters you can do

sweeps = Sweeps(5)
setmaxdim!(sweeps,10, 20, 30, 40, 50)
setcutoff!(sweeps,1E-10)

Checkout the documentation for more: Sweeps · ITensors.jl

2 Likes

Also, I wanted to note that we encourage users not to use the Sweeps object any more. While it is still supported for a short time longer, we encourage you to use the new array-based interface.

I noticed that the code at the link you provided, @dennil, already uses this newer interface. Is there a reason you were trying to use the older one?

Lastly, there’s no Dict("Quiet" => true) input recognized by our Julia DMRG code. Please see the documentation of the dmrg function to see which arguments are accepted. The one closest to that one would be the outputlevel keyword argument which is an integer controlling the amount of output printed.