# Problem with contracting a network of tensors.

**URL:** <https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592>\
**Category:** ITensor Julia Questions\
**Created:** [January 12, 2023, 2:30pm UTC](https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592 "2023-01-12T14:30:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Jay](https://itensor.discourse.group/u/Jay)\
**Post date:** [January 12, 2023, 2:30pm UTC](https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592/1 "2023-01-12T14:30:22Z")

</div>

Hi I am trying to contract a network of 4 tensors as shown in the figure attached

 ![Screenshot from 2023-01-12 19-54-51](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/4f4714235beb0312eb90447b733ea6da3d46fb9e.png)

This is the code I am using for this -

```julia
using ITensors
let
  D = Index(9,"D")
  w = Index(2,"w")
  d = Index(2,"d")

  C = randomITensor(D,w,D)
  A = randomITensor(D,d,D)
  X = randomITensor(w,d,w,d)
  B = randomITensor(D,d,D)
  C1 = C * B
  C2 = C1 * X
  C3 = C2 * A
end

```

But it is giving an error saying wrong permutation of indices…what am I doing wrong?  
This is the error code -  
" ArgumentError: Invalid permutation of length 4: (4, 3, 3, 4)"

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [January 12, 2023, 3:13pm UTC](https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592/2 "2023-01-12T15:13:45Z")

</div>

Thanks for the question; it’s a good question. The main issue with your code, and something which is not too obvious from the ITensor documentation unfortunately, is that in ITensor you aren’t allowed to use the exact same Index twice on a single ITensor. (E.g. your `C` ITensor has the Index D twice.) This behavior is intentional, and is necessary for our automatic contraction and addition operations to work properly.

The recommended practice in a case like yours is to use **prime levels** to distinguish indices.

So you could define `C` as

```auto
C = randomITensor(D,w,D')

```

and correspondingly prime the Index of `A` that you want to contract with the primed index of `C`, etc.

(When you think about the code you wrote above, notice that the computer has no way of knowing which of the two “D” indices of C should be contracted with the “D” indices of A. So the prime levels disambiguate that.)

---

<div class="post-metadata">

**Author:** ![Jay](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@Jay](https://itensor.discourse.group/u/Jay)\
**Post date:** [January 12, 2023, 4:51pm UTC](https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592/3 "2023-01-12T16:51:33Z")

</div>

Thanks for the swift reply…by the way is there any place where I can find the documentation of all the built in functions of ITensor say for creating operators etc. all of it in one place?

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [January 12, 2023, 5:14pm UTC](https://itensor.discourse.group/t/problem-with-contracting-a-network-of-tensors/592/4 "2023-01-12T17:14:31Z")

</div>

Great. Not sure what you’re asking, though, about “creating operators, etc.” because that could cover many things, and there are many objects that act like operators (certain ITensors, MPO tensor networks, operators created from names like “Sz” or “Sy”).

Overall our documentation is organized into categories like operations on individual ITensors, versus systems for making single-site operators out of local indices, to working with MPS and MPO tensor networks.

Here is the link to the documentation: [https://itensor.github.io/ITensors.jl/stable/](https://itensor.github.io/ITensors.jl/stable/)

Did you have a specific question about creating operators (and giving some more context about what you mean by “operator”)?

Thanks
