# Selecting specific rows, columns, i.e. fancy indexing in ITensor

+1 vote

Hi folks,

I think I just missed it in the docs, but I do not see an easy way to do "fancy" indexing in ITensor. Lets us consider the following example

i = Index(10,"i")
j = Index(20,"j")
k = Index(30,"k")
A = randomITensor(i,j,k)
U,Σ,V = svd(A,(i,j));
u = commonind(U,Σ)
# Now I would like to select all rows where Σ is larger than 15
cn = (Σ.store .> 15)
dim_cn = sum(cn)
c = Index(dim_cn,"c")
selector = zeros(dim_cn,dim(u))
selector[:,cn].= I(dim_cn)
selector = ITensor(selector,c,u)
U_new = selector * U


You see what is my goal: I would like to select specific rows of U. With standard Julia syntax I could just use

U_new = U[:,Σ.> 15]


Thanks a lot.

Best,

v.

commented by (70.1k points)
Hi, good question: the answer is that we haven't implemented this for ITensor yet. But we do have plans to do so. I haven't put an official answer below because Matt Fishman, who is the lead developer for the Julia version, may have more details to offer but we are still in the design and planning phase of how this feature will work. (It's not quite as obvious as it may seem but partly the interface will work in a very similar way to how you are envisioning.)

Best,
Miles

The answer Miles gave above in the comments is correct. Unfortunately right now this feature is a work in progress (something we think about periodically but haven't taken the time to implement). To explain some of the complications, the design is fairly straightforward for slicing dense ITensors, but for block sparse ITensors the slicing becomes a bit more subtle (i.e., do you only only allow slicing entire blocks, or also subblocks, of a block sparse ITensor). In addition, the design we were thinking of would involve the indices themselves also getting sliced (i.e. in your example, the U_new tensor would have indices (u, v => 1:dim_cn) where v = commonind(V,Σ), instead of an entirely new index c). Sorry for the inconvenience, the strategy you take above seems fine for now. If you find that it is not fast enough for any reason, or need something more sophisticated that is not available right now, we may be able to help by pointing out certain functions or providing helper functions for certain operations.