Schrodinger
Schrodinger

Reputation: 141

Why two styles of executing Juilia programs are giving different results?

If a run a program written in julia as

sachin@localhost:$ julia mettis.jl then it runs sucessfully, without printing anything, though one print statement is in it.

And Secondly If run it as by going in julia:

sachin@localhost:$ julia
julia> include("mettis.jl")
main (generic function with 1 method)`
julia> main()

Then It gives some error.

I am puzzled why two style of executing is giving different result ?

Here is my 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)

# Make an array of 'site' indices
s = siteinds("S=1/2", N; conserve_qns=true)
#  @show s
# Make gates (1,2),(2,3),(3,4),...
 gates = ops([("expτSS", (n, n + 1), (τ=-δτ / 2,)) for n in 1:(N - 1)], s)
 # Include gates in reverse order to complete Trotter formula
 append!(gates, reverse(gates))

 # Initial state is infinite-temperature mixed state
 rho = MPO(s, "Id") ./ √2
 @show inner(rho, H)
 # Make H for measuring the energy
 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)

# Do the time evolution by applying the gates
# for Nsteps steps
for β in 0:δτ:beta_max
  energy = inner(rho, H)
  @printf("β = %.2f energy = %.8f\n", β, energy)
  rho = apply(gates, rho; cutoff)main
  rho = rho / tr(rho)
end
#  @show energy
 return nothing
end

Upvotes: 2

Views: 108

Answers (1)

ahnlabb
ahnlabb

Reputation: 2162

There is nothing special about a function called main in Julia and defining a function is different from calling it. Consequently a file mettis.jl with the following code:

function main()
    println("Hello, World!")
end

will not "do" anything when run (julia mettis.jl). However if you actually call the function at the end:

function main()
    println("Hello, World!")
end
main()

you get the expected result

$ julia mettis.jl
Hello, World!

Upvotes: 3

Related Questions