ITensor type definition for function input argument

I want to define a function which takes two ITensor of order 1 and dim 4 as input arguments. So my question would be, what is the suggested way of doing this …

I can understand why you’d like to do this, but ITensor types are not parameterized by their order or index dimensions. So you cannot constrain the inputs at the function definition level this way. The most you can do is define a function like:

function f(T1::ITensor, T2::ITensor, ...)
...
end

Then inside the function you can dynamically check whether e.g. the order of each ITensor is 1 or not if you’d like.

By the way, we have a separate Discourse forum specifically for ITensor if you’re interested to ask questions there in the future. It will be seen by more experts and you might get a more prompt response. Here’s the link:
https://itensor.discourse.group

Alternatively, you can convert to a Tensor object from NDTensors, which is parametrized by the tensor order, for example:

using ITensors
using NDTensors

function f(t::ITensor)
  return itensor(f(tensor(t)))
end

function f(t::Tensor{<:Any,1})
  # Implementation for order-1 Tensor
end

Currently you can’t dispatch on the Tensor dimensions since we don’t support tensors with static dimensions, at least not in a simple way. Given new changes being done to NDTensors it should be possible in the near future to wrap a Tensor around a StaticArray.

I have rarely found it is necessary to dispatch on the order of an ITensor. In general, I’ve found it is best to make code independent of the tensor order, which makes it more generic, flexible, and can even lead to simpler code when done in a thoughtful way. I think that the ability to easily write code that is independent of the tensor order is a big advantage of the ITensor library.

1 Like

Thanks for the fast replies to my question!!!

@miles with checking this inside the function I feel somehow uncomfortable as I lose all the advantages of julia doing the type safety book-keeping for me. Thanks for the link to you rdiscourse site, but I posted it here on purpose, because I thought this is a very general question, so it might be nice to have it on the central julia discourse :wink:

@mtfishman I think this is the workaround I will go with :+1: