# partial trace of MPO

**URL:** <https://itensor.discourse.group/t/partial-trace-of-mpo/98>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mpo\
**Created:** [April 30, 2022, 9:12am UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98 "2022-04-30T09:12:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgh](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cgh](https://itensor.discourse.group/u/cgh)\
**Post date:** [April 30, 2022, 9:12am UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/1 "2022-04-30T09:12:32Z")

</div>

Hi miles,  
I want to ask more details from the question :[https://itensor.discourse.group/t/trace-and-partial-trace-of-mpo/79](https://itensor.discourse.group/t/trace-and-partial-trace-of-mpo/79)

For instance, I have a MPO `rho`:

```auto
n = 6
s = siteinds("S=1/2",n)
rho = randomMPO(s)

```

I want to get another MPO `M` with partial trace:

 ![Screenshot_20220430_170600_com.newskyer.draw_edit](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/282f751b778c1848eb3cc6debda69beb52d0a5be.jpeg)

I have tried code in above link but I do not know what I can get through them.  
How can I get M?

---

<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:** [April 30, 2022, 6:59pm UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/2 "2022-04-30T18:59:22Z")

</div>

Hi cgh, here is the code for the `tr` of MPO function inside of ITensor:

```auto
function tr(M::MPO; plev::Pair{Int,Int}=0 => 1, tags::Pair=ts"" => ts"")
  N = length(M)
  L = tr(M[1]; plev=plev, tags=tags)
  for j in 2:N
    L *= M[j]
    L = tr(L; plev=plev, tags=tags)
  end
  return L
end

```

What you could do to adapt this to your case is the following:

1. change the range in the for loop to only go from 1:2 or 1:NL where NL is the number of tensors you want to trace over on the left
2. multiply L into the next tensor of the MPO, so the one at location NL+1
3. define a new MPO of length `(N-NL)` and assign the MPO tensors from `NL+1:N` to it

Do you think that will work for your case?

---

<div class="post-metadata">

**Author:** ![cgh](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cgh](https://itensor.discourse.group/u/cgh)\
**Post date:** [May 1, 2022, 3:44am UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/3 "2022-05-01T03:44:41Z")

</div>

Yeah. I change the function and it works well. Thank you so much.

---

<div class="post-metadata">

**Author:** ![vyason](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/vyason/32/682_2.png) [@vyason](https://itensor.discourse.group/u/vyason)\
**Post date:** [December 22, 2024, 8:02pm UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/4 "2024-12-22T20:02:29Z")

</div>

@miles How do I change this code if I divide my sites (1 – N) into these parts A B C D E.

Region A: 1–n1  
Region B: n1+1 – n2  
Region C: n2+1 – n3  
Region D: n3+1 – n4  
Region E: n4+1 – N

And I want to trace out regions A C and E, and find the reduced MPO for sites B and D only.

Thanks a lot for your code snippets! They are really helpful 🙂

---

<div class="post-metadata">

**Author:** ![cgh](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cgh](https://itensor.discourse.group/u/cgh)\
**Post date:** [December 23, 2024, 8:05am UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/5 "2024-12-23T08:05:40Z")

</div>

I think you can finish this via ITensor, but using partial trace three times.  
First tracing out A as

```auto
    p1=outer(psi',psi)

    k1=n1
    L1 = tr(p1[1])
    for i = 2:k1
        L1 *= p1[i]
        L1 = tr(L1)
    end

    p1[k1+1]=L1*p1[k1+1]

```

Do this for C and E similarly.

---

<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:** [January 2, 2025, 8:06am UTC](https://itensor.discourse.group/t/partial-trace-of-mpo/98/6 "2025-01-02T08:06:17Z")

</div>

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