# Calculate second order renyi entropy of a MPS state.

**URL:** <https://itensor.discourse.group/t/calculate-second-order-renyi-entropy-of-a-mps-state/353>\
**Category:** ITensor Julia Questions\
**Created:** [August 17, 2022, 1:22pm UTC](https://itensor.discourse.group/t/calculate-second-order-renyi-entropy-of-a-mps-state/353 "2022-08-17T13:22:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![XiaoTailong](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/xiaotailong/32/130_2.png) [@XiaoTailong](https://itensor.discourse.group/u/XiaoTailong)\
**Post date:** [August 17, 2022, 1:22pm UTC](https://itensor.discourse.group/t/calculate-second-order-renyi-entropy-of-a-mps-state/353/1 "2022-08-17T13:22:38Z")

</div>

Suppose I get a MPS state psi when I finish a time evolution, I want to calculate its second renyi entropy. Refer to the tutorial code in Itensor, I write down the code like this

```auto
function calcuate_entropy(psi_e, c)
    orthogonalize!(psi_e, c)
    ## which does not modify the original tensor data
    U,S,V = svd(psi_e[c], (linkind(psi_e, c-1), siteind(psi_e,c)))
    SvN = 0.0
    for n=1:dim(S, 1)
        p = S[n,n]^2
        SvN -= log(p^2)
    end
    return SvN
end

```

where psi\_e is the MPS state, c is partition position. I am not sure that my code can give the right answer. The second renyi entropy can be calculated by S = -\log(\text{Tr}[\rho^2]).

Thanks.

---

<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 17, 2022, 2:01pm UTC](https://itensor.discourse.group/t/calculate-second-order-renyi-entropy-of-a-mps-state/353/2 "2022-08-17T14:01:05Z")

</div>

Hi, so here it’s helpful to write out each type of entropy formula as an explicit sum over the eigenvalues of \rho. Let’s call these eigenvalues p\_n.

For second Renyi entropy S\_2, it is:

S\_2 = -\log(\text{Tr}[\rho^2]) = -\log(\sum\_n p^2\_n)

For von Neumann entropy S\_{vN} it is:

S\_{vN} = -\text{Tr}[\rho \log\rho] = -\sum\_n p\_n \log(p\_n)

In contrast, your code is computing -\sum\_n \log(p\_n) which is not equal to either of these formulas. I think the fix you need is to first sum the squares of the eigenvalues of \rho, then take the log afterwards versus taking the log of each value in the sum.

---

<div class="post-metadata">

**Author:** ![XiaoTailong](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/xiaotailong/32/130_2.png) [@XiaoTailong](https://itensor.discourse.group/u/XiaoTailong)\
**Post date:** [August 18, 2022, 2:31am UTC](https://itensor.discourse.group/t/calculate-second-order-renyi-entropy-of-a-mps-state/353/3 "2022-08-18T02:31:59Z")

</div>

Thanks. 😁
