Edited Ques: @printf not printing anythin in following

Why @printf at the end is not printing anything for the following ?
Note iTensors is a package. So you might ignore the code.

using ITensors
using Printf
function ITensors.op(::OpName"expτSS", ::SiteType"S=1/2", s1::Index, s2::Index; τ)
h =
1 / 2 * op(“S+”, s1) * op(“S-”, s2) +
1 / 2 * op(“S-”, s1) * op(“S+”, s2) +
op(“Sz”, s1) * op(“Sz”, s2)
return exp(τ * h)
end
function main(; N=10, cutoff=1E-8, δτ=0.1, beta_max=2.0)
s = siteinds(“S=1/2”, N; conserve_qns=true)
gates = ops([(“expτSS”, (n, n + 1), (τ=-δτ / 2,)) for n in 1:(N - 1)], s)
append!(gates, reverse(gates))
rho = MPO(s, “Id”) ./ √2
terms = OpSum()
for j in 1:(N - 1)
terms += 1 / 2, “S+”, j, “S-”, j + 1
terms += 1 / 2, “S-”, j, “S+”, j + 1
terms += “Sz”, j, “Sz”, j + 1
end
H = MPO(terms, s)
for β in 0:δτ:beta_max
energy = inner(rho, H)
@printf(“β = %.2f energy = %.8f\n”, β, energy)
rho = apply(gates, rho; cutoff)
rho = rho / tr(rho)
end
return nothing
end

Hi @_SACHIN

Welcome to Julia :smiley:

It’s always good idea to

  • post a minimal working example
  • put your code in code block
  • post errors you get by running the mwe
  • post your expectations

The following code worked on my Julia 1.7

using Printf
δτ=1
beta_max=10
energy = 1.0
for β in 0:δτ:beta_max
    @printf("β = %.2f energy = %.8f\n", β, energy)
end

output:

β = 0.00 energy = 1.00000000
β = 1.00 energy = 1.00000000
β = 2.00 energy = 1.00000000
β = 3.00 energy = 1.00000000
β = 4.00 energy = 1.00000000
β = 5.00 energy = 1.00000000
β = 6.00 energy = 1.00000000
β = 7.00 energy = 1.00000000
β = 8.00 energy = 1.00000000
β = 9.00 energy = 1.00000000
β = 10.00 energy = 1.00000000

1 Like

Oops, I get ERROR: UndefVarError: @printf not defined on Julia 1.9.0?

My bad. You need

using Printf

And I’ve fixed it. Thanks @goerch !

1 Like

Might be OP’s problem then, too :slight_smile:

1 Like

no, I am using “using Printf”

The difference between @_SACHIN and @yashi is the line energy = inner(rho.H). The code blocks are not the same.

What is the value of energy?

1 Like
using ITensors
using Printf
function ITensors.op(::OpName"expτSS", ::SiteType"S=1/2", s1::Index, s2::Index; τ)
    h =
        1 / 2 * op("S+", s1) * op("S-", s2) +
        1 / 2 * op("S-", s1) * op("S+", s2) +
        op("Sz", s1) * op("Sz", s2)
    return exp(τ * h)
end
function main(; N=10, cutoff=1E-8, δτ=0.1, beta_max=2.0)
    s = siteinds("S=1/2", N; conserve_qns=true)
    gates = ops([("expτSS", (n, n + 1), (τ=-δτ / 2,)) for n in 1:(N - 1)], s)
    append!(gates, reverse(gates))
    rho = MPO(s, "Id") ./ √2
    terms = OpSum()
    for j in 1:(N - 1)
        terms += 1 / 2, "S+", j, "S-", j + 1
        terms += 1 / 2, "S-", j, "S+", j + 1
        terms += "Sz", j, "Sz", j + 1
    end
    H = MPO(terms, s)
    for β in 0:δτ:beta_max
        energy = inner(rho, H)
        @printf("β = %.2f energy = %.8f\n", β, energy)
        rho = apply(gates, rho; cutoff)
        rho = rho / tr(rho)
    end
    return nothing
