# Entanglement entropy (Julia)

Hello,
how we can calculate the entanglement entropy of an half chain with Julia?
I am trying to adapt the following lines of code which are given for C++

Thanks a lot!
Jacopo

auto b = 10;

//"Gauge" the MPS to site b
psi.position(b);

//SVD this wavefunction to get the spectrum
//of density-matrix eigenvalues
auto s = siteIndex(psi,b);
auto [U,S,V] = svd(psi(b),{l,s});
auto u = commonIndex(U,S);

//Apply von Neumann formula
//to the squares of the singular values
Real SvN = 0.;
for(auto n : range1(dim(u)))
{
auto Sn = elt(S,n,n);
auto p = sqr(Sn);
if(p > 1E-12) SvN += -p*log(p);
}

selected

Hi Jacopo,

Here's a code for computing the von Neumann entropy in Julia:

using ITensors

function entropy_von_neumann(psi::MPS, b::Int)
orthogonalize!(psi, b)
_,S = svd(psi[b], (linkind(psi, b-1), s[b]))
SvN = 0.0
for n in dim(S, 1)
p = S[n,n]^2
SvN -= p * log(p)
end
return SvN
end

N = 10
s = siteinds("S=1/2", N)
psi = randomMPS(s, 4)
SvN = entropy_von_neumann(psi, b)


Hopefully we will find time to create code formulas for common operations like this in Julia.

Cheers,
Matt

commented by (200 points)
thanks a lot!