# How to contract 2 MPS objects

**URL:** <https://itensor.discourse.group/t/how-to-contract-2-mps-objects/329>\
**Category:** ITensor Julia Questions\
**Created:** [August 9, 2022, 6:55am UTC](https://itensor.discourse.group/t/how-to-contract-2-mps-objects/329 "2022-08-09T06:55:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsegev](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@tsegev](https://itensor.discourse.group/u/tsegev)\
**Post date:** [August 9, 2022, 6:55am UTC](https://itensor.discourse.group/t/how-to-contract-2-mps-objects/329/1 "2022-08-09T06:55:20Z")

</div>

Hey all!  
If I want to couple an outer spin to a spin chain.  
my original thought was to try and build to MPS one of the dimensions of the chain, and the other with dim of 1 (for the spin), but I can’t contract the MPS’s.  
Does someone knows what is the best way to do it ?  
 ![Screen Shot 2022-08-09 at 9.53.03](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/94f122870dc0983c4d21b05290286682a78ec55b.png)

---

<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:** [August 9, 2022, 1:27pm UTC](https://itensor.discourse.group/t/how-to-contract-2-mps-objects/329/2 "2022-08-09T13:27:30Z")

</div>

I’d like to help you but there are some issues with the code you posted that I am not sure about:

- The image is cut off, so it doesn’t show the whole code or error messages. Can you please post the code as text, using 3 backticks (''' … code … ''') to surround the block of code?
- What is `ITensor(0,p)` supposed to be doing?
- What is the intent of the rest of the code (i.e. what is the meaning of P, Q, QU, etc.)?

Based on the first part of your question, when you say couple a spin to a spin chain, by “a spin chain” do you mean you already have the ground state of that spin chain? Or are you trying to make a specific wavefunction? Or are you just trying to build a model or Hilbert space you can use to initialize e.g. a DMRG calculation? Thanks –

---

<div class="post-metadata">

**Author:** ![Johannes](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/johannes/32/206_2.png) [@Johannes](https://itensor.discourse.group/u/Johannes)\
**Post date:** [January 30, 2023, 2:31pm UTC](https://itensor.discourse.group/t/how-to-contract-2-mps-objects/329/3 "2023-01-30T14:31:28Z")

</div>

@miles This is marked as solved, but I’m not able to find the solution.  
I do have the same problem. I have generated an MPS of length N-1:

> sites = siteinds(“Fermion”, 3)  
> states\_psi = [“0” for i in 1:2]  
> psi = MPS(sites[1:2], states)

Now I want to append another Fermion, means I want to create a state on \mathcal{H}^N. In physics language I would like to take the Kronecker product of psi with another state phi.

> states\_phi = [“1”]  
> phi = MPS([sites[end]], states\_phi)
> 
> # Kronecker product  
> chi = psi \* phi

For MPOs there is the possibillity to construct and MPO on a larger Hilbert space via contract(), where the resulting MPO lives on all indices which are not shared between the former MPOs.  
For MPS this contraction yields the following error.

> ERROR: MethodError: no method matching contract(::MPS, ::MPS)  
> Closest candidates are:  
> contract(::NDTensors.DiagTensor, ::Any, ::NDTensors.Tensor{ElT, N, StoreT, IndsT} where {ElT, N, IndsT, StoreT\<:NDTensors.Combiner}, ::Any) at .julia/packages/NDTensors/FHyfI/src/combiner/combiner.jl:125  
> contract(::NDTensors.DiagBlockSparseTensor, ::Any, ::NDTensors.BlockSparseTensor, ::Any) at .julia/packages/NDTensors/FHyfI/src/blocksparse/diagblocksparse.jl:565  
> contract(::NDTensors.DiagBlockSparseTensor, ::Any, ::NDTensors.BlockSparseTensor, ::Any, ::Any) at .julia/packages/NDTensors/FHyfI/src/blocksparse/diagblocksparse.jl:565  
> …  
> Stacktrace:  
> [1] \*(A::MPS, B::MPS; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})  
> @ ITensors .julia/packages/ITensors/fpBnt/src/mps/abstractmps.jl:1681  
> [2] \*(A::MPS, B::MPS)  
> @ ITensors .julia/packages/ITensors/fpBnt/src/mps/abstractmps.jl:1681  
> [3] top-level scope  
> @ REPL[12]:1

Is there some concise solution for this Kronecker product of MPS or am I missing something obvious?

For Background: I try to create a specific MPS iteratively by appending new sites.
