# Drop Length 1 Index in ITensors

**URL:** https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305
**Category:** General Usage
**Tags:** question, itensors
**Created:** [November 16, 2023, 10:00am UTC](https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305 "2023-11-16T10:00:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AlexanderNenninger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexandernenninger/32/24602_2.png) [@AlexanderNenninger](https://discourse.julialang.org/u/AlexanderNenninger)
#### Post date: [November 16, 2023, 10:00am UTC](https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305/1 "2023-11-16T10:00:07Z")

</div>

What’s the canonical way to drop length 1 Indices in ITensors? Specifically, I for tensor train algorithms, it is convenient to have order-3 tensors on both ends, but with the first and last index being of length 1. Is there convenient in-place way to drop these dimensions?

## Example

```julia-auto
i1 = Index(1, "i1")
i2 = Index(10, "i1")
A = ITensor(rand(1, 10), (i1, i2))

# how to drop `i1` in-place?

```

---

<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: [November 17, 2023, 12:32am UTC](https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305/2 "2023-11-17T00:32:55Z")

</div>

Good question. Mostly what we are recommending to users is that they just contract with a (dimension 1) vector to remove unwanted indices. Even though this does more work than is necessary, it’s usually not a bottleneck in most applications. So like

```julia
D = A * onehot(i1=>1)

```

Alternatively, you could use the following function `drop_ones` to do it:

```julia
using ITensors

function drop_ones(A::ITensor)
  one_inds = findall(isone,dim.(inds(A))) # dim==1 index locations
  d_inds = setdiff(1:order(A),i1s) # locations of > 1 indices
  a = dropdims(array(A);dims=tuple(one_inds...)) # remove dim==1 axes
  return ITensor(a,inds(A)[d_inds])
end

let
  i1 = Index(1, "i1")
  i2 = Index(10, "i2")
  A = ITensor(rand(1, 10), (i1, i2))

  D = drop_ones(A)
  @show D

  return
end

```

---

<div class="post-metadata">

### Author: ![AlexanderNenninger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexandernenninger/32/24602_2.png) [@AlexanderNenninger](https://discourse.julialang.org/u/AlexanderNenninger)
#### Post date: [November 17, 2023, 8:30am UTC](https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305/3 "2023-11-17T08:30:51Z")

</div>

Thanks! Love ITensors btw. Carrying around named indices really makes working with tensors a breeze. Compared to macro solutions, it’s much easier to build on top of it.

---

<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: [November 18, 2023, 8:07pm UTC](https://discourse.julialang.org/t/drop-length-1-index-in-itensors/106305/4 "2023-11-18T20:07:42Z")

</div>

Glad you’re finding it so useful! We do think the index system really pays off over time.
