Skip to content

Commit bfe1f99

Browse files
committed
fix entanglement
1 parent 4ecf694 commit bfe1f99

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/entropy.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Calculates the [entanglement entropy](https://en.wikipedia.org/wiki/Entropy_of_e
195195
196196
# Notes
197197
198-
- `ρ` can be either a [`Ket`](@ref) or an [`Operator`](@ref).
198+
- `ρ` can be either a [`Ket`](@ref) or an [`Operator`](@ref). But should be a pure state.
199199
- `sel` specifies the indices of the remaining sub-system. See also [`ptrace`](@ref).
200200
- `kwargs` are the keyword arguments for calculating Von Neumann entropy. See also [`entropy_vn`](@ref).
201201
"""
@@ -204,10 +204,16 @@ function entanglement(
204204
sel::Union{Int,AbstractVector{Int},Tuple},
205205
kwargs...,
206206
) where {OpType<:Union{KetQuantumObject,OperatorQuantumObject}}
207-
= normalize(ρ)
208-
ρ_tr = ptrace(_ρ, sel)
207+
p = purity(ρ)
208+
isapprox(p, 1; atol = 1e-2) || throw(
209+
ArgumentError(
210+
"The entanglement entropy only works for normalized pure state, the purity of the given state: $(p) ≉ 1",
211+
),
212+
)
213+
214+
ρ_tr = ptrace(ρ, sel)
209215
val = entropy_vn(ρ_tr; kwargs...)
210-
return (val > 0) * val
216+
return max(0.0, val) # use 0.0 to make sure it always return value in Float-type
211217
end
212218

213219
@doc raw"""
@@ -220,7 +226,11 @@ Calculate the [concurrence](https://en.wikipedia.org/wiki/Concurrence_(quantum_c
220226
- `ρ` can be either a [`Ket`](@ref) or an [`Operator`](@ref).
221227
"""
222228
function concurrence::QuantumObject{OpType}) where {OpType<:Union{KetQuantumObject,OperatorQuantumObject}}
223-
.dimensions == Dimensions((Space(2), Space(2)))) || throw(ArgumentError("The `concurrence` only support for a two-qubit state, invalid dims = $(_get_dims_string.dimensions))."))
229+
.dimensions == Dimensions((Space(2), Space(2)))) || throw(
230+
ArgumentError(
231+
"The `concurrence` only works for a two-qubit state, invalid dims = $(_get_dims_string.dimensions)).",
232+
),
233+
)
224234

225235
= ket2dm(ρ).data
226236
σy = sigmay()
@@ -233,4 +243,4 @@ function concurrence(ρ::QuantumObject{OpType}) where {OpType<:Union{KetQuantumO
233243
λ = sqrt.(abs.(real(eigvals(_ρ * ρ_tilde; sortby = x -> -real(x)))))
234244

235245
return max(0.0, λ[1] - λ[2] - λ[3] - λ[4]) # use 0.0 to make sure it always return value in Float-type
236-
end
246+
end

test/core-test/entropy_and_metric.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ end
7272
@test isapprox(val, sqrt(2 * entropy_linear(ptrace(ψr, 1))); atol = 1e-5) # √(2 * (1 - Tr(ρA^2)))
7373
@test isapprox(val, sqrt(2 * entropy_linear(ptrace(ψr, 2))); atol = 1e-5) # √(2 * (1 - Tr(ρB^2)))
7474

75-
@test_throws ArgumentError concurrence(rand_dm(4))
75+
@test_throws ArgumentError entanglement(rand_dm((2, 2)), 1)
7676
@test_throws ArgumentError concurrence(rand_dm((2, 3)))
77+
@test_throws ArgumentError concurrence(rand_dm(4))
7778

7879
@testset "Type Stability (entanglement)" begin
7980
@inferred entanglement(ψb, 1)

0 commit comments

Comments
 (0)