Skip to content

Commit 7dcbf35

Browse files
committed
add tests for new metric functions
1 parent 350caae commit 7dcbf35

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

test/core-test/entropy_and_metric.jl

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565
end
6666
end
6767

68-
@testset "trace distance" begin
68+
@testset "trace and Hilbert-Schmidt distance" begin
6969
ψz0 = basis(2, 0)
7070
ψz1 = basis(2, 1)
7171
ρz0 = to_sparse(ket2dm(ψz0))
@@ -76,26 +76,81 @@ end
7676
@test tracedist(ψz1, ρz0) 1.0
7777
@test tracedist(ρz0, ρz1) 1.0
7878

79+
ψ = rand_ket(10)
80+
ϕ = rand_ket(10)
81+
@test isapprox(tracedist(ψ, ϕ)^2, hilbert_dist(ψ, ϕ) / 2; atol = 1e-6)
82+
7983
@testset "Type Inference (trace distance)" begin
8084
@inferred tracedist(ψz0, ψx0)
8185
@inferred tracedist(ρz0, ψz1)
8286
@inferred tracedist(ψz1, ρz0)
8387
@inferred tracedist(ρz0, ρz1)
8488
end
89+
90+
@testset "Type Inference (Hilbert-Schmidt distance)" begin
91+
@inferred hilbert_dist(ψz0, ψx0)
92+
@inferred hilbert_dist(ρz0, ψz1)
93+
@inferred hilbert_dist(ψz1, ρz0)
94+
@inferred hilbert_dist(ρz0, ρz1)
95+
end
8596
end
8697

87-
@testset "fidelity" begin
88-
M = sprand(ComplexF64, 5, 5, 0.5)
89-
M0 = Qobj(M * M')
90-
ψ1 = Qobj(rand(ComplexF64, 5))
91-
ψ2 = Qobj(rand(ComplexF64, 5))
92-
M1 = ψ1 * ψ1'
98+
@testset "fidelity, Bures metric, and Hellinger distance" begin
99+
M0 = rand_dm(5)
100+
ψ1 = rand_ket(5)
101+
ψ2 = rand_ket(5)
102+
M1 = ket2dm(ψ1)
103+
b00 = bell_state(Val(0), Val(0))
104+
b01 = bell_state(Val(0), Val(1))
93105
@test isapprox(fidelity(M0, M1), fidelity(ψ1, M0); atol = 1e-6)
94106
@test isapprox(fidelity(ψ1, ψ2), fidelity(ket2dm(ψ1), ket2dm(ψ2)); atol = 1e-6)
107+
@test isapprox(fidelity(b00, b00), 1; atol = 1e-6)
108+
@test isapprox(bures_dist(b00, b00) + 1, 1; atol = 1e-6)
109+
@test isapprox(bures_angle(b00, b00) + 1, 1; atol = 1e-6)
110+
@test isapprox(hellinger_dist(b00, b00) + 1, 1; atol = 1e-6)
111+
@test isapprox(fidelity(b00, b01) + 1, 1; atol = 1e-6)
112+
@test isapprox(bures_dist(b00, b01), 2; atol = 1e-6)
113+
@test isapprox(bures_angle(b00, b01), π / 2; atol = 1e-6)
114+
@test isapprox(hellinger_dist(b00, b01), 2; atol = 1e-6)
115+
116+
# some relations between Bures and Hellinger dintances
117+
# together with some monotonicity under tensor products
118+
# [see arXiv:1611.03449 (2017); section 4.2]
119+
ρA = rand_dm(5)
120+
ρB = rand_dm(6)
121+
ρAB = tensor(ρA, ρB)
122+
σA = rand_dm(5)
123+
σB = rand_dm(6)
124+
σAB = tensor(σA, σB)
125+
d_Bu_A = bures_dist(ρA, σA)
126+
d_Bu_AB = bures_dist(ρAB, σAB)
127+
d_He_A = hellinger_dist(ρA, σA)
128+
d_He_AB = hellinger_dist(ρAB, σAB)
129+
@test isapprox(fidelity(ρAB, σAB), fidelity(ρA, σA) * fidelity(ρB, σB); atol = 1e-6)
130+
@test d_He_AB >= d_Bu_AB
131+
@test d_Bu_AB >= d_Bu_A
132+
@test isapprox(bures_dist(ρAB, tensor(σA, ρB)), d_Bu_A; atol = 1e-6)
133+
@test d_He_AB >= d_He_A
134+
@test isapprox(hellinger_dist(ρAB, tensor(σA, ρB)), d_He_A; atol = 1e-6)
95135

96136
@testset "Type Inference (fidelity)" begin
97137
@inferred fidelity(M0, M1)
98138
@inferred fidelity(ψ1, M0)
99139
@inferred fidelity(ψ1, ψ2)
100140
end
141+
142+
@testset "Type Inference (Hellinger distance)" begin
143+
@inferred hellinger_dist(M0, M1)
144+
@inferred hellinger_dist(ψ1, M0)
145+
@inferred hellinger_dist(ψ1, ψ2)
146+
end
147+
148+
@testset "Type Inference (Bures metric)" begin
149+
@inferred bures_dist(M0, M1)
150+
@inferred bures_dist(ψ1, M0)
151+
@inferred bures_dist(ψ1, ψ2)
152+
@inferred bures_angle(M0, M1)
153+
@inferred bures_angle(ψ1, M0)
154+
@inferred bures_angle(ψ1, ψ2)
155+
end
101156
end

0 commit comments

Comments
 (0)