+1 vote
asked by (350 points)

Hello iTensor team,

First i have to thank all iTensor team for keep this incredible package alive and growing.

Secondly, I'm trying to use an external file to study a ferrimagnetic system with Spin S=2. I follow the correct structure of external file, which give the following simple code

using ITensors
function ITensors.space(::SiteType"S=2";  conserve_qns=false)
if conserve_qns
    return [QN("Sz",4)=>1,QN("Sz",2)=>1,
  return 5

function ITensors.op!(Op::ITensor, ::OpName"Sz", 
::SiteType"S=2", s::Index)
Op[s'=>1,s=>1] = +2
Op[s'=>2,s=>2] = +1
Op[s'=>3,s=>3] = +0
Op[s'=>4,s=>4] = -1
Op[s'=>5,s=>5] = -2

function ITensors.op!(Op::ITensor, ::OpName"S+",
::SiteType"S=2", s::Index)
Op[s'=>1,s=>2] = 2
Op[s'=>2,s=>3] = sqrt(6)
Op[s'=>3,s=>4] = sqrt(6)
Op[s'=>4,s=>5] = 2

function ITensors.op!(Op::ITensor, ::OpName"S-",
::SiteType"S=2", s::Index)
Op[s'=>2,s=>1] = 2
Op[s'=>3,s=>2] = sqrt(6)
Op[s'=>4,s=>3] = sqrt(6)
Op[s'=>5,s=>4] = 2

ITensors.state(::StateName"Up+", ::SiteType"S=2") = [1.0, 0.0, 0.0, 0.0, 0.0]
ITensors.state(::StateName"Up", ::SiteType"S=2") = [0.0, 1.0, 0.0, 0.0, 0.0]
ITensors.state(::StateName"Z0", ::SiteType"S=2") = [0.0, 0.0, 1.0, 0.0, 0.0]
ITensors.state(::StateName"Dn", ::SiteType"S=2") = [0.0, 0.0, 0.0, 1.0, 0.0]
ITensors.state(::StateName"Dn-", ::SiteType"S=2") = [0.0, 0.0, 0.0, 0.0, 1.0]

The interesting part is, I'm receiving an error message "InexactError" from the line concerning the value of Sqrt(6). The funny part is when I tried using the same structure to construct the same external file for different spins (S=3/2, 5/2) everyting works fine. I'm not using the convert Julia function to convert any float number to an integer, so I don't know exactly what i had to change to make my code work.
Here follow the error message :

ERROR: LoadError: InexactError: Int64(2.449489742783178)
[1] Int64
@ ./float.jl:723 [inlined]
[2] convert
@ ./number.jl:7 [inlined]
[3] setindex!
@ ./array.jl:839 [inlined]
[4] setindex!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/NDTensors/tensorstorage.jl:25 [inlined]
[5] setindex!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/NDTensors/blocksparse/blocksparsetensor.jl:266 [inlined]
[6] setindex!!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/NDTensors/tensor.jl:325 [inlined]
[7] setindex!!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/qn/qnitensor.jl:11 [inlined]
[8] _setindex!!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/itensor.jl:855 [inlined]
[9] _setindex!!(::ITensors.NDTensors.BlockSparseTensor{Int64, 2, Tuple{Index{Vector{Pair{QN, Int64}}}, Index{Vector{Pair{QN, Int64}}}}, ITensors.NDTensors.BlockSparse{Int64, Vector{Int64},
@ ITensors /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/itensor.jl:911
[10] setindex!
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/itensor.jl:919 [inlined]
[11] op!(Op::ITensor, #unused#::OpName{Symbol("S+")}, #unused#::SiteType{S=2}, s::Index{Vector{Pair{QN, Int64}}})
@ Main /scratch/lmverissimo/Top2/2/N=128/S=2.jl:29
[12] op(name::String, s::Index{Vector{Pair{QN, Int64}}}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ ITensors /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/sitetype.jl:256
[13] op
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/sitetype.jl:221 [inlined]
[14] #op#902
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/sitetype.jl:332 [inlined]
[15] op
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/sitetype.jl:332 [inlined]
[16] (::ITensors.var"#calcQN#1004"{Vector{Index{Vector{Pair{QN, Int64}}}}, Dict{Pair{String, Int64}, ITensor}})(term::Vector{ITensors.SiteOp})
@ ITensors /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/autompo.jl:668
[17] qn
svdMPO(ampo::OpSum, sites::Vector{Index{Vector{Pair{QN, Int64}}}}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ ITensors /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/autompo.jl:676
[18] qn_svdMPO
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/autompo.jl:632 [inlined]
[19] #MPO#1007
@ /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/autompo.jl:973 [inlined]
[20] MPO(ampo::OpSum, sites::Vector{Index{Vector{Pair{QN, Int64}}}})
@ ITensors /scratch/lmverissimo/.julia/packages/ITensors/XnM47/src/physics/autompo.jl:966

Any help will be appreciated, Thank you !

1 Answer

+1 vote
answered by (70.1k points)

Hi, thanks for the question and the kind words about ITensor!

Most likely the error you are getting comes from setting your ITensor first with an integer value, then afterward trying to set a floating point value. When you have a line of code like

Op[s'=>2,s=>1] = 2

as the first non-zero element to be set, the storage type inside the ITensor ends up being an array of integers, rather than floating point values. So then when you set the second element to be sqrt(6), it's causing an error.

This is a behavior we might change in the future, making the storage type switch automatically in a case like yours.

But for now, please just change the code to use explicit floating point values like this:

Op[s'=>2,s=>1] = 2.0

or you could put the lines with sqrt(6) as the first lines of code.

Best regards,

commented by (350 points)
Oh I see !  It works perfectly, thank you again Miles !
commented by (70.1k points)
Glad it worked!
commented by (70.1k points)
Also if it's more convenient, we offer a newer style of op function overload where you leave out the Index from the arguments, and which returns a Matrix instead of an ITensor. So you could then explicitly write the matrix and return that, or make a matrix using `M = zeros(5,5)` and set its non-zero elements, then return it.

Section 10.2 of the ITensor Paper shows some examples of this style:
commented by (350 points)

Yes sure, I'll take a look.
 It seems more convenient to use Matrix Notation for sure...
Again, Thank you Miles!
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.