# Embedding a tensor into a higher order tensor

**URL:** <https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210>\
**Category:** ITensor Julia Questions\
**Created:** [January 22, 2025, 4:55pm UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210 "2025-01-22T16:55:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leobb](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@leobb](https://itensor.discourse.group/u/leobb)\
**Post date:** [January 22, 2025, 4:55pm UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/1 "2025-01-22T16:55:07Z")

</div>

Hello all! I’m new to ITensor and Tensor Networks so I’d greatly appreciate any help.

My problem is this: I currently have a predefined ITensor, A (2x2x2) which has been extracted from an MPS (psi) at site 3:

```julia
A = psi[3]

```

I would like to embed this tensor into a 4th order (2x2x2x2) zero tensor, T:

```julia
i = Index(2, "i")
j = Index(2, "j")
k = Index(2, "k")
l = Index(2, "l")
T = ITensor(i,j,k,l)

```

Once I embed A into T, the resulting tensor should be a 4th order tensor of dimension (2x2x2x2) (call it G). G should have the structure G[:,:,:,1] = A, and G[:,:,:,2] = a 3rd order (2x2x2) zero tensor. This means that a slice of G contains A, and the other slice contains the 3rd order zero tensor.

I’ve tried:

```julia
T[l => 1] = A

```

where I imagine that A is being embedded in the 1st slice of the last dimension of T (Where A is actually embedded is arbitrary). However, I get the following error:

```julia
MethodError: no method matching setindex!(::ITensor, ::ITensor, ::Pair{Index{Int64}, Int64})

```

My question is: How can I embed a 3rd order tensor A (2x2x2) into a 4th order zero tensor T (2x2x2x2) to achieve an output of a 4th order tensor (2x2x2x2) with the structure specified by G.

I hope this is clearly explained. Please let me know if you require more context (or a better explanation).

Thank you all!

---

<div class="post-metadata">

**Author:** ![ryanlevy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/ryanlevy/32/150_2.png) [@ryanlevy](https://itensor.discourse.group/u/ryanlevy)\
**Post date:** [January 22, 2025, 6:58pm UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/2 "2025-01-22T18:58:51Z")

</div>

I would suggest instead of slicing, try multiplying by [`onehot`](https://itensor.github.io/ITensors.jl/stable/ITensorType.html#ITensors.onehot)

```julia
using ITensors
i, j, k, l = Index.((2, 2, 2, 2))

A = random_itensor(i, j, k)
G = A * onehot(l => 1)

# double check:
Aarray, Garray = array(A, i, j, k), array(G, i, j, k, l)
@show Aarray ≈ Garray[:, :, :, 1]
@show Aarray * 0 ≈ Garray[:, :, :, 2]

```

output:

```julia
Aarray ≈ Garray[:, :, :, 1] = true
Aarray * 0 ≈ Garray[:, :, :, 2] = true

```

---

<div class="post-metadata">

**Author:** ![leobb](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@leobb](https://itensor.discourse.group/u/leobb)\
**Post date:** [January 22, 2025, 8:41pm UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/3 "2025-01-22T20:41:38Z")

</div>

Thanks ryan, this solves my issue

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [January 23, 2025, 5:18pm UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/4 "2025-01-23T17:18:26Z")

</div>

An alternative is the [`ITensors.directsum`](https://itensor.github.io/ITensors.jl/dev/ITensorType.html#ITensors.directsum-Tuple%7BPair%7BITensor%7D,%20Pair%7BITensor%7D,%20Pair%7BITensor%7D,%20Vararg%7BAny%7D%7D) function.

---

<div class="post-metadata">

**Author:** ![leobb](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@leobb](https://itensor.discourse.group/u/leobb)\
**Post date:** [January 29, 2025, 10:15am UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/5 "2025-01-29T10:15:48Z")

</div>

Thanks Matt, just tried this and works perfectly fine too!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [February 8, 2025, 10:16am UTC](https://itensor.discourse.group/t/embedding-a-tensor-into-a-higher-order-tensor/2210/6 "2025-02-08T10:16:26Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
