BoundsError: attempt to access 0-element Vector{Int64} at index [1]

I encountered this error while using apply for TEBD, but I have already “grad” the sites of the MPS as described in this link.

function measure_pauli(mps::MPS, basis::Vector{Int})
    psi = copy(mps)
    N = length(psi)
    sites = siteinds(psi)

    x_inds = findall(x -> x == 1, basis)
    y_inds = findall(x -> x == 2, basis)

    gates_x = [op("H", sites[i]) for i in x_inds]
    gates_y1 = [op("Phase", sites[i])' for i in y_inds]
    gates_y2 = [op("H", sites[i]) for i in y_inds]

    if !isempty(gates_x)
        psi = apply(gates_x, psi)
    end
    if !isempty(gates_y1)
        psi = apply(gates_y1, psi)
    end
    if !isempty(gates_y2)
        psi = apply(gates_y2, psi)
    end


    result = sample!(psi)

    return result
end

function classical_shadows(mps::MPS, num_samples=500)
    N = length(mps)
    bases = Matrix{Int}(undef, num_samples, N)
    snapshots = Matrix{Int}(undef, num_samples, N)

    for t in 1:num_samples
        basis = rand(1:3, N)
        bases[t, :] = basis
        snapshots[t, :] = measure_pauli(mps, basis)
    end

    return snapshots, bases
end

N = 14
T = 100

sites = siteinds("Qubit", N)
psi = randomMPS(sites, 2)
snapshots, bases = classical_shadows(psi, T)

here is the error report

Stacktrace:
 [1] getindex
   @ ./essentials.jl:13 [inlined]
 [2] product(o::ITensor, ψ::MPS, ns::Vector{Int64}; move_sites_back::Bool, apply_dag::Bool, kwargs::@Kwargs{})
   @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:2120
 [3] product (repeats 2 times)
   @ ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:2105 [inlined]
 [4] product(As::Vector{ITensor}, ψ::MPS; move_sites_back_between_gates::Bool, move_sites_back::Bool, kwargs::@Kwargs{})
   @ ITensors.ITensorMPS ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:2247
 [5] product
   @ ~/.julia/packages/ITensors/oOwvi/src/lib/ITensorMPS/src/abstractmps.jl:2238 [inlined]
 [6] measure_pauli(mps::MPS, basis::Vector{Int64})
   @ Main ~/CSViT/classcal_shadow.jl:42
 [7] classical_shadows(mps::MPS, num_samples::Int64)
   @ Main ~/CSViT/classcal_shadow.jl:62
 [8] top-level scope
   @ ~/CSViT/classcal_shadow.jl:73

Hi @zipeilee,
since this is your first post, welcome in the community.

The problem is how you are writing the dagger, the symbol ' has a different meaning when applied to an ITensor, it primes each index of the tensor.

The function op supports an optional boolean argument adjoint that you can set to true, so your code would work by changing how you construct the phase gates to this:

    gates_y1 = [op("Phase",sites[i]; adjoint=true) for i in y_inds]

Mind that the adjoint keyword is not bulletproof, I think it hasn’t been included in the documentation yet cause it is equivalent to swapprime(dag(op),1=>0), so it only works if your operator maps s -> s'(which is the most common case), but if it has different prime levels it would fail. Anyway for your code works fine.

1 Like

very useful, thanks a lot!

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