end
julia> main()
β = 0.00 energy = 0.00000000
β = 0.10 energy = -0.17283071
β = 0.20 energy = -0.35297829
β = 0.30 energy = -0.53907929
β = 0.40 energy = -0.72957328
β = 0.50 energy = -0.92249295
β = 0.60 energy = -1.11609434
β = 0.70 energy = -1.30848387
β = 0.80 energy = -1.49784914
β = 0.90 energy = -1.68252460
β = 1.00 energy = -1.86105195
β = 1.10 energy = -2.03222170
β = 1.20 energy = -2.19509440
β = 1.30 energy = -2.34900281
β = 1.40 energy = -2.49353817
β = 1.50 energy = -2.62853829
β = 1.60 energy = -2.75405200
β = 1.70 energy = -2.87026069
β = 1.80 energy = -2.97746496
β = 1.90 energy = -3.07607951
β = 2.00 energy = -3.16658443

julia> 

Hey, How you are able to get the results successfully.

I am not able to get that.

β = 0.00 energy = 0.00000000 
ERROR: LoadError: MethodError: no method matching /(::MPO, ::Float64)
Closest candidates are:
/(::StridedArray{P, N} where N, ::Real) where P<:Dates.Period at /buildworker/worker    
/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/deprecated.jl:44
/(::Union{SparseArrays.SparseVector{Tv, Ti}, SubArray{Tv, 1, var"#s814",      
Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false} where var"#s814"     
<:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, SubArray{Tv, 1, var"#s814",  
Tuple{Base.Slice{Base.OneTo{Int64}}}, false} where var"#s814"     
<:SparseArrays.AbstractSparseVector{Tv, Ti}} where {Tv, Ti}, ::Number) at /buildworker/worker/   
package_linux64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/sparsevector.jl:1450
 /(::Complex, ::Real) at complex.jl:333

 ...
    
Stacktrace:
 [1] main(; N::Int64, cutoff::Float64, δτ::Float64, beta_max::Float64)
 @ Main /media/sachin/data/sachin/chumbak/simulations/itensors/scripts_org/test_p.jl:26
 [2] main()
  @ Main /media/sachin/data/sachin/chumbak/simulations/itensors/scripts_org/test_p.jl:11
 [3] top-level scope
 @ /media/sachin/data/sachin/chumbak/simulations/itensors/scripts_org/test_p.jl:30

Because the code you are running (I suppose it is the one you posted on stackoverflow) is substantially different from @yashi’s (which works for me).

Edit: added diff

No, I don’t think both are different. Btw, I also tried to run the same code which is posted here, and getting the same error. Could you please see to it.

@goerch

See if run the same as it is in yashi, then also I get the same error.

 using ITensors
 using Printf
 function ITensors.op(::OpName"expτSS", ::SiteType"S=1/2", s1::Index, s2::Index; τ)
     h =
          1 / 2 * op("S+", s1) * op("S-", s2) +
          1 / 2 * op("S-", s1) * op("S+", s2) +
          op("Sz", s1) * op("Sz", s2)
       return exp(τ * h)
 end
 function main(; N=10, cutoff=1E-8, δτ=0.1, beta_max=2.0)
 s = siteinds("S=1/2", N; conserve_qns=true)
 gates = ops([("expτSS", (n, n + 1), (τ=-δτ / 2,)) for n in 1:(N - 1)], s)
 append!(gates, reverse(gates))
 rho = MPO(s, "Id") ./ √2
 terms = OpSum()
 for j in 1:(N - 1)
     terms += 1 / 2, "S+", j, "S-", j + 1
    terms += 1 / 2, "S-", j, "S+", j + 1
    terms += "Sz", j, "Sz", j + 1
end
H = MPO(terms, s)
for β in 0:δτ:beta_max
    energy = inner(rho, H)
    @printf("β = %.2f energy = %.8f\n", β, energy)
    rho = apply(gates, rho; cutoff)
    rho = rho / tr(rho)
end
return nothing
end
main()

Pl tell me whether you are able to run the same ?

I copied the code you just posted, pasted and saved it to a new file and include-d it in the REPL. This works for me without problems.

Could you show us the error you are seeing somehow?

See the error : https://stackoverflow.com/questions/71837797/getting-an-error-in-running-a-julia-program

OK, when I run the stack overflow code I first get

ERROR: UndefVarError: H not defined

for

 @show inner(rho, H)

Removing that line I then see

ERROR: MethodError: no method matching *(::MPO, ::typeof(main))

here

  rho = apply(gates, rho; cutoff)main

Removing main from that line

  rho = apply(gates, rho; cutoff)

makes the program work for me.

oh! that was a typo in stackoverflow code.

I am getting the same error. could you please see the error and let me possibilities for it ?

Same program is working in REPL

but why now not on my computer ?

@goerch Looks like I have old version of itensors.

1 Like