# Questions about \`apply\`

**URL:** <https://itensor.discourse.group/t/questions-about-apply/2090>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mps, mpo\
**Created:** [October 27, 2024, 1:17pm UTC](https://itensor.discourse.group/t/questions-about-apply/2090 "2024-10-27T13:17:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John-J-Tanner](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/john-j-tanner/32/654_2.png) [@John-J-Tanner](https://itensor.discourse.group/u/John-J-Tanner)\
**Post date:** [October 27, 2024, 1:17pm UTC](https://itensor.discourse.group/t/questions-about-apply/2090/1 "2024-10-27T13:17:52Z")

</div>

Hi there,

Just some quick questions about the `apply` method (sorry if the answers are stated in the docs or forum somewhere, but I couldn’t find them).

On the ITensor website in _Tutorials \> MPS Time Evolution_, the line `psi = apply(gates, psi; cutoff)` passes `gates`, which is of type `Array{ITensor, 1}`, to the first argument of `apply`. In the docs I can only find info about the behaviour of `apply` when the first argument is an MPO.

1. Does the `apply` method in the _MPS Time Evolution_ code:  
(A) Convert each of the operations in `gates` into a separate MPO and apply them one by one to the MPS? Or  
(B) Convert `gates` into a single MPO and then contract with the MPS?

In _Code Examples \> MPS and MPO Examples \> Applying a Two-site Operator to an MPS_, the code explicitly moves the orthogonality centre of the MPS appropriately, resets the prime level of the indices to 0, and groups the singular values to the right in the last step.

1. (Assuming the answer to Q1 is A) I know that `apply` deals with the index primes, but in the _MPS Time Evolution_ code, does `apply` also automatically move the orthogonality centre appropriately (as in the _Applying a Two-site Operator to an MPS_ example) each time an operation is applied? And are the singular values grouped to the right-most site of the last operation applied to the MPS (which, if my understanding is correct, means the orthogonality centre is also on the right-most site of the final operation)?

Thanks so much!

---

<div class="post-metadata">

**Author:** ![ryanlevy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/ryanlevy/32/150_2.png) [@ryanlevy](https://itensor.discourse.group/u/ryanlevy)\
**Post date:** [October 29, 2024, 1:51pm UTC](https://itensor.discourse.group/t/questions-about-apply/2090/2 "2024-10-29T13:51:37Z")

</div>

> [@John-J-Tanner](#):
>
> Does the `apply` method in the _MPS Time Evolution_ code:  
> (A) Convert each of the operations in `gates` into a separate MPO and apply them one by one to the MPS? Or  
> (B) Convert `gates` into a single MPO and then contract with the MPS?

Neither of them really, it follows this doc [MPS and MPO · ITensors.jl](https://itensor.github.io/ITensors.jl/stable/MPSandMPO.html#ITensors.product-Tuple%7BITensor,%20AbstractMPS%7D)

It should first swap sites of the gates if they are not contiguous, then contract the MPS/MPO tensors to match the gate, apply the gate, and decompose back (and potentially move the sites back). This is the same as the two site operator example you mention

> [@John-J-Tanner](#):
>
> (Assuming the answer to Q1 is A) I know that `apply` deals with the index primes, but in the _MPS Time Evolution_ code, does `apply` also automatically move the orthogonality centre appropriately (as in the _Applying a Two-site Operator to an MPS_ example) each time an operation is applied? And are the singular values grouped to the right-most site of the last operation applied to the MPS (which, if my understanding is correct, means the orthogonality centre is also on the right-most site of the final operation)?

Yes, apply should be taking care of the orthogonality center movements. The orthogonality center is moved to the first site in the operator before its applied. As you can see in the code snippet below, this can probably be optimized a bit more (PRs welcome!)

> <https://github.com/ITensor/ITensorMPS.jl/blob/main/src/abstractmps.jl#L2121>

The resulting MPS should have the orthogonality center on the right-most site of the gate at the end, looking at [source](https://github.com/ITensor/ITensorMPS.jl/blob/main/src/abstractmps.jl#L1933)

---

<div class="post-metadata">

**Author:** ![John-J-Tanner](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/john-j-tanner/32/654_2.png) [@John-J-Tanner](https://itensor.discourse.group/u/John-J-Tanner)\
**Post date:** [October 30, 2024, 4:27am UTC](https://itensor.discourse.group/t/questions-about-apply/2090/3 "2024-10-30T04:27:35Z")

</div>

Hi Ryan,

I see, thanks for explaining that and pointing me to all the right parts of the docs and source code, everything is much clearer now 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [November 9, 2024, 4:27am UTC](https://itensor.discourse.group/t/questions-about-apply/2090/4 "2024-11-09T04:27:42Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
