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

**URL:** https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299
**Category:** ITensor Julia Questions
**Tags:** julia, mps, mpo
**Created:** [March 12, 2025, 7:05am UTC](https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299 "2025-03-12T07:05:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zipeilee](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/zipeilee/32/435_2.png) [@zipeilee](https://itensor.discourse.group/u/zipeilee)
#### Post date: [March 12, 2025, 7:05am UTC](https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299/1 "2025-03-12T07:05:06Z")

</div>

I encountered this error while using `apply` for TEBD, but I have already “grad” the sites of the MPS as described in [this link](https://itensor.org/support/3645/the-time-evolution-for-a-h5-file).

```Julia
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

```auto
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

```

---

<div class="post-metadata">

### Author: ![VinceNeede](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/vinceneede/32/731_2.png) [@VinceNeede](https://itensor.discourse.group/u/VinceNeede)
#### Post date: [March 12, 2025, 9:37am UTC](https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299/2 "2025-03-12T09:37:38Z")

</div>

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:

```julia
    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.

---

<div class="post-metadata">

### Author: ![zipeilee](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/zipeilee/32/435_2.png) [@zipeilee](https://itensor.discourse.group/u/zipeilee)
#### Post date: [March 15, 2025, 12:16pm UTC](https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299/3 "2025-03-15T12:16:20Z")

</div>

very useful， thanks a lot！

---

<div class="post-metadata">

### Author: ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)
#### Post date: [March 25, 2025, 12:16pm UTC](https://itensor.discourse.group/t/boundserror-attempt-to-access-0-element-vector-int64-at-index-1/2299/4 "2025-03-25T12:16:54Z")

</div>

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