• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iitis / QuantumInformation.jl / 20922195133

12 Jan 2026 02:04PM UTC coverage: 92.391% (-0.05%) from 92.445%
20922195133

Pull #108

github

web-flow
Merge 3f49c6580 into cc74f1371
Pull Request #108: Support for arbitrary element types and sparse matrices

17 of 20 new or added lines in 4 files covered. (85.0%)

38 existing lines in 9 files now uncovered.

680 of 736 relevant lines covered (92.39%)

158.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.36
/src/functionals.jl
1
export norm_trace, trace_distance, norm_hs, hs_distance,
2
    purity, fidelity_sqrt, fidelity, gate_fidelity, shannon_entropy,
3
    vonneumann_entropy, renyi_entropy, relative_entropy, kl_divergence,
4
    js_divergence, bures_distance, bures_angle, superfidelity, negativity,
5
    log_negativity, ppt, concurrence
6

7
"""
8

9
- `A`: matrix.
10

11
Return [trace norm](https://www.quantiki.org/wiki/trace-norm) of matrix `A`.
12
"""
13
norm_trace(A::AbstractMatrix{<:Number}) = sum(svdvals(A))
31✔
14

15
"""
16

17
- `A`: matrix.
18
- `B`: matrix.
19

20
Return [trace distance](https://www.quantiki.org/wiki/trace-distance) between matrices `A` and `B`.
21
"""
22
function trace_distance(A::AbstractMatrix{T1}, B::AbstractMatrix{T2}) where {T1<:Number, T2<:Number}
9✔
23
    norm_trace(A - B) / 2
9✔
24
end
25

26
"""
27

28
- `A`: matrix.
29

30
Return [Hilbert–Schmidt norm](https://en.wikipedia.org/wiki/Hilbert%E2%80%93Schmidt_operator) of matrix `A`.
31
"""
32
norm_hs(A::AbstractMatrix{<:Number}) = sqrt(sum(abs2.(A)))
12✔
33

34
"""
35

36
- `A`: matrix.
37
- `B`: matrix.
38

39
Return [Hilbert–Schmidt distance](https://en.wikipedia.org/wiki/Hilbert%E2%80%93Schmidt_operator) between matrices `A` and `B`.
40
"""
41
function hs_distance(A::AbstractMatrix{<:Number}, B::AbstractMatrix{<:Number})
6✔
42
    norm_hs(A - B)
6✔
43
end
44

45
"""
46

47
- `ρ`: matrix.
48

49
Return the purity of `ρ` ∈ [1/d, 1]
50
"""
51
purity(ρ::AbstractMatrix{<:Number}) = tr(ρ^2)
3✔
52

