The issues of frequent recompilation and low computational efficiency in ITensor.jl

I’m a beginner in Julia and I’m using ITensor.jl for computations in my project. However, I’ve noticed that the program runs with very low efficiency, and both CPU and memory usage are minimal. I’m not sure how to optimize it to address this issue.

using ITensors, LinearAlgebra, DelimitedFiles, Plots
using Random, Combinatorics, Distributions

function sample_pairs(rng::AbstractRNG, N::Int, m::Int, alpha::Float64)
    all_pairs = collect(combinations(1:N, 2))
    dists = [min(j-i, N-(j-i)) for (i,j) in all_pairs]
    weights = alpha == 0.0 ? ones(length(dists)) : [r^(-alpha) for r in dists]
    probs = weights ./ sum(weights)
    cat = Categorical(probs)
    idxs = rand(rng, cat, m)
    return [all_pairs[k] for k in idxs]
end

ITensors.space(::SiteType"Replica") = 6
uq = readdlm("weingarten_6_6_6_6.csv", ',')
function ITensors.op(::OpName"Transfer", ::SiteType"Replica", s1::Index, s2::Index)
    itensor(Float64, reduce(vcat, uq), s2', s1', s2, s1)
end

ITensors.state(::StateName"FUp",   ::SiteType"Replica") = [1.,1.,1.,0.,0.,1.]
ITensors.state(::StateName"FDown", ::SiteType"Replica") = [1.,0.,0.,1.,1.,1.]

N = 16
NA = 4
tmax = 15

alphas = [0.0,]
thetas = [i * π/10 for i in 2:5]

num_samples = 50

cutoff = 1e-8
maxdim = 100

rng = MersenneTwister(10086)

sites = siteinds("Replica", N)
results = []

for alpha in alphas
    for theta in thetas
        c, s = cos(theta/2), sin(theta/2)
        ITensors.state(::StateName"Theta", ::SiteType"Replica") = [c^4; fill(c^2*s^2, 4); s^4]
        avg_P = zeros(tmax+1)
        avg_Ppr = zeros(tmax+1)
        for sample_id in 1:num_samples
            psi = productMPS(Float64, sites, "Theta")
            P_t = zeros(tmax+1)
            Ppr_t = zeros(tmax+1)
            for t in 0:tmax
                bnd = productMPS(Float64, sites, n -> (n <= NA ? "FDown" : "FUp"))
                P = inner(bnd, psi)
                val = 0.0 + 0.0im
                for k in 0:NA
                    phi = 2pi*k/(NA+1)
                    ITensors.state(::StateName"FDownk", ::SiteType"Replica") = [1.0, 0.0, 0.0, exp(1im*phi), exp(-1im*phi), 1.0]
                    bndk = productMPS(ComplexF64, sites, n -> (n <= NA ? "FDownk" : "FUp"))
                    val += inner(bndk, psi)
                end
                Ppr = abs(val)/(NA+1)
                P_t[t+1] = P
                Ppr_t[t+1] = Ppr
                pairs = sample_pairs(rng, N, N, alpha)
                opslist = [("Transfer", i, j) for (i, j) in pairs]
                mpo = ops(opslist, sites)
                psi = apply(mpo, psi; cutoff=cutoff, maxdim=maxdim)
            end
            avg_P .+= P_t
            avg_Ppr .+= Ppr_t
        end
        avg_P ./= num_samples
        avg_Ppr ./= num_samples
        delta_S2 = -log.(avg_Ppr ./ avg_P)
        for t in 0:tmax
            push!(results, (alpha, theta, t, delta_S2[t+1]))
        end
    end
end

open("results.csv", "w") do io
    println(io, "alpha,theta,t,delta_S2")
    for (alpha, theta, t, v) in results
        println(io, string(alpha, ",", theta, ",", t, ",", v))

end
end

Welcome to the community!

If you post a code example, please make sure to include the code in triple backticks, like ``` .

Otherwise the readability is very bad.

For more details, see: Please read: make it easier to help you

Hi ufechner7, many thx for you for the warm welcome and the tip!

Thanks for fixing the formatting of your example.

One more point: In your example you are reading a .csv file.

Can you share that file?

The first thing I noticed is the use of globals, that can decrease efficiency, but I don’t know whether it is the only reason.

You could try to use a statistical profiler to understand where is that you are spending most of the time

If you could also explain what is that you are trying to do, and how you are trying to do that (a commented code would be fine) we could help better

Finally, you may want to try to write in the Itensor discourse group

Hi, ufechner7, thanks for the reply, and here is the .csv file link: Richard/weingarten_6_6_6_6.csv at main · Veneme/Richard · GitHub.

1 Like

Hi Vince, This code is for simulating entanglement asymmetry (EA) dynamics in a 1D random quantum circuit of 16 six-level “replica” sites under a long-range random quantum circuit: at each of 15 time steps it samples N two-site gates with distance-dependent weight r^{-\alpha}, applies them to a product MPS initialized by an angle theta, then measures the overlap P(t), and a phase-averaged overlap P'(t) with a reference state to compute the second renyi entropy. Repeating this 50 times per theta, and averaging yields the growth dynamics of EA.

Small remark: You can use LaTeX in your messages, like r^{-\alpha} . Just put your LaTeX string between $ signs.

Thank you! ufechner7, and I have fixed it.

1 Like