# Adding two mps or constructing a superposition state

**URL:** <https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673>\
**Category:** ITensor Julia Questions\
**Tags:** julia, dmrg, mps\
**Created:** [April 30, 2024, 12:54am UTC](https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673 "2024-04-30T00:54:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pk123](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@pk123](https://itensor.discourse.group/u/pk123)\
**Post date:** [April 30, 2024, 12:54am UTC](https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673/1 "2024-04-30T00:54:17Z")

</div>

I want to construct a state \ket{\up}\otimes A + \ket{\down}\otimes B where A and B are random and orthogonal.

I constructed A and B and added Up and Down to the initial sites of these A and B but then I couldn’t add them because they don’t have same site index. How do I achieve this?  
Here is my code:

```julia
using ITensors
s = siteinds("S=1/2", 12)
vecA = randomMPS(s; linkdims=50)
vecB = randomMPS(s; linkdims=50)
newB=vecB-inner(vecA,vecB)*vecA
Bvec=normalize!(newB)
Avec=normalize!(vecA)
inner(Bvec,Avec)

sp = siteinds("S=1/2", 13)
ψA = randomMPS(sp)
ψB = randomMPS(sp)

CC=MPS(sp,"Up")

ψA[1] = CC[1]
for j in 2:13
    ψA[j] = Avec[j-1]
end
expect(ψA,"Sz")

DD=MPS(sp,"Dn")

ψB[1] = DD[1]
for j in 2:13
    ψB[j] = Bvec[j-1]
end
expect(ψB,"Sz")

```

This is how I constructed A and B and up_A and Down_B. I can perfect add A+B but I can’t add ψA and ψB and get error “the input `MPS` or `MPO` do not have the same site indices”.

How do I go about constructing such a state?

---

<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:** [April 30, 2024, 3:21am UTC](https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673/2 "2024-04-30T03:21:17Z")

</div>

In your code, you create a second set of site indices `sp` by calling `siteinds` a second time. Instead, I would suggest you just make a single set of site indices by calling `siteinds` for all 13 of your sites (also I would definitely suggest using a variable `N=13` rather than hard coding numbers throughout). Then when you make the shorter states such as `vecA` at the beginning, you can pass just the first `N-1` indices like this:

```julia
s = siteinds("S=1/2",N)
vecA = randomMPS(s[1:N-1]; linkdims=50)

```

---

<div class="post-metadata">

**Author:** ![pk123](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@pk123](https://itensor.discourse.group/u/pk123)\
**Post date:** [April 30, 2024, 10:13am UTC](https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673/3 "2024-04-30T10:13:17Z")

</div>

Hi Miles,

Thank you for your prompt reply.

Now, I tried

```auto
using ITensors
N=13
s = siteinds("S=1/2", N)
vecA = randomMPS(s[1:N-1]; linkdims=50)
vecB = randomMPS(s[1:N-1]; linkdims=50)
newB=vecB-inner(vecA,vecB)*vecA
Bvec=normalize!(newB)
Avec=normalize!(vecA)
inner(Bvec,Avec)

ψA = randomMPS(s; linkdims=50)
ψB = randomMPS(s; linkdims=50)

CC=MPS(s,"Up")

ψA[1] = CC[1]
for j in 2:13
    ψA[j] = Avec[j-1]
end

DD=MPS(s,"Dn")

ψB[1] = DD[1]
for j in 2:13
    ψB[j] = Bvec[j-1]
end

```

But I still can not construct

```auto
ψA+ψB

```

I get the same error that these do not have same site indices.  
Probably it is becase of the use of CC and DD to pass the first index but I am not sure how to achieve this state.

---

<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:** [April 30, 2024, 11:54am UTC](https://itensor.discourse.group/t/adding-two-mps-or-constructing-a-superposition-state/1673/4 "2024-04-30T11:54:50Z")

</div>

Please print out each state ψA and ψB and draw its index structure and think about whether it is a valid MPS. When I did this, I spotted two issues:

(1) in your code above, the first site index gets put twice onto ψA and ψB, meaning the first tensor has site index 1 but then the second tensor also has site index 1. You can fix this by using s[2:N] at the beginning instead of s[1:N-1]

(2) then there is an issue where the first tensor has an extra, dangling link index. This is because if you call `MPS(s,"Up")` it puts dimension 1 link indices throughout the MPS that it makes. Instead, you can just make a single ITensor which is in the state “Up” and set ψA[1] to be that ITensor.

A good way to make such an ITensor is by doing

```julia
ψA[1] = state(s[1],"Up")

```
