Create combined MPS defined on some indicies

Hi, I want to create a state like |00\rangle |W_{n}\rangle |00\rangle where the W-state is defined on a subset of qubits in the middle of the chain, i.e. |W \rangle = (|100\cdots 0\rangle + |010\cdots0\rangle + \cdots |00 \cdots 1 \rangle )/\sqrt{\mathcal{N}}. When I do this, using the code below, I get an error about having unmatched site indices (unmatched on the links). I’m sure there is some easy fix, but I can’t seem to figure it out and would appreciate any help I can get!

using ITensors, ITensorMPS
using LinearAlgebra

let

# Want to create a state |00> |W_state> |00>
Ntot = 6
Nleft = 1:2
Nright = Ntot-1:Ntot
Nmid = 3:Ntot-2


function state_vec(n)
    twos_vec = [1 << nn for nn = collect(0:n-1)]
    cb_idx(bs) = dot(twos_vec, bs) + 1

    bitstrings = Diagonal(ones(Bool,n))
    @show bitstrings[1,:]
    @show bvals = [cb_idx(bitstrings[i,:]) for i in 1:n] 
    
    state = zeros(2^n)
    coef = ones(n)
    state[bvals] .= coef
    return state
end

maxdim = 10; cutoff = 1e-8

sites = siteinds("Qubit", Ntot)


psi_temp = MPS(sites,"0")
# Initialize Wstate from the state vector corresponding to a sum of each state
Wstate = MPS(state_vec(length(Nmid)),sites[Nmid];maxdim=maxdim, cutoff=cutoff)
normalize!(Wstate)

println("Psi temporary")
display(psi_temp)
println("W state")
display(Wstate)

for (i,j) in enumerate(Nmid)
    psi_temp[j] = Wstate[i]
end
orthogonalize!(psi_temp,1)
psi = psi_temp
println("Final State")
display(psi)

@show inner(psi',psi)

end

which gives the error indicating the mis-matched link indices:

ERROR: LoadError: Calling `dot(ϕ::MPS/MPO, ψ::MPS/MPO)` with multiple site indices per
MPS/MPO tensor but the site indices don't match. Even with `make_inds_match = true`,
the case of multiple site indices per MPS/MPO is not handled automatically.
The sites with unmatched site indices are:

    inds(ϕ[1]) = ((dim=2|id=855|"Qubit,Site,n=1")', (dim=2|id=382|"Link,l=1")')

    inds(ψ[1]) = ((dim=2|id=855|"Qubit,Site,n=1"), (dim=2|id=938|"Link,l=1"))

Make sure the site indices of your MPO/MPS match. You may need to prime
one of the MPS, such as `dot(ϕ', ψ)`.

Nice code – I think you mistake is just at the end: if you do inner(psi',psi) that is asking the inner function to contract two MPS where one of the MPS has all of its indices “primed” which will also prime the site indices and then they won’t match.

Instead all you need to do is:

@show inner(psi,psi)

Oh haha thank you! I also encountered this error when I try to take the expectation value expect(psi,"Z") of the above state:

ERROR: LoadError: ArgumentError: Overload of "op" or "op!" functions not found for operator name "Z" and Index tags: ("Link,l=4",).

In fact, it actually works for expect(psi,"Z";sites=1:4) but once I go to expect(psi,"Z";sites=1:5) (or any indices after 5) it gives the above error.

Do you know why it doesn’t register the operator name “Z” for this link?

Yes, that’s a different error and is happening because your sites do not have an appropriate “tag” in them that would define the “Z” operator for those sites. Our system looks at a site index, checks its tags (in your case the tags are apparently “Link” and “l=4”) and then sees if one of those tags has a “Z” operator. I’m not sure why your sites don’t have the “Qubit” tag because it looks like you did make "Qubit sites originally. If you print out your MPS at each step maybe it could become clear where the tags changed or went away. Also please do make sure that your MPS has the correct structure where all of the internal or link indices match up with each other and there are no extra “dangling” ones which could also be a possible issue here.

Finally, both “Qubit” and “S=1/2” define the “Z” operator so as long as your sites have one of those tags (and as long as your MPS is not ill-defined in some way) then you should be good to go.

Also, for a complete list of operators defined on various site types (tags), you can check here:

https://itensor.github.io/ITensors.jl/dev/IncludedSiteTypes.html

Yea I think extra links is my problem. How do I resolve this? For example, this is the MPS of the final state with | 0_1 0_2 \rangle | W_{3:4} \rangle |0_5 0_6 \rangle:

Final State
MPS
[1] ((dim=2|id=20|"Qubit,Site,n=1"), (dim=1|id=676|"Link,l=1"))
[2] ((dim=2|id=54|"Qubit,Site,n=2"), (dim=1|id=873|"Link,l=2"), (dim=1|id=975|"Link,l=2"), (dim=1|id=676|"Link,l=1"))
[3] ((dim=2|id=676|"Qubit,Site,n=3"), (dim=2|id=986|"Link,n=1"), (dim=1|id=975|"Link,l=2"))
[4] ((dim=2|id=650|"Qubit,Site,n=4"), (dim=1|id=524|"Link,l=4"), (dim=2|id=986|"Link,n=1"))
[5] ((dim=1|id=404|"Link,l=4"), (dim=2|id=173|"Qubit,Site,n=5"), (dim=1|id=13|"Link,l=5"), (dim=1|id=524|"Link,l=4"))
[6] ((dim=2|id=251|"Qubit,Site,n=6"), (dim=1|id=13|"Link,l=5"))

In particular, it looks like I have incorrect links where I replaced the state with the W-state. Ideally, when I replace the MPS one sites 3:4, I want it to replace the links there too?
What do I need to do to fix this? Combine the indicies using combiner?