Your entanglement
function code is very nice! One improvement that’s needed is to check if p > 0.0
before plugging it into the entropy formula, just because otherwise you can sometimes get a NaN result if p==0.0
.
I believe there’s nothing wrong with the code you posted, and I tested it, so the problems must be these:
- the state
psi
you are passing may not be the state you were intending to make (may not have the entanglement you expect, and/or might not be correctly normalized)
- for 4 qubits which are maximally entangled, the largest entropy possible is 2 \log(2) not 4 \log(2). This is because such a state will consist of two entangled pairs of qubits.
Here is a code I wrote to test your function on two representative states psiA
and psiB
which are as described in the comments below:
using ITensors
function entanglement(psi, b)
orthogonalize!(psi, b)
U,S,V = svd(psi[b], (linkind(psi, b-1), siteind(psi, b)) )
SVN = 0.0
for n=1: dim(S, 1)
p = S[n, n]^2
if p > 0.0
SVN = (SVN - p * log(p) )
end
end
SVN= SVN/log(2)
return SVN
end
let
N = 4
s = siteinds("S=1/2",N)
# Bell pair betweeen (2,3), 1 and 4 in a product state
A = ITensor(s...)
A[1,1,1,1] = 1/√2
A[1,2,2,1] = 1/√2
psiA = MPS(A,s)
@show norm(psiA)
@show entanglement(psiA,2)
# Two Bell pairs (2,3), (1,4)
B = ITensor(s...)
B[1,1,1,1] = 1/2
B[2,1,1,2] = 1/2
B[1,2,2,1] = 1/2
B[2,2,2,2] = 1/2
psiB = MPS(B,s)
@show norm(psiB)
@show entanglement(psiB,2)
return
end