# Adding two mps or constructing a superposition state

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:

using ITensors
s = siteinds("S=1/2", 12)
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 upA and DownB. 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?

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:

s = siteinds("S=1/2",N)


Hi Miles,

Now, I tried

using ITensors
N=13
s = siteinds("S=1/2", N)
newB=vecB-inner(vecA,vecB)*vecA
Bvec=normalize!(newB)
Avec=normalize!(vecA)
inner(Bvec,Avec)

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

ψ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.

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

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