ITensor / ITensorMPS.jl

MPS and MPO methods based on ITensor (ITensors.jl)
Apache License 2.0
25 stars 8 forks source link

Issue running Trotter-Suzuki example #90

Open nikita-astronaut opened 1 month ago

nikita-astronaut commented 1 month ago

Dear ITensors developers, I hope you are doing well!

I ran into an issue with one of the examples trotter_suzuki_decomposition.py.

$ julia trotter_suzuki_decomposition.jl 
ERROR: LoadError: You are trying to add an ITensor with indices:

((dim=2|id=657|"S=1/2,Site,n=2")', (dim=2|id=657|"S=1/2,Site,n=2"), (dim=2|id=902|"S=1/2,Site,n=3")', (dim=2|id=902|"S=1/2,Site,n=3"))

into an ITensor with indices:

((dim=2|id=640|"S=1/2,Site,n=1")', (dim=2|id=640|"S=1/2,Site,n=1"), (dim=2|id=657|"S=1/2,Site,n=2")', (dim=2|id=657|"S=1/2,Site,n=2"))

but the indices are not permutations of each other.

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] _map!!(f::Function, R::NDTensors.DenseTensor{Float64, 4, NTuple{4, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}}, T1::NDTensors.DenseTensor{Float64, 4, NTuple{4, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}}, T2::NDTensors.DenseTensor{Float64, 4, NTuple{4, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}})
    @ ITensors ~/.julia/packages/ITensors/GkEpm/src/itensor.jl:1884
  [3] map!(f::Function, R::ITensor, T1::ITensor, T2::ITensor)
    @ ITensors ~/.julia/packages/ITensors/GkEpm/src/itensor.jl:1910
  [4] copyto!
    @ ~/.julia/packages/ITensors/GkEpm/src/broadcast.jl:310 [inlined]
  [5] materialize!
    @ ./broadcast.jl:878 [inlined]
  [6] materialize!
    @ ./broadcast.jl:875 [inlined]
  [7] _add(A::NDTensors.DenseTensor{Float64, 4, NTuple{4, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}}, B::NDTensors.DenseTensor{Float64, 4, NTuple{4, Index{Int64}}, NDTensors.Dense{Float64, Vector{Float64}}})
    @ ITensors ~/.julia/packages/ITensors/GkEpm/src/itensor.jl:1806
  [8] +(A::ITensor, B::ITensor)
    @ ITensors ~/.julia/packages/ITensors/GkEpm/src/itensor.jl:1814
  [9] ITensor(o::Sum{Scaled{Float64, Prod{Op}}}, s::Vector{Index{Int64}})
    @ ITensors.ITensorsOpsExt ~/.julia/packages/ITensors/GkEpm/src/lib/ITensorsOpsExt/src/ITensorsOpsExt.jl:41
 [10] main(N::Int64; nsteps::Int64, order::Int64)
    @ Main ~/trotter_suzuki_decomposition.jl:20
 [11] macro expansion
    @ show.jl:1229 [inlined]
 [12] top-level scope
    @ ~/trotter_suzuki_decomposition.jl:27
in expression starting at /home/nktastr_google_com/trotter_suzuki_decomposition.jl:27

Do you have an idea what could have gone wrong? Using the CUDA.jl backend. Thank you very much!

mtfishman commented 1 month ago

Very strange, thanks for the report. If I downgrade to Julia 1.6 and ITensors.jl v0.2.11 (before the existence of ITensorMPS.jl) that example code works, I guess something happened between then and now that broke that code.

EDIT: It looks like it was already broken back in ITensors.jl v0.3.68 (with the same error message).

mtfishman commented 1 month ago

It looks like this PR broke that code: https://github.com/ITensor/ITensors.jl/pull/919.

The issue is that this line is converting the OpSum of the Hamiltonian into an ITensor, which requires inserting identity operators when terms are not overlapping. We were doing that properly before https://github.com/ITensor/ITensors.jl/pull/919, but I guess that was causing some trouble for automatic differentiation libraries so I simplified that conversion code, but I simplified it too much and it no longer inserts identities properly.

Anyway, that code is pretty experimental so shouldn't really be relied on, I would recommend this tutorial: https://itensor.github.io/ITensors.jl/dev/tutorials/MPSTimeEvolution.html for a more reliable (but more manual) approach.

I'm going to delete that example for now to not cause confusion and keep this issue open as a reminder to fix it.