53
"""
54

55
- `ρ`: matrix.
56
- `σ`: matrix.
57

58
Return square root of [fidelity](https://www.quantiki.org/wiki/fidelity) between matrices `ρ` and `σ`.
59
"""
60
function fidelity_sqrt(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
24✔
61
  if size(ρ, 1) != size(ρ, 2) || size(σ, 1) != size(σ, 2)
45✔
62
    throw(ArgumentError("Non square matrix"))
3✔
63
  end
64
  λ = real(eigvals(ρ * σ))
24✔
65
  r = real(sum(sqrt.(λ[λ.>0])))
21✔
66
end
67

68
"""
69

70
- `ρ`: matrix.
71
- `σ`: matrix.
72

73
Return [fidelity](https://www.quantiki.org/wiki/fidelity) between matrices `ρ` and `σ`.
74
"""
75
function fidelity(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
6✔
76
  fidelity_sqrt(ρ, σ)^2
6✔
77
end
78

79
fidelity(ϕ::AbstractVector{<:Number}, ψ::AbstractVector{<:Number}) = abs2(dot(ϕ, ψ))
6✔
80
fidelity(ϕ::AbstractVector{<:Number}, ρ::AbstractMatrix{<:Number}) = real(ϕ' * ρ * ϕ)
6✔
81
fidelity(ρ::AbstractMatrix{<:Number}, ϕ::AbstractVector{<:Number}) = fidelity(ϕ, ρ)
3✔
82

83
"""
84

85
- `U`: quantum gate.
86
- `V`: quantum gate.
87

88
Return [fidelity](https://www.quantiki.org/wiki/fidelity) between gates `U` and `V`.
89
"""
90
function gate_fidelity(U::AbstractMatrix{<:Number}, V::AbstractMatrix{<:Number})
15✔
91
    abs(1.0 / size(U,1) * tr(U'*V))
15✔
92
end
93

94
"""
95

96
- `p`: vector.
97

98
Return [Shannon entorpy](https://en.wikipedia.org/wiki/Entropy_(information_theory)) of vector `p`.
99
"""
100
shannon_entropy(p::AbstractVector{<:Real}) = -sum(p .* log.(p))
24✔
101

102
"""
103

104
- `x`: real number.
105

106
Return binary [Shannon entorpy](https://en.wikipedia.org/wiki/Entropy_(information_theory)) given by \$-x  \\log(x) - (1 - x)  \\log(1 - x)\$.
107
"""
108
shannon_entropy(x::Real) = x > 0 ? -x * log(x) - (1 - x) * log(1 - x) : error("Negative number passed to shannon_entropy")
6✔
109

110
"""
111

112
- `ρ`: quantum state.
113

114
Return [Von Neumann entropy](https://en.wikipedia.org/wiki/Von_Neumann_entropy) of quantum state `ρ`.
115
"""
116
function vonneumann_entropy(ρ::Hermitian{<:Number})
21✔
117
    λ = eigvals(ρ)
21✔
118
    shannon_entropy(λ[λ .> 0])
21✔
119
end
120

121
vonneumann_entropy(H::AbstractMatrix{<:Number}) = ishermitian(H) ? vonneumann_entropy(Hermitian(H)) : error("Non-hermitian matrix passed to entropy")
21✔
122
vonneumann_entropy(ϕ::AbstractVector{T}) where T<:Number = zero(T)
3✔
123

124
"""
125

126
- `ρ`: quantum state.
127
- `α`: order of Renyi entropy.
128

129
Return [Renyi entropy](https://en.wikipedia.org/wiki/R%C3%A9nyi_entropy) of quantum state `ρ`.
130
"""
131
function renyi_entropy(ρ::Hermitian{<:Number}, α::Real)
6✔
132

133
    α >= 0 && α!=1 ? () : throw(ArgumentError("Parameter α must be α ≥ 0 and α ≠ 1"))
6✔
134
    λ = eigvals(ρ)
6✔
135
    λ = λ[λ .> 0]
6✔
136
    1/(1-α)*log(sum(λ.^α))
6✔
137
end
138

139
renyi_entropy(ρ::AbstractMatrix{<:Number}, α::Real) = renyi_entropy(Hermitian(ρ), α)
6✔
140
"""
141

142
- `ρ`: quantum state.
143
- `σ`: quantum state.
144

145
Return [quantum relative entropy](https://en.wikipedia.org/wiki/Quantum_relative_entropy) of quantum state `ρ` with respect to `σ`.
146
"""
147
function relative_entropy(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
18✔
148
    log_σ = funcmh(log, σ)
18✔
149
    real(-vonneumann_entropy(ρ) - tr(ρ * log_σ))
18✔
150
end
151

152
"""
153

154
- `ρ`: quantum state.
155
- `σ`: quantum state.
156

157
Return [Kullback–Leibler divergence](https://en.wikipedia.org/wiki/Quantum_relative_entropy) of quantum state `ρ` with respect to `σ`.
158
"""
159
function kl_divergence(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
10✔
160
    relative_entropy(ρ, σ)
12✔
161
end
162

163
"""
164

165
- `ρ`: quantum state.
166
- `σ`: quantum state.
167

168
Return [Jensen–Shannon divergence](https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence) of quantum state `ρ` with respect to `σ`.
169
"""
170
function js_divergence(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
3✔
171
    0.5kl_divergence(ρ, σ) + 0.5kl_divergence(σ, ρ)
3✔
172
end
173

174
"""
175

176
- `ρ`: quantum state.
177
- `σ`: quantum state.
178

179
Return [Bures distance](https://en.wikipedia.org/wiki/Bures_metric) between quantum states `ρ` and `σ`.
180
"""
181
function bures_distance(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
6✔
182
    sqrt(2 - 2 * fidelity_sqrt(ρ, σ))
6✔
183
end
184

185
"""
186

187
- `ρ`: quantum state.
188
- `σ`: quantum state.
189

190
Return [Bures angle](https://en.wikipedia.org/wiki/Bures_metric) between quantum states `ρ` and `σ`.
191
"""
192
function bures_angle(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
6✔
193
    acos(fidelity_sqrt(ρ, σ))
6✔
194
end
195

196
"""
197

198
- `ρ`: quantum state.
199
- `σ`: quantum state.
200

201
Return [superfidelity](https://www.quantiki.org/wiki/superfidelity) between quantum states `ρ` and `σ`.
202
"""
203
function superfidelity(ρ::AbstractMatrix{<:Number}, σ::AbstractMatrix{<:Number})
9✔
204
    return real(tr(ρ'*σ) + sqrt(1 - tr(ρ'*ρ)) * sqrt(1 - tr(σ'*σ)))
9✔
205
end
206

207
"""
208

209
- `ρ`: quantum state.
210
- `dims`: dimensions of subsystems.
211
- `sys`: transposed subsystem.
212

213
Return [negativity](https://www.quantiki.org/wiki/negativity) of quantum state `ρ`.
214
"""
215
function negativity(ρ::AbstractMatrix{<:Number}, dims::Vector{Int}, sys::Int)
9✔
216
    ρ_s = ptranspose(ρ, dims, sys)
9✔
217
    λ = eigvals(ρ_s)
9✔
218
    -real(sum(λ[λ .< 0]))
9✔
219
end
220

221
"""
222

223
- `ρ`: quantum state.
224
- `dims`: dimensions of subsystems.
225
- `sys`: transposed subsystem.
226

227
Return [log negativity](https://www.quantiki.org/wiki/negativity) of quantum state `ρ`.
228
"""
229
function log_negativity(ρ::AbstractMatrix{<:Number}, dims::Vector{Int}, sys::Int)
9✔
230
    ρ_s = ptranspose(ρ, dims, sys)
9✔
231
    log(norm_trace(ρ_s))
9✔
232
end
233

234
"""
235

236
- `ρ`: quantum state.
237
- `dims`: dimensions of subsystems.
238
- `sys`: transposed subsystem.
239

240
Return minimum eigenvalue of [positive partial transposition](https://www.quantiki.org/wiki/positive-partial-transpose) of quantum state `ρ`.
241
"""
242
function ppt(ρ::AbstractMatrix{<:Number}, dims::Vector{Int}, sys::Int)
9✔
243
    ρ_s = ptranspose(ρ, dims, sys)
9✔
244
    minimum(eigvals(ρ_s))
9✔
245
end
246

247
"""
248

249
- `ρ`: quantum state.
250

251
Calculates the [concurrence of a two-qubit system](https://en.wikipedia.org/wiki/Concurrence_(quantum_computing)) `ρ`.
252
"""
253
function concurrence(ρ::AbstractMatrix{<:Number})
6✔
254
    if size(ρ, 1) != 4
6✔
UNCOV
255
        throw(ArgumentError("Concurrence only properly defined for two qubit systems"))
×
256
    end
257

258
    σ = (sy ⊗ sy)*conj.(ρ)*(sy ⊗ sy)
12✔
259
    λ = sqrt.(sort(real(eigvals(ρ*σ)), rev=true))
6✔
260
    max(0, λ[1]-λ[2]-λ[3]-λ[4])
6✔
261
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc