Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

NDTensors.jl incompatible with Optim.jl #62

Closed
jtschneider opened this issue Jan 26, 2021 · 4 comments · Fixed by #65 or #68
Closed

NDTensors.jl incompatible with Optim.jl #62

jtschneider opened this issue Jan 26, 2021 · 4 comments · Fixed by #65 or #68

Comments

@jtschneider
Copy link

Hello everybody,

I am trying to minimise a function of some variables and quantum state by ITensors.jl's means to measure an MPS. However, the Optim.jl package has a dependency on FiniteDiff.jl which in turn has a dependency on ArrayInterface.jl which in turn overloads the method setindex of NDTensors.jl. This throws an error when using the elementary method setelt() in ITensors.jl

An example:

julia> using ITensors, Optim

julia> i = Index(4)
(dim=4|id=70)

julia> setelt(i=>1)
ERROR: MethodError: setindex(::NDTensors.Tensor{Float64,1,NDTensors.Empty{Float64,NDTensors.Dense{Float64,Array{Float64,1}}},IndexSet{1,Index{Int64},Tuple{Index{Int64}}}}, ::Float64, ::Int64) is ambiguous. Candidates:
  setindex(T::NDTensors.Tensor{var"#s79",N,StoreT,IndsT} where StoreT<:NDTensors.Empty where IndsT where var"#s79"<:Number, x, I...) where N in NDTensors at ~/.julia/packages/NDTensors/YYVnF/src/empty.jl:99
  setindex(x::AbstractArray{T,1} where T, v, i::Int64) in ArrayInterface at ~/.julia/packages/ArrayInterface/hIHGL/src/ArrayInterface.jl:106
Possible fix, define
  setindex(::NDTensors.Tensor{T,1,StoreT,IndsT} where StoreT<:NDTensors.Empty where IndsT where T<:Number, ::Any, ::Int64)
Stacktrace:
 [1] setindex!!(::NDTensors.Tensor{Float64,1,NDTensors.Empty{Float64,NDTensors.Dense{Float64,Array{Float64,1}}},IndexSet{1,Index{Int64},Tuple{Index{Int64}}}}, ::Float64, ::Int64) at ~/.julia/packages/NDTensors/YYVnF/src/empty.jl:106
 [2] setindex!(::ITensor{1}, ::Float64, ::Int64) at ~/.julia/packages/ITensors/ka2Nl/src/itensor.jl:706
 [3] setelt(::Pair{Index{Int64},Int64}, ::Vararg{Pair{Index{Int64},Int64},N} where N) at ~/.julia/packages/ITensors/ka2Nl/src/itensor.jl:473
 [4] top-level scope at REPL[22]:1

I think it would be great to be able to use Optim.jl in conjuction with ITensors.jl. I have however no idea if the proposed solution by the compiler Possible fix, define setindex(::NDTensors.Tensor{T,1,StoreT,IndsT} where StoreT<:NDTensors.Empty where IndsT where T<:Number, ::Any, ::Int64) would be suitable

Cheers
Jan

@mtfishman
Copy link
Member

Sorry, I just noticed this. I'm not sure why I don't get notifications about issues raised, I'll have to look into my settings.

Thanks for pointing that out. It would be great to have ITensors working with Optim. I'll look into the best way to get it working (it may be just adding the solution that Julia recommends).

@mtfishman
Copy link
Member

This should be fixed on master and will be included in the next ITensors release (v0.1.38), please let me know if you continue to have problems like this.

@gamatos
Copy link

gamatos commented Feb 23, 2021

Hi,

I'm not sure whether I should be posting in this Issue or opening a new one - apologies if the latter. Today I tried using ITensors.jl to do some calculations and had this same problem. If I use a package which depends on ArrayInterface.jl and then try to run, for instance, the first half of the DMRG example script https://itensor.org/docs.cgi?vers=julia&page=getting_started/dmrg :

using ITensors
let
  N = 100
  sites = siteinds("S=1",N)

  ampo = AutoMPO()
  for j=1:N-1
    ampo += "Sz",j,"Sz",j+1
    ampo += 1/2,"S+",j,"S-",j+1
    ampo += 1/2,"S-",j,"S+",j+1
  end
  H = MPO(ampo,sites)
end

I get the same error. I fixed it by extending your fix to accept two integer arguments

# This is needed to fix an ambiguity error with ArrayInterface.jl
# https://github.com/ITensor/NDTensors.jl/issues/62
@propagate_inbounds function setindex(T::EmptyTensor, x, I::Int, J::Int)
  return _setindex(T, x, I, J)
end

I assume this happens because when constructing the MPO from the AutoMPO, ITensors needs to set two indices at once when instantiating the local operators, and the previous solution only fixed it when setting one index.

@mtfishman mtfishman reopened this Feb 23, 2021
@mtfishman
Copy link
Member

Thanks for pointing that out, I'm fixing it now (more generically by defining setindex(T::EmptyTensor, x, I::Int...)).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants