mixed site Error

Hi!

When I change the Julia ITensor to the latest version, an error arises for my mixed site model, which works for previous versions. And I have not updated julia ITensor
for several months.
The minimal code I have tested is as follows:

using ITensors

function mixedSites(N::Int)
    sites = Vector{Index}(undef,N)
    for n=1:N
        if isodd(n)
            sites[n] = siteind("Electron"; addtags="n=$n", conserve_qns=true)
        else
            sites[n] = siteind("Boson"; addtags ="n=$n", dim = 4, conserve_qns=false) 
        end
    end
    return sites
end

let
    N = 40
    sites = mixedSites(N)
    state = Vector{String}(undef, N)
    for xy = 1 : N   
        if xy % 2 == 1    #elecreon site
            state[xy] = trunc(xy/2) % 2 == 0 ? "Up" : "Dn"
        else              #Boson site
            state[xy] = "1"  
        end
    end
    psi0 = randomMPS(sites, state; linkdims = 20)
end

There is an error " LoadError: MethodError: no method matching sort(::Tuple{Int64, Int64})", which arises from the randomMPS() function.

It should be pointed out that I already set the space in qudits.jl to return QN(qnname_number, 0) =>dim even if I set conserve_qns = false for “Boson” to use the QN of “Electron”. I think that such a modification will not alert the result.

It works when I change the code as:

    psi0 = MPS(sites, state)

I want to know how to fix this.

Best!

Zongsheng Zhou.

You’re right about needing to also modify the space function in qudits.jl or to use a custom one. But I think that could be the source of the error. Could you please also share how you modified it?

I found that the following modification of your code (without needing to change the space function) worked correctly:

# Changing the following line:
#sites[n] = siteind("Boson"; addtags ="n=$n", dim = 4, conserve_qns=false)
# Into this line:
sites[n] = Index(QN()=>4; tags="Boson,Site,n=$n")

Hi Miles,

Thanks for your prompt reply. In fact,

sites[n] = Index(QN()=>4; tags="Boson,Site,n=$n")

is my original code, referred from a previous discussion section. When I changed the Boson site as above, the problem still unsolved.

The following is my modification.

function ITensors.space(
  ::SiteType"Qudit";
  dim=2,
  conserve_qns=false,
  conserve_number=conserve_qns,
  qnname_number="Number",
)
  if conserve_number
    return [QN(qnname_number, n - 1) => 1 for n in 1:dim]
  else
    return [QN(qnname_number, 0) => dim]
  end
  return dim
end

I just added the else branch.

I see what you’re asking & it’s a good question. The difference I see is that you are writing

whereas I just used QN()=>dim. Could you please try that version with an empty argument to QN to see if that works?

I’m a bit confused myself because I would think those act rather similarly but maybe there’s a subtle difference that is mattering here. Also QN() better expresses the idea of “no quantum number”.

When I do the following:

change qudit.jl when conserve_qns=false

return [QN()=> dim]

and in my test file, I use

sites[n] = siteind("Boson"; addtags ="n=$n", dim = 4, conserve_qns=false)

The error still exists:ERROR: LoadError: MethodError: no method matching sort(::Tuple{Int64, Int64})

In all the cases listed above, the MPS(eltype::Type{<:Number}, sites::Vector{<:Index}) works.

psi0 = MPS(sites, state)

So I infer that certain freature conflict with mixed sites in the function randomMPS(eltype::Type{<:Number}, sites::Vector{<:Index}; linkdims=1)

Hi Miles,

It seems that the problem is due to the Julia package itself. When I change julia-1.10.2 to julia-1.8.5. The error disappears but with a warning: “MPS center bond dimension is less than requested (you requested 12, but in practice, it is 1. This is likely due to technicalities of truncating quantum number sectors.”

Thanks a lot for your reply.

Best!

Zongsheng Zhou

Glad it is working now!

Ideally users should never have to downgrade their Julia version to make ITensor code work, if that’s the case it should be considered a bug that we need to investigate.

@zhouzsh I would like to understand better why you had to downgrade your version of Julia to get things working. I can’t reproduce that issue you are seeing, here is what I am running:

using ITensors

function ITensors.space(
  ::SiteType"Qudit";
  dim=2,
  conserve_qns=false,
  conserve_number=conserve_qns,
  qnname_number="Number",
)
  if conserve_number
    return [QN(qnname_number, n - 1) => 1 for n in 1:dim]
  else
    return [QN() => dim]
  end
  return dim
end

function mixed_sites(N::Int)
  sites = Vector{Index}(undef,N)
  for n=1:N
    if isodd(n)
      sites[n] = siteind("Electron"; addtags="n=$n", conserve_qns=true)
    else
      sites[n] = siteind("Boson"; addtags ="n=$n", dim = 4, conserve_qns=false)
    end
  end
  return sites
end

let
  N = 40
  sites = mixed_sites(N)
  state = Vector{String}(undef, N)
  for xy = 1 : N
    if xy % 2 == 1    #elecreon site
      state[xy] = trunc(xy/2) % 2 == 0 ? "Up" : "Dn"
    else              #Boson site
      state[xy] = "1"
    end
  end
  psi0 = randomMPS(sites, state; linkdims = 20)
end

which outputs an MPS without any errors, though with the warning:

Warning: MPS center bond dimension is less than requested (you requested 20, but in practice it is 1. This is likely due to technicalities of truncating quantum number sectors.

The versions I’m using are:

julia> using Pkg; Pkg.status()
Status `~/Simons Foundation Dropbox/Matthew Fishman/Documents/workdir/ITensors.jl/discourse/1619_sort_bug/Project.toml`
  [9136182c] ITensors v0.3.66

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Does that code reproduce the issue you are seeing in Julia 1.10.2? If so, could you share the output of using Pkg; Pkg.status()?

Thanks for testing my code. Now I produce the same results as yours even if I upgrade my Julia package to 1.10.2. In fact, I have never modified the test code since the last time. Maybe it is due to the environment configuration issue after upgrading Julia and ITensor packages at the same time.

Best!
zhouzsh.

Great, thanks for checking, glad to hear everything works with Julia 1.10.2.