Skip to content

Commit 13071e1

Browse files
committed
introduce hilbert_dist
1 parent 46b98de commit 13071e1

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

docs/src/resources/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ entropy_linear
259259
entropy_mutual
260260
entropy_conditional
261261
entanglement
262-
tracedist
263262
fidelity
263+
tracedist
264+
hilbert_dist
264265
```
265266

266267
## [Spin Lattice](@id doc-API:Spin-Lattice)

docs/src/users_guide/states_and_operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ coherent_dm(5, 1.25)
161161
thermal_dm(5, 1.25)
162162
```
163163

164-
`QuantumToolbox` also provides a set of distance metrics for determining how close two density matrix distributions are to each other. Included are the [`fidelity`](@ref), and trace distance ([`tracedist`](@ref)).
164+
`QuantumToolbox` also provides a set of distance metrics (see section [Entropy and Metrics](@ref doc-API:Entropy-and-Metrics) in API page) for determining how close two density matrix distributions are to each other. Included are the [`fidelity`](@ref), and trace distance ([`tracedist`](@ref)).
165165

166166
```@example states_and_operators
167167
x = coherent_dm(5, 1.25)

src/metrics.jl

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@
22
Functions for calculating metrics (distance measures) between states and operators.
33
=#
44

5-
export tracedist, fidelity
5+
export fidelity
6+
export tracedist, hilbert_dist
7+
8+
@doc raw"""
9+
fidelity(ρ::QuantumObject, σ::QuantumObject)
10+
11+
Calculate the fidelity of two [`QuantumObject`](@ref):
12+
``F(\hat{\rho}, \hat{\sigma}) = \textrm{Tr} \sqrt{\sqrt{\hat{\rho}} \hat{\sigma} \sqrt{\hat{\rho}}}``
13+
14+
Here, the definition is from Nielsen & Chuang, "Quantum Computation and Quantum Information". It is the square root of the fidelity defined in R. Jozsa, Journal of Modern Optics, 41:12, 2315 (1994).
15+
16+
Note that `ρ` and `σ` must be either [`Ket`](@ref) or [`Operator`](@ref).
17+
"""
18+
function fidelity::QuantumObject{OperatorQuantumObject}, σ::QuantumObject{OperatorQuantumObject})
19+
sqrt_ρ = sqrt(ρ)
20+
eigval = abs.(eigvals(sqrt_ρ * σ * sqrt_ρ))
21+
return sum(sqrt, eigval)
22+
end
23+
fidelity::QuantumObject{OperatorQuantumObject}, ψ::QuantumObject{KetQuantumObject}) = sqrt(abs(expect(ρ, ψ)))
24+
fidelity::QuantumObject{KetQuantumObject}, σ::QuantumObject{OperatorQuantumObject}) = fidelity(σ, ψ)
25+
fidelity::QuantumObject{KetQuantumObject}, ϕ::QuantumObject{KetQuantumObject}) = abs(dot(ψ, ϕ))
626

727
@doc raw"""
828
tracedist(ρ::QuantumObject, σ::QuantumObject)
@@ -21,20 +41,22 @@ tracedist(
2141
} = norm(ket2dm(ρ) - ket2dm(σ), 1) / 2
2242

2343
@doc raw"""
24-
fidelity(ρ::QuantumObject, σ::QuantumObject)
44+
hilbert_dist(ρ::QuantumObject, σ::QuantumObject)
2545
26-
Calculate the fidelity of two [`QuantumObject`](@ref):
27-
``F(\hat{\rho}, \hat{\sigma}) = \textrm{Tr} \sqrt{\sqrt{\hat{\rho}} \hat{\sigma} \sqrt{\hat{\rho}}}``
28-
29-
Here, the definition is from Nielsen & Chuang, "Quantum Computation and Quantum Information". It is the square root of the fidelity defined in R. Jozsa, Journal of Modern Optics, 41:12, 2315 (1994).
46+
Calculates the Hilbert-Schmidt distance between two [`QuantumObject`](@ref):
47+
``D_{HS}(\hat{\rho}, \hat{\sigma}) = \textrm{Tr}\left[\hat{A}^\dagger \hat{A}\right]``, where ``\hat{A} = \hat{\rho} - \hat{\sigma}``.
3048
3149
Note that `ρ` and `σ` must be either [`Ket`](@ref) or [`Operator`](@ref).
3250
"""
33-
function fidelity::QuantumObject{OperatorQuantumObject}, σ::QuantumObject{OperatorQuantumObject})
34-
sqrt_ρ = sqrt(ρ)
35-
eigval = abs.(eigvals(sqrt_ρ * σ * sqrt_ρ))
36-
return sum(sqrt, eigval)
51+
function hilbert_dist(
52+
ρ::QuantumObject{ObjType1},
53+
σ::QuantumObject{ObjType2},
54+
) where {
55+
ObjType1<:Union{KetQuantumObject,OperatorQuantumObject},
56+
ObjType2<:Union{KetQuantumObject,OperatorQuantumObject},
57+
}
58+
check_dimensions(ρ, σ)
59+
60+
A = ket2dm(ρ) - ket2dm(σ)
61+
return tr(A' * A)
3762
end
38-
fidelity::QuantumObject{OperatorQuantumObject}, ψ::QuantumObject{KetQuantumObject}) = sqrt(abs(expect(ρ, ψ)))
39-
fidelity::QuantumObject{KetQuantumObject}, σ::QuantumObject{OperatorQuantumObject}) = fidelity(σ, ψ)
40-
fidelity::QuantumObject{KetQuantumObject}, ϕ::QuantumObject{KetQuantumObject}) = abs(dot(ψ, ϕ))

0 commit comments

Comments
 (0)