# Computing Renyi entropy of a sub-system in the middle of a 1D chain

**URL:** <https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523>\
**Category:** ITensor Julia Questions\
**Tags:** julia, entanglement, mps, mpo\
**Created:** [November 6, 2025, 11:40am UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523 "2025-11-06T11:40:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamoghnaRay](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/tamoghnaray/32/360_2.png) [@tamoghnaRay](https://itensor.discourse.group/u/tamoghnaRay)\
**Post date:** [November 6, 2025, 11:40am UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523/1 "2025-11-06T11:40:10Z")

</div>

Hi, I have a spin- 1/2 XXZ chain of length L. I have a sub-system of length L\_S, in the middle of the chain. Say L = 10 and the system goes from site 4 to 7 (L\_S = 4). I want to compute S\_n = \frac{1}{1-n}\log[\text{Tr} \rho\_S^n]. I am using the following code to compute this -

Function to compute partial trace -

```julia
function PartialTrace(rho::MPO, # MPO of the total density matrix
                        start::Int, # start index of the sub-system
                        stop::Int # end index of the sub-system
                        )
    L = length(rho) # total system size
    M = stop - start + 1 # sub-system size
    L1 = ITensor(1.)
    for i = 1:start-1
        L1 *= rho[i] # contracting all the link indices of the matrices on the left
        L1 = tr(L1) # taking trace over the left part
    end
    # L1 = tr(L1) # taking trace over the left part
    rho[start] *= L1 # absorbing the left contraction to the first site of the sub-system
    R1 = ITensor(1.)
    for i = stop+1:L
        R1 *= rho[i] # contracting all the link indices of the matrices on the right
        R1 = tr(R1) # taking trace over the right part
    end
    # R1 = tr(R1) # taking trace over the right part
    rho[stop] *= R1 # absorbing the right contraction to the last site of the sub-system
    rho_reduced = MPO(M) # initializing the reduced density matrix MPO
    for i=1:M
        rho_reduced[i] = rho[start-1+i]
    end 
    return rho_reduced 
end

```

Computing reduced density matrix-

```julia
rho = outer(psi', psi) # psi is the MPS of the full system
rho_sys = PartialTrace(rho, start, stop) # reduced density matrix of the sub-system

```

There are two ways that I have used to compute S\_2. Method 2 seems faster, at least when L\_S is small.

```julia
# method - 1 
rho_sys_sq = apply(rho_sys, rho_sys)
tr_rho_sys_sq = tr(rho_sys_sq)
S2 = -log(real(tr_rho_sys_sq))

# method - 2
rho_sys_mat = prod(rho_sys)
D,U = eigen(rho_sys_mat, ishermitian=true)  
u_idx = commonind(U, D)
eigenvalues = [D[u_idx => k, prime(u_idx) => k] for k in 1:dim(u_idx)]
S2 = -log(sum(eigenvalues.^2))

```

The step that takes the most time is when I am computing `rho = outer(psi', psi) `. This time is quite significant compared to the rest of the steps, especially when the bond dimension is high. I have also used `rho = projector(psi)`. Both take a similar time to run.

1. Is there a more efficient way to compute the MPO `rho`, or is there a way to compute S\_2 directly from the MPS `psi`?
2. Another step that takes a lot of time is `rho_sys_mat = prod(rho_sys)`, where I compute the dense tensor from the MPO. Is there a better way to do this?

---

<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:** [November 6, 2025, 3:16pm UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523/2 "2025-11-06T15:16:04Z")

</div>

Yes, I’m not surprised that `outer(psi', psi)` is a bottleneck. The short answer is that making a density matrix that way is not an efficient way to do it and there are much more efficient ways.

One thing that might immediately help you is the ITensorEntropyTools.jl package:  
[https://itensor.discourse.group/t/introducing-itensorentropytools-jl/2192](https://itensor.discourse.group/t/introducing-itensorentropytools-jl/2192)  
One of the main features of this package is that it automatically determines the best strategy for computing density matrices of the type you are computing (there are roughly two good ways to compute these and it figures out which one).

If that package doesn’t cover your case, please reply below and we could give you additional pointers about how to do this calculation.

---

<div class="post-metadata">

**Author:** ![tamoghnaRay](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/tamoghnaray/32/360_2.png) [@tamoghnaRay](https://itensor.discourse.group/u/tamoghnaRay)\
**Post date:** [November 7, 2025, 12:05pm UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523/4 "2025-11-07T12:05:33Z")

</div>

Hi @miles, thanks for the suggestion. This package works fine for my case.

---

<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:** [November 7, 2025, 2:57pm UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523/5 "2025-11-07T14:57:27Z")

</div>

Glad to hear it!

---

<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 19, 2025, 9:34am UTC](https://itensor.discourse.group/t/computing-renyi-entropy-of-a-sub-system-in-the-middle-of-a-1d-chain/2523/6 "2025-11-19T09:34:07Z")

</div>

Closed on November 19, 2025, 9:34am UTC

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