# Why cannot I convert this ITensor to an Array{Float64}

**URL:** <https://itensor.discourse.group/t/why-cannot-i-convert-this-itensor-to-an-array-float64/1835>\
**Category:** ITensor Julia Questions\
**Tags:** julia\
**Created:** [June 27, 2024, 1:54pm UTC](https://itensor.discourse.group/t/why-cannot-i-convert-this-itensor-to-an-array-float64/1835 "2024-06-27T13:54:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uranium92238](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/uranium92238/32/577_2.png) [@Uranium92238](https://itensor.discourse.group/u/Uranium92238)\
**Post date:** [June 27, 2024, 1:54pm UTC](https://itensor.discourse.group/t/why-cannot-i-convert-this-itensor-to-an-array-float64/1835/1 "2024-06-27T13:54:17Z")

</div>

I am trying to convert a specific ITensor to an `Array{Float64,4}` as part of the following code

```auto
using Serialization, ITensors
using TensorOperations
include("ccd-helper.jl")
initialize_cc_variables()
begin
a_1 = Index(2,"a_1")
a_2 = Index(2,"a_2")
i_1 = Index(4,"i_1")
i_2 = Index(4,"i_2")
end
begin
iT2 = ITensor(T2,a_1,a_2,i_1,i_2)
iR2 = ITensor(R2,a_1,a_2,i_1,i_2)
iScaled_R2 = ITensor(Scaled_R2,a_1,a_2,i_1,i_2)
end

for i in 1:25
    aT2 = Array(iT2,a_1,a_2,i_1,i_2)
    serialize("T2_vvoo.jlbin",aT2)
    iR2 = calculate_residual_memeff()
    iT2 = update_T2(iT2,iR2,iScaled_R2)
    # @show iR2
end
Array(iR2,a_1,a_2,i_1,i_2)

```

where `T2` `R2` and `Scaled_R2` are all of type `Array{Float64,4}`. Running the above code results in the error

```auto
ERROR: BoundsError: attempt to access NTuple{4, Index{Int64}} at index [0]
Stacktrace:
  [1] getindex
    @ ./tuple.jl:31 [inlined]
  [2] #7
    @ ~/.julia/packages/ITensors/2Xz5q/src/NDTensors/tupletools.jl:43 [inlined]
  [3] macro expansion
    @ ./ntuple.jl:72 [inlined]
  [4] ntuple
    @ ./ntuple.jl:69 [inlined]
  [5] _permute
    @ ~/.julia/packages/ITensors/2Xz5q/src/NDTensors/tupletools.jl:43 [inlined]
  [6] permute
    @ ~/.julia/packages/ITensors/2Xz5q/src/NDTensors/tupletools.jl:50 [inlined]
  [7] permutedims
    @ ~/.julia/packages/ITensors/2Xz5q/src/NDTensors/dense.jl:373 [inlined]
  [8] permutedims
    @ ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1613 [inlined]
  [9] _permute(as::ITensors.NDTensors.NeverAlias, T::ITensors.NDTensors.DenseTensor{…}, new_inds::NTuple{…})
    @ ITensors ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1618
 [10] permute(as::ITensors.NDTensors.NeverAlias, T::ITensor, new_inds::NTuple{4, Index{Int64}})
    @ ITensors ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1622
 [11] permute
    @ ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1627 [inlined]
 [12] #permute#214
    @ ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1603 [inlined]
 [13] permute
    @ ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:1592 [inlined]
 [14] Array{Float64, 4}(::ITensor, ::Index{Int64}, ::Index{Int64}, ::Index{Int64}, ::Index{Int64})
    @ ITensors ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:771
 [15] Array(::ITensor, ::Index{Int64}, ::Index{Int64}, ::Index{Int64}, ::Index{Int64})
    @ ITensors ~/.julia/packages/ITensors/2Xz5q/src/itensor.jl:780
 [16] top-level scope
    @ ~/Desktop/Andreas/phase_2/julia-code/ccd/itensors/t5.jl:52
Some type information was truncated. Use `show(err)` to see complete types.

```

Whereas doing `Array(iR2,a_1,a_2,i_1,i_2)` gives the expected correct result. I understand that this might be due to what is happening inside the function `calculate_residual_memeff()`. The function definition is given below

> **calculate\_residual\_memeff()**
>
> ```auto
> function calculate_residual_memeff()
> nocc = deserialize("nocc.jlbin")
> nv = deserialize("nv.jlbin")
> i_1= Index(nocc, "i_1")
> i_2= Index(nocc, "i_2")
> i_3= Index(nocc, "i_3")
> i_4= Index(nocc, "i_4")
> a_1= Index(nv, "a_1")
> a_2= Index(nv, "a_2")
> a_3= Index(nv, "a_3")
> a_4= Index(nv, "a_4")
> I_vvoo = ITensor(zeros(Float64, nv, nv, nocc, nocc), a_1, a_2, i_1, i_2) #create function generated this line
> g_voov = ITensor(deserialize("g_voov.jlbin"),a_1, i_3, i_1, a_3)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_3, i_2)
>     
> I_vvoo += -1*g_voov*T2_vvoo
> #Unload T2_vvoo
> #Unload g_voov
> g_vvoo = ITensor(deserialize("g_vvoo.jlbin"),a_1, a_2, i_1, i_2)
>     
> I_vvoo += 1/2*g_vvoo
> #Unload g_vvoo
> g_vovo = ITensor(deserialize("g_vovo.jlbin"),a_2, i_3, a_3, i_2)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_1, i_3)
>     
> I_vvoo += -1*g_vovo*T2_vvoo
> #Unload T2_vvoo
> #Unload g_vovo
> g_vovo = ITensor(deserialize("g_vovo.jlbin"),a_2, i_3, a_3, i_1)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_3, i_2)
>     
> I_vvoo += -1*g_vovo*T2_vvoo #fourth update
> # @show I_vvoo
> #Unload T2_vvoo
> #Unload g_vovo
> g_voov = ITensor(deserialize("g_voov.jlbin"),a_1, i_3, i_1, a_3)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_2, i_3)
>     
> I_vvoo += 2*g_voov*T2_vvoo
> #Unload T2_vvoo
> #Unload g_voov
> g_oooo = ITensor(deserialize("g_oooo.jlbin"),i_3, i_4, i_1, i_2)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_2, i_3, i_4)
>     
> I_vvoo += 1/2*g_oooo*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oooo
> f_vv = ITensor(deserialize("f_vv.jlbin"),a_2, a_3)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_1, i_2)
>     
> I_vvoo += f_vv*T2_vvoo
> #Unload T2_vvoo
> #Unload f_vv
> g_vvvv = ITensor(deserialize("g_vvvv.jlbin"),a_1, a_2, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_3, a_4, i_1, i_2)
>     
> I_vvoo += 1/2*g_vvvv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_vvvv
> f_oo = ITensor(deserialize("f_oo.jlbin"),i_3, i_2)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_2, i_1, i_3)
>     
> I_vvoo += -1*f_oo*T2_vvoo
> #Unload T2_vvoo
> #Unload f_oo
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_1, a_4) #create function generated this line
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_1, i_3)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_4, i_2, i_4)
>     
> I_vvoo += 2*I_ovov*T2_vvoo #10th update
> # @show I_vvoo
> #Unload T2_vvoo
> #Unload I_ovov
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_1, a_4)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_1, i_3)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_4, i_4, i_2)
>     
> I_vvoo += -2*I_ovov*T2_vvoo
> #Unload T2_vvoo
> #Unload I_ovov
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_1, a_4)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_3, i_1)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_4, i_4, i_2)
>     
> I_vvoo += 1/2*I_ovov*T2_vvoo
> #Unload T2_vvoo
> #Unload I_ovov
> I_oo = ITensor(zeros(Float64, nocc, nocc), i_4, i_2) #create function generated this line
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_3, a_4, i_3, i_2)
>     
> I_oo += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_2, i_1, i_4)
>     
> I_vvoo += -2*I_oo*T2_vvoo
> #Unload T2_vvoo
> #Unload I_oo
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_1, a_3)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_4, i_1, i_3)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_2, i_4)
>     
> I_vvoo += -1*I_ovov*T2_vvoo
> #Unload T2_vvoo
> #Unload I_ovov
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_1, a_3)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_4, i_1, i_3)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_4, i_2)
>     
> I_vvoo += I_ovov*T2_vvoo #15th update
>     
>     
>     
>     
>     
>     
>     
>     
>     
>     
> # @show I_vvoo #15th update obdi shob same asche
> #Unload T2_vvoo  
> #Unload I_ovov
> I_vv = ITensor(zeros(Float64, nv, nv), a_2, a_3) #create function generated this line
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_4, i_4, i_3)
>     
> I_vv += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_3, i_1, i_2)
>     
> I_vvoo += I_vv*T2_vvoo
> #Unload T2_vvoo
> #Unload I_vv
> I_oo = ITensor(zeros(Float64, nocc, nocc), i_4, i_2)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_3, a_4, i_2, i_3)
>     
> I_oo += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_2, i_1, i_4)
>     
> I_vvoo += I_oo*T2_vvoo
> #Unload T2_vvoo
> #Unload I_oo
> I_vv = ITensor(zeros(Float64, nv, nv), a_2, a_4)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_4, i_3)
>     
> I_vv += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_4, i_1, i_2)
>     
> I_vvoo += -2*I_vv*T2_vvoo #18th update
> # @show I_vvoo #Till here everything matches
>     
>     
>     
>     
>     
>     
>     
>     
>     
>     
>     
> #Unload T2_vvoo
> #Unload I_vv
> I_ovov = ITensor(zeros(Float64, nocc, nv, nocc, nv), i_4, a_1, i_2, a_3)#Load and set to zero julia equivalent
>     
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4)
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_4, i_3, i_2)
>     
> I_ovov += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_2, a_3, i_4, i_1)    
> I_vvoo += 1/2*I_ovov*T2_vvoo
> #Unload T2_vvoo
> #Unload I_ovov
> I_oooo = ITensor(zeros(Float64, nocc, nocc, nocc, nocc), i_3, i_4, i_1, i_2) #create function generated this line
> g_oovv = ITensor(deserialize("g_oovv.jlbin"),i_3, i_4, a_3, a_4
>     
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_3, a_4, i_1, i_2)
> I_oooo += g_oovv*T2_vvoo
> #Unload T2_vvoo
> #Unload g_oovv
> T2_vvoo = ITensor(deserialize("T2_vvoo.jlbin"),a_1, a_2, i_3, i_4)
> I_vvoo += 1/2*I_oooo*T2_vvoo #20th update
> R2 = ITensor(zeros(Float64,nv,nv,nocc,nocc),a_1,a_2,i_1,i_2)
> R2 += I_vvoo + swapinds(I_vvoo,(a_1,i_1),(a_2,i_2)) #correct symmetrization
> return R2
> end 
> 
> ```

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 28, 2024, 12:55pm UTC](https://itensor.discourse.group/t/why-cannot-i-convert-this-itensor-to-an-array-float64/1835/2 "2024-06-28T12:55:53Z")

</div>

I don’t know what is going on in your code, but this minimal example works for me:

```julia
julia> using ITensors

julia> using ITensors: Index, random_itensor

julia> i, j = Index.((2, 2))
((dim=2|id=781), (dim=2|id=473))

julia> a = random_itensor(i, j)
ITensor ord=2 (dim=2|id=781) (dim=2|id=473)
NDTensors.Dense{Float64, Vector{Float64}}

julia> Array(a, i, j)
2×2 Matrix{Float64}:
 0.744929 0.220073
 1.5734 0.112152

```

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 28, 2024, 1:06pm UTC](https://itensor.discourse.group/t/why-cannot-i-convert-this-itensor-to-an-array-float64/1835/3 "2024-06-28T13:06:07Z")

</div>

Trying to construct your own minimal examples like that may be instructive for you, you have been raising a lot of issues that appear to be code snippets from a more complicated code. Breaking up the problem into smaller parts and writing simple example codes on the side that are independent from the rest of your code that try to reproduce issues you think you are seeing may help you debug more yourself, and also if you find what you think is an issue those simpler codes would be more helpful to share with us rather than parts of a more complicated code.
