MethodError: no method matching size

Hello ITensor team,
When constructing an MPO with OpSum, shown in the example below, I get the following error, which I do not know how to debug. I am building an pretty unusual MPO, but in the minimal example, you can see the problem appearing in a minimal MPO. Could you give me a hint what the solution might be? Thank you very much in advance.

ERROR: MethodError: no method matching size(::StaticArraysCore.SVector{16, UInt16})

Minimal working example

using ITensors

function construct_id_operator_matrix(dim_charge_basis::Int=7, dim_harmonic_basis::Int=4)
    id_harmonic = I(dim_harmonic_basis)
    id_charge = I(dim_charge_basis)
    return kron(id_harmonic, id_charge)
end

dim_harmonic_basis = 4 
dim_charge_basis = 7
num_sites = 2

ITensors.op(::OpName"ID", ::SiteType"Boson",  dim_charge_basis::Int, dim_harmonic_basis::Int) = construct_id_operator_matrix(dim_charge_basis, dim_harmonic_basis)
sites = siteinds("Boson", num_sites, dim=dim_harmonic_basis*dim_charge_basis)

os = OpSum()
os += 0.5, "ID", 1
H = MPO(os, sites)

My Environment

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: 8 × Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)
(@v1.10) pkg> status
Status `~/.julia/environments/v1.10/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [336ed68f] CSV v0.10.14
  [a93c6f00] DataFrames v1.6.1
  [7073ff75] IJulia v1.24.2
  [0d1a4710] ITensorMPS v0.2.4
  [9136182c] ITensors v0.6.14
  [b964fa9f] LaTeXStrings v1.3.1
  [91a5bcdd] Plots v1.40.4
  [c3e4b0f8] Pluto v0.19.42
  [92933f4c] ProgressMeter v1.10.0
  [9ff05d80] TickTock v1.3.0

First, your minimal example gives me a different error ( LoadError: MethodError: no method matching op(::OpName{:ID}, ::SiteType{Qudit}, ::Int64))
Second, here is what I needed to do to get things to run

using ITensors
using LinearAlgebra: I

function construct_id_operator_matrix(dimtot::Int, dim_charge_basis::Int=7, dim_harmonic_basis::Int=4)
    @assert dimtot == dim_charge_basis*dim_harmonic_basis
    id_harmonic = I(dim_harmonic_basis)
    id_charge = I(dim_charge_basis)
    return kron(id_harmonic, id_charge)
end

dim_harmonic_basis = 4
dim_charge_basis = 7
num_sites = 2

ITensors.op(::OpName"ID", ::SiteType"Boson",  dim::Int) = construct_id_operator_matrix(dim)
sites = siteinds("Boson", num_sites, dim=dim_harmonic_basis*dim_charge_basis)

os = OpSum()
os += 0.5, "ID", 1
H = MPO(os, sites)

You’ll probably want to pass the two quantum numbers explicitly instead

2 Likes

Thank you very much! It’s working now, as expected. Sorry for not going through the ITensors.op documentation more carefully to avoid this question.

Also, the name of the operator defined in ITensor is "Id", not "ID". So if you try "Id" in your code it should work for any site type. (Probably we should add "ID" as an alias and make sure that "Id" is documented better.)

"I" can also be used as an alternative to "Id". @miles I’m not sure if we want to support "ID", "I", and also "Id", I think "I" and "Id" are sufficient (unless "ID" is commonly used in certain subfields).

Agreed. Maybe it’s just a documentation issue for the most part. I did list all the site type specific operators in the docs but the generic ones may be missing.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.