# Edited Ques: @printf not printing anythin in following

**URL:** https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142
**Category:** New to Julia
**Created:** [April 7, 2022, 5:47am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142 "2022-04-07T05:47:20Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 7, 2022, 5:47am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/1 "2022-04-07T05:47:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)
#### Post date: [April 7, 2022, 5:59am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/2 "2022-04-07T05:59:03Z")

</div>

Hi @_SACHIN

Welcome to Julia 😃

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

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

```

output:

```julia
β = 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

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 7, 2022, 6:02am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/3 "2022-04-07T06:02:52Z")

</div>

> [@yashi](#):
>
> ```julia
> δτ=1
> beta_max=10
> energy = 1.0
> for β in 0:δτ:beta_max
> @printf("β = %.2f energy = %.8f\n", β, energy)
> end
> 
> ```

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

---

<div class="post-metadata">

### Author: ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)
#### Post date: [April 7, 2022, 6:06am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/4 "2022-04-07T06:06:32Z")

</div>

My bad. You need

```julia
using Printf

```

And I’ve fixed it. Thanks @goerch !

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 7, 2022, 6:07am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/5 "2022-04-07T06:07:55Z")

</div>

> [@yashi](#):
>
> `using Printf`

Might be OP’s problem then, too 🙂

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 7, 2022, 6:10am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/6 "2022-04-07T06:10:34Z")

</div>

no, I am using “using Printf”

---

<div class="post-metadata">

### Author: ![FrancisKing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisking/32/24966_2.png) [@FrancisKing](https://discourse.julialang.org/u/FrancisKing)
#### Post date: [April 7, 2022, 8:48am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/7 "2022-04-07T08:48:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yashi/32/19448_2.png) [@yashi](https://discourse.julialang.org/u/yashi)
#### Post date: [April 7, 2022, 12:29pm UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/8 "2022-04-07T12:29:41Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 5:00am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/10 "2022-04-12T05:00:23Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 12, 2022, 5:13am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/11 "2022-04-12T05:13:50Z")

</div>

> [@\_SACHIN](#):
>
> Hey, How you are able to get the results successfully.
> 
> I am not able to get that.

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/0/40ac46b2bc7728d6932f2221e3c1966bfe922793.png)

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 5:24am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/12 "2022-04-12T05:24:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 5:45am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/13 "2022-04-12T05:45:01Z")

</div>

@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()

```

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 5:46am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/14 "2022-04-12T05:46:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 12, 2022, 5:51am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/16 "2022-04-12T05:51:29Z")

</div>

> [@\_SACHIN](#):
>
> 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.

> [@\_SACHIN](#):
>
> See if run the same as it is in yashi, then also I get the same error.

Could you show us the error you are seeing somehow?

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 6:04am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/17 "2022-04-12T06:04:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 12, 2022, 6:12am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/18 "2022-04-12T06:12:29Z")

</div>

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

```julia
ERROR: UndefVarError: H not defined

```

for

```julia
 @show inner(rho, H)

```

Removing that line I then see

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

```

here

```julia
  rho = apply(gates, rho; cutoff)main

```

Removing `main` from that line

```julia
  rho = apply(gates, rho; cutoff)

```

makes the program work for me.

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 6:30am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/19 "2022-04-12T06:30:32Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 7:06am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/20 "2022-04-12T07:06:41Z")

</div>

Same program is working in REPL

but why now not on my computer ?

---

<div class="post-metadata">

### Author: ![\_SACHIN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_sachin/32/35272_2.png) [@\_SACHIN](https://discourse.julialang.org/u/_SACHIN)
#### Post date: [April 12, 2022, 11:10am UTC](https://discourse.julialang.org/t/edited-ques-printf-not-printing-anythin-in-following/79142/21 "2022-04-12T11:10:50Z")

</div>

@goerch Looks like I have old version of itensors.
