Unknown issue concerning diagITensor with adding MPSs?

I am trying to add two MPSs in a relatively straightforward fashion, as follows:

error_component = G*psi_temp
psi_temp_corrected = add(psi_temp, -1*error_component; maxdim=100, cutoff=1E-12)


I verified that both of these terms are of type MPS and have index agreement. Despite this, I am getting this error:

ERROR: LoadError: MethodError: no method matching diagITensor(::Float64,
::IndexSet{2,Index,Tuple{Index{Array{Pair{QN,Int64},1}},Index{Int64}}})
Closest candidates are:
diagITensor(::Number, ::Union{Tuple{Vararg{IndexT,N}},
IndexSet{N,IndexT,Tuple{Vararg{IndexT,N}}}} where IndexT where N) at
C:\Users\user\.julia\dev\ITensors\src\itensor.jl:358
diagITensor(::Number, ::Index...) at C:\Users\user\.julia\dev\ITensors\src\itensor.jl:363


By the way, I get this same error whether I use the "add" method or the "sum" method.

I tried to look into abstractmps.jl and itensor.jl to understand this, but I don't really understand what relevance these diagITensor methods play in addition. Any help on this would be appreciated.

commented by (49k points)
Hi Sujay, would it be easy to provide a complete, minimal sample code reproducing the issue? One reason I ask is because it might matter how the MPS were made. Also the error may not be coming from add per se but from the expression -1*error_component, for example. Thanks
commented by (9.7k points)
I agree with Miles that it would be helpful to have a minimal working example as well as the full error message (it is not clear to me how a diagITensor is involved in the above code, since it isn't usually in an MPS). Just a note that if you are using QNs, unfortunately adding QN MPS is broken right now: https://github.com/ITensor/ITensors.jl/issues/523
commented by (940 points)
OK, here is the thing I was working on. The main weird thing going on here is that the lattice has multiple different types of sites and some weird choices of quantum numbers (if you really want to know why I am trying this, I can explain). If I had to guess, the main thing that is probably causing the error is the weird quantum number stuff that I am trying to define, especially since I noticed some weird business when I tried to run DMRG on this state. However, the error when trying to add states shows up even if I just initialize a state and don't run DMRG, so I will open a separate question for that issue once I have more information on it. In any case, here is the code. It might seem like a lot, but a lot of the code is just to intialize a state given a list of strings representing the initial state.

# defines weird quantum number stuff
ferm_full = QN("Sz",-1) => 1
ferm_vac = QN("Sz",0) => 1
antiferm_full = QN("Sz",+1) => 1
antiferm_vac = QN("Sz",0) => 1
even_link_p1 = QN("Sz",+2) => 1 # p1 = "positive 1"
even_link_n1 = QN("Sz",-2) => 1 # n1 = "negative 1"

# defines sites, given number of spatial sites
function Kogut_Susskind_sites(num_spatial_sites)
sites = Index[]
for i = 1:num_spatial_sites
push!(sites, Index(ferm_full,ferm_vac; tags="Site,S=1/2,n=$(4*i-3)")) push!(sites, Index(odd_link_p1,odd_link_0,odd_link_n1; tags="Site,S=1,n=$(4*i-2)"))
push!(sites, Index(antiferm_full,antiferm_vac; tags="Site,S=1/2,n=$(4*i-1)")) push!(sites, Index(even_link_p1,even_link_0,even_link_n1; tags="Site,S=1,n=$(4*i)"))
end
return sites
end

# returns a state vector for a given list of link values
if length(link_value_list) % 2 == 1
error("number of links must be even (twice the number of spatial sites).")
end
num_spatial_sites = length(link_value_list) ÷ 2 # division symbol denotes integer division
basis_state = ["Up" for n = 1:4*num_spatial_sites] # just to initialize
for j=1:2*num_spatial_sites
# find status of particle (fermion/antifermion/vacuum) at site immediately before
if j == 1
basis_state[2*j-1] = "Dn"
basis_state[2*j-1] = "Up"
else
error("invalid link configuration: change not equal to 0 or -1 at fermion site")
end
elseif j % 2 == 1
basis_state[2*j-1] = "Dn"
basis_state[2*j-1] = "Up"
else
error("invalid link configuration: change not equal to 0 or -1 at fermion site")
end
else # j % 2 == 0
basis_state[2*j-1] = "Up"
basis_state[2*j-1] = "Dn"
else
error("invalid link configuration: change not equal to 0 or +1 at antifermion site")
end
end

basis_state[2*j] = "Up"
basis_state[2*j] = "Z0"
basis_state[2*j] = "Dn"
else
error("invalid link configuration: all link values must be +1, 0, or -1")
end
end
return basis_state
end

# initialize state and try to add state to itself
num_spatial_sites = 2
sites = Kogut_Susskind_sites(num_spatial_sites)
init_state = ["Dn", "Z0", "Up", "Z0", "Dn", "Z0", "Up", "Z0"] # fully empty lattice
psi0 = productMPS(sites, init_state)
psi_test = add(psi0, psi0; maxdim=100, cutoff=1E-12)
commented by (9.7k points)
As I said in my previous comment, there is currently a bug in adding QN MPS: https://github.com/ITensor/ITensors.jl/issues/523

(though there may be issues that are specific to your problem as well). I am focusing on some other things right now so won't have time to fix this bug in the next few weeks. However, often times you can avoid actually adding two MPS by rewriting your equations. For example, if you wanted to compute:

phi = psi1 + psi2
inner(psi3, phi)

inner(psi3, psi1) + inner(psi3, psi2)

This may not be applicable to your particular use case, however.
commented by (940 points)
Ah, I see. Thank you so much! Yeah, my particular use case does require me to actually construct the state resulting from adding states together. But that's all right--I guess I can wait until you have this bug fixed?
commented by (9.7k points)
I started working on fixing addition of QN MPS/MPO. It looks like it is working, I just need to test it more thoroughly and add it to the official ITensors.jl package.
commented ago by (49k points)
This is now implemented in the latest version of ITensors.jl. You can view the closed issue here:
https://github.com/ITensor/ITensors.jl/issues/523

Could you please update your version of ITensors.jl to v0.1.26 (using julia> ] up ITensors) and try out your code again?