# about orthogonalize! function

**URL:** <https://itensor.discourse.group/t/about-orthogonalize-function/904>\
**Category:** ITensor Julia Questions\
**Created:** [May 29, 2023, 8:20am UTC](https://itensor.discourse.group/t/about-orthogonalize-function/904 "2023-05-29T08:20:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zz123](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zz123](https://itensor.discourse.group/u/zz123)\
**Post date:** [May 29, 2023, 8:20am UTC](https://itensor.discourse.group/t/about-orthogonalize-function/904/1 "2023-05-29T08:20:50Z")

</div>

Dear there,  
I have a quick question about calculating expectation of a two-site operator like simply c^{\dag}\_jc\_{j+1} with a MPS got from DMRG. I tried two ways as follows, and my questions is I find they give totally different results:

```auto
# Method 1 -- use MPO
value=0
for j in 1:1:(N-1)
    hop = OpSum()
    hop += 1.0, "Cdagup", j, "Cup", j + 1
    hop_mpo = MPO(hop,sites) 
    value = inner(psi0',hop_mpo,psi0)
    println("j=$j, C+upCup=$value")
end
       
# Method 2 -- use orthogonalize  
value2 = 0
for j in 1:1:(N-1)
	orthogonalize!(psi0, j)
	psi0_j = psi0[j]*psi0[j+1]
	psidag0_j = dag(prime(psi0[j]*psi0[j+1], "Site"))
	ampo = op(sites, "Cdagup", j) * op(sites, "Cup", j + 1)
	value2 = scalar(psidag0_j * ampo * psi0_j) 
	println("j=$j, C+upCup=$value2")
end

```

So in the 1st way, I made c^{\dag}\_jc\_{j+1} as a MPO, and in the 2nd way I orthogonalize at site j and use op. But I’m worried about if the 2nd way is valid because for each \langle c^{\dag}\_jc\_{j+1}\rangle I use both site j and j+1, but I only orthogonalize j not j+1.

So I’m just wondering could you help to see what’s wrong in 1 or 2 so that the results are different, and if the 2nd way is indeed wrong due to my orthoginalize, then should I orthogonalize two sites simultaneously or maybe this way can only be used for single-site operator expectation?

Thank you so much!!

---

<div class="post-metadata">

**Author:** ![JanReimers](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/janreimers/32/171_2.png) [@JanReimers](https://itensor.discourse.group/u/JanReimers)\
**Post date:** [May 29, 2023, 3:08pm UTC](https://itensor.discourse.group/t/about-orthogonalize-function/904/2 "2023-05-29T15:08:17Z")

</div>

Hi zz, The MPO method should give you correct answer, but it is very inefficient. I would also recommend that you add a third method and that is using the [correlation\_matrix](https://itensor.github.io/ITensors.jl/stable/MPSandMPO.html#ITensors.correlation_matrix-Tuple%7BMPS,%20AbstractString,%20AbstractString%7D) function. It does what you are trying to do in method 2. It orthogonalizes on site j and the calculates what is effectively a left envorinment for the operator at site j, and sweeps that environment through to site j+n.  
You can view the code [here](https://github.com/ITensor/ITensors.jl/blob/95397a1e3b3001cf2144b4bea1e4917f6a9efea4/src/mps/mps.jl#LL775C2-L802C8) to see how it works. This should be the most efficient way to calculate multi site expectations.  
Let me know if you have more questions.  
I hope this helps.  
JR

---

<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:** [May 30, 2023, 2:13pm UTC](https://itensor.discourse.group/t/about-orthogonalize-function/904/3 "2023-05-30T14:13:36Z")

</div>

To answer the original question as to why method 2 is wrong, when applying ops to the MPS like that you have to keep track of the [fermion strings](https://itensor.org/docs.cgi?page=tutorials/fermions) yourself, which the helper function Jan posted does. To fix your method 2 code, just change `"Cdagup"` → `"Cdagup * F"`.

As for the orthogonalize, this works because your operator is acting on sites `(j,j+1)` and the rest of the MPS contracts into an identity. It can be helpful to draw this contraction out in order to see why you only need either `j` or `j+1` called in `orthogonalize!`
