0 votes
asked by (1.1k points)

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, 
Closest candidates are:
  diagITensor(::Number, ::Union{Tuple{Vararg{IndexT,N}}, 
IndexSet{N,IndexT,Tuple{Vararg{IndexT,N}}}} where IndexT where N) at 
  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 (70.1k 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 (14.1k 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 (1.1k 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_0 = QN("Sz",0) => 1
even_link_n1 = QN("Sz",-2) => 1 # n1 = "negative 1"
odd_link_p1 = QN("Sz",-2) => 1
odd_link_0 = QN("Sz",0) => 1
odd_link_n1 = QN("Sz",+2) => 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)"))
  return sites

# returns a state vector for a given list of link values
function basis_state(link_value_list)
  if length(link_value_list) % 2 == 1
    error("number of links must be even (twice the number of spatial sites).")
  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
      if link_value_list[j] - link_value_list[2*num_spatial_sites] == 0
        basis_state[2*j-1] = "Dn"
      elseif link_value_list[j] - link_value_list[2*num_spatial_sites] == -1
        basis_state[2*j-1] = "Up"
        error("invalid link configuration: change not equal to 0 or -1 at fermion site")
    elseif j % 2 == 1
      if link_value_list[j] - link_value_list[j-1] == 0
        basis_state[2*j-1] = "Dn"
      elseif link_value_list[j] - link_value_list[j-1] == -1
        basis_state[2*j-1] = "Up"
        error("invalid link configuration: change not equal to 0 or -1 at fermion site")
    else # j % 2 == 0
      if link_value_list[j] - link_value_list[j-1] == 0
        basis_state[2*j-1] = "Up"
      elseif link_value_list[j] - link_value_list[j-1] == +1
        basis_state[2*j-1] = "Dn"
        error("invalid link configuration: change not equal to 0 or +1 at antifermion site")

    # fill in link value
    if link_value_list[j] == +1
      basis_state[2*j] = "Up"
    elseif link_value_list[j] == 0
      basis_state[2*j] = "Z0"
    elseif link_value_list[j] == -1
      basis_state[2*j] = "Dn"
      error("invalid link configuration: all link values must be +1, 0, or -1")
  return basis_state

# 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 (14.1k 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)

you could instead compute:

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

This may not be applicable to your particular use case, however.
commented by (1.1k 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 (14.1k 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 by (70.1k points)
This is now implemented in the latest version of ITensors.jl. You can view the closed issue here:

1 Answer

0 votes
answered by (14.1k points)
edited by

I've implemented a new algorithm for adding MPS/MPO which now works with QNs here: https://github.com/ITensor/ITensors.jl/pull/528

We will release a new version of ITensors.jl that includes this improvement tomorrow. Hopefully this fixes the problem you are seeing. Please try it out after it is released and update us here or with an issue on Github if you keep seeing problems after updating your ITensors.jl version.

commented by (14.1k points)
Hi Sujay,

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

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.