Qudits/Boson observable expectation problems

Hello, I am using ITensor with Julia, and I have a question regarding measuring expectation on Bosons or Qudits. I have searched for many hours and chated with GPT but there are always issues, so I wanted to ask my question here. Thank you in advance for any help you could provide me.

My goal is start with some bosonic state using qudits, and do some real-time evolution with an AutoMPO Hamiltonian and then measure some observable (both local observable at each site and global one).

My issue is with the measurement using “expect” function.

using ITensors

L = 20
Nmax = 10
sites = siteinds("Boson", L; dim=Nmax+1)

# Initial vacuum state
state = ["0" for n in 1:L]
ψ0 = productMPS(sites, state)

# Measure <A> at site 2
a_exp = expect(ψ0, "A", 2; sites=sites)
println("⟨A⟩ at site 2: ", a_exp)

this gives me errors like
“”"
MethodError: no method matching expect(::MPS, ::String, ::Int64; sites::Vector{Index{Int64}})
The function expect exists, but no method is defined for this combination of argument types.
“”"

Ideally I also like to build an local operator and do things automatically like I was able to do with qubits, but there are also issues

function measure_phi(ψ::MPS, sites::Vector{<:Index}, j::Int, m::Real)
    op_phi = (op(sites, "A", j) + op(sites, "Adag", j)) / sqrt(2 * m)
    return expect(ψ, op_phi; sites=sites)
end

measure_phi(ψ0, sites, 2, 1.0)

this gives me error like
“”"
ArgumentError: invalid index: (dim=4|id=1|“Boson,Site,n=1”) of type Index{Int64}
“”"

(Note, please ensure you have updated your code such that you need to have using ITensorMPS as well as ITensors)

To measure A on site 2 only, you can do either

a_exp_2 = expect(ψ0, "A"; sites = [2])

or

# measure A on every site, report site 2
a_exp = expect(ψ0, "A"; sites = 1:L)
println("⟨A⟩ at site 2: ", a_exp[2])

For the second part, I would suggest defining a custom operator (see Physics (SiteType) System Examples · ITensorMPS.jl ) and calling expect on using the new custom operator with either of the above two methods