Correct tensor to matrix transformation

Hi everyone,
Sorry for the naive question, it is just to be sure of everything I am doing. If I have a 2 site operator, and a local dimension of 2, this operator can be represented by a 4x4 matrix. In the tensor network language, this is instead a 2x2x2x2 tensor. If I have a ITensor object of size 2x2x2x2 called op and I want to obtain its 4x4 matrix representation, is this snippet correct ?

using ITensors

 indx=inds(op)

 A = Array(op,indx[1],indx[3],indx[2],indx[4])

  @show (A)

This should combine the indices on the same side of the tensor and giving the right result. Is this correct?
Thank you for your time and availability !

Hello Francesco,

I think you are looking for the combiner which is used to fuze indices together
Heres a small example

julia> using ITensors
julia> i,j,k,l = Index.((2,2,2,2))
julia> A = randomITensor(i,j,k,l)
ITensor ord=4 (dim=2|id=199) (dim=2|id=19) (dim=2|id=10) (dim=2|id=463)
NDTensors.Dense{Float64, Vector{Float64}}

julia> Amat = combiner(i,j) * A * combiner(k,l)
ITensor ord=2 (dim=4|id=411|"CMB,Link") (dim=4|id=99|"CMB,Link")
NDTensors.Dense{Float64, Vector{Float64}}

Hello Karl,
Thank you for your answer. Yes, this is what I was looking for! I assume the order of the indices passed to the combiner matters. If my A is 2 site gate acting on spinless Fermion sites i and j, where j=i+1 and I want to get its representation in the basis |0\rangle,a_i^{\dag}|0\rangle,a_j^{\dag}|0\rangle,a_i^{\dag}a_j^{\dag}|0\rangle, is the order of indices that I have to pass to combiner the same as in your code?
Thank you

Hi Francesco,

I would in general just suggest verifying the basis is what you expect. Here’s one way to check the representation basis by measuring \hat{O}=\hat{n}_2 + \hat{n}_3:

julia> s = siteinds("Fermion",4)
julia> O = op("N",s[2])*op("I",s[3])+op("I",s[2])*op("N",s[3])
ITensor ord=4 (dim=2|id=684|"Fermion,Site,n=2")' (dim=2|id=684|"Fermion,Site,n=2") (dim=2|id=20|"Fermion,Site,n=3")' (dim=2|id=20|"Fermion,Site,n=3")
NDTensors.Dense{Float64, Vector{Float64}}

julia> Omat = O * combiner(s[2],s[3]) * combiner(s[2]',s[3]')
ITensor ord=2 (dim=4|id=411|"CMB,Link") (dim=4|id=78|"CMB,Link")
NDTensors.Dense{Float64, Vector{Float64}}

julia> @show Omat
Omat = ITensor ord=2
Dim 1: (dim=4|id=411|"CMB,Link")
Dim 2: (dim=4|id=78|"CMB,Link")
NDTensors.Dense{Float64, Vector{Float64}}
 4×4
 0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0
 0.0  0.0  1.0  0.0
 0.0  0.0  0.0  2.0
ITensor ord=2 (dim=4|id=411|"CMB,Link") (dim=4|id=78|"CMB,Link")
NDTensors.Dense{Float64, Vector{Float64}}
2 Likes

Thank you for your advice!