# ITensor type definition for function input argument

**URL:** <https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340>\
**Category:** Numerics\
**Tags:** itensors\
**Created:** [June 14, 2023, 3:29pm UTC](https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340 "2023-06-14T15:29:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![8me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/8me/32/18724_2.png) [@8me](https://discourse.julialang.org/u/8me)\
**Post date:** [June 14, 2023, 3:29pm UTC](https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340/1 "2023-06-14T15:29:20Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![miles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles/32/29112_2.png) [@miles](https://discourse.julialang.org/u/miles)\
**Post date:** [June 14, 2023, 6:27pm UTC](https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340/2 "2023-06-14T18:27:45Z")

</div>

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:

```julia
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](https://itensor.discourse.group)

---

<div class="post-metadata">

**Author:** ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)\
**Post date:** [June 14, 2023, 10:14pm UTC](https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340/3 "2023-06-14T22:14:35Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![8me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/8me/32/18724_2.png) [@8me](https://discourse.julialang.org/u/8me)\
**Post date:** [June 15, 2023, 10:18am UTC](https://discourse.julialang.org/t/itensor-type-definition-for-function-input-argument/100340/4 "2023-06-15T10:18:20Z")

</div>

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 😉

@mtfishman I think this is the workaround I will go with 👍
