@@ -236,3 +236,46 @@ function avg_gate_fidelity(x::T, y::T) where T <: Union{PauliTransferMatrix{B, B
236236 dim = 2 ^ length (x. basis_l)
237237 return (tr (transpose (x. data) * y. data) + dim) / (dim^ 2 + dim)
238238end
239+
240+ """
241+ entanglement_entropy(state, partition, [entropy_fun=entropy_vn])
242+
243+ Computes the entanglement entropy of `state` between the list of sites `partition`
244+ and the rest of the system. The state must be defined in a composite basis.
245+
246+ If `state isa AbstractOperator` the operator-space entanglement entropy is
247+ computed, which has the property
248+ ```julia
249+ entanglement_entropy(dm(ket)) = 2 * entanglement_entropy(ket)
250+ ```
251+
252+ By default the computed entropy is the Von-Neumann entropy, but a different
253+ function can be provided (for example to compute the entanglement-renyi entropy).
254+ """
255+ function entanglement_entropy (psi:: Ket{B} , partition:: Vector{Int} , entropy_fun= entropy_vn) where B<: CompositeBasis
256+ # check that sites are within the range
257+ @assert all (partition .<= length (psi. basis. bases))
258+
259+ rho = ptrace (psi, partition)
260+ return entropy_fun (rho)
261+ end
262+
263+ function entanglement_entropy (rho:: DenseOperator{B,B} , partition:: Vector{Int} , args... ) where {B<: CompositeBasis }
264+ # check that sites is within the range
265+ hilb = rho. basis_l
266+ all (partition .<= length (hilb. bases)) || throw (ArgumentError (" Indices in partition must be within the bounds of the composite basis." ))
267+ length (partition) <= length (hilb. bases) || throw (ArgumentError (" Partition cannot include the whole system." ))
268+
269+ # build the doubled hilbert space for the vectorised dm, normalized like a Ket.
270+ b_doubled = hilb^ 2
271+ rho_vec = normalize! (Ket (b_doubled, vec (rho. data)))
272+
273+ return entanglement_entropy (rho_vec,
274+ vcat (partition, partition.+ length (hilb. bases)),
275+ args... )
276+ end
277+
278+ entanglement_entropy (state, partition:: Number , args... ) =
279+ entanglement_entropy (state, [partition], args... )
280+ entanglement_entropy (state, partition, args... ) =
281+ entanglement_entropy (state, collect (partition), args... )
0 commit comments