# Accessing elements of tensor with variable rank

+1 vote

Is it possible to access the elements of tensor whose rank is not known at compile time (but given via the input system):

If the rank of Itensor T is known (and in this case 3), I would use:

auto rt = T.real(i(1),j(1),k(1));


with i, j, k indices of T.

However, if T is dynamically built during the execution of the program and is a rank N tensor (with N an input parameter), how can I access its elements (without a lot if else clauses and hard coding the above expression for every possible value of N) ?

Is there something like

auto rt = T.real( list_of_index_values )


with listofindex_values a dynamically built (unpacked) list or tuple of Index values?

Best,
Andreas

Hey Andreas,

That functionality had not been available, but that is a good suggestion (.set() can currently accept vector<IndexVal>, so it is inconsistent that .real() and .cplx() can't).

I made a pull request for adding .real(vector<IndexVal>) and .cplx(vector<IndexVal>), is that sufficient for your use case?

Cheers,
Matt

commented by (8.9k points)
The methods .real(vector<IndexVal>) and .cplx(vector<IndexVal>) have been included in ITensor master, by the way.

-Matt
commented by (190 points)
Thank you very much!! This is very helpful!
commented by (240 points)
It would be very helpful if we could do all these operations on tensor slices, by passing partial vectors of IndexVal. This is a common functionality elsewhere that I feel it's missing now.
commented by (8.9k points)
You can currently  take slices of an ITensor using a special single element tensor: http://www.itensor.org/docs.cgi?vers=cppv2&page=classes/single_itensor

Unfortunately, this makes a copy of the ITensor data, so it doesn't help with the problem of setting a block of elements of an ITensor. We plan to add more functionality for taking partial slices of ITensors as "views" into the data. However, it takes some thought to come up with a nice interface for doing that when the ITensors have QN conservation (block sparse storage).