From a5bb06ddac9bb3f02c5b865011d84897321c1c5d Mon Sep 17 00:00:00 2001 From: araujoms Date: Fri, 6 Dec 2024 22:58:00 +0100 Subject: [PATCH] fix tests --- test/random.jl | 92 +++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 58 deletions(-) diff --git a/test/random.jl b/test/random.jl index 3059433..17f87bb 100644 --- a/test/random.jl +++ b/test/random.jl @@ -5,49 +5,34 @@ ψ = random_state_ket(3) @test isa(ψ, Vector{ComplexF64}) for R in [Float64, Double64, Float128, BigFloat] - Random.seed!(1337) #make ranks behave - ψ = random_state_ket(R, 3) - @test ψ' * ψ ≈ 1 - @test isa(ψ, Vector{R}) - ρ = random_state(R, 3) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 3 - ρ = random_state(R, 3, 2) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 2 - ρ = random_state(R, 3, 1) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 1 - @test minimum(eigvals(ρ)) > -Ket._rtol(R) - @test isa(ρ, Hermitian{R}) - T = Complex{R} - ψ = random_state_ket(T, 3) - @test ψ' * ψ ≈ 1 - @test isa(ψ, Vector{T}) - ρ = random_state(T, 3) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 3 - ρ = random_state(T, 3, 2) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 2 - ρ = random_state(T, 3, 1) - @test tr(ρ) ≈ 1 - @test rank(ρ; rtol = Ket._rtol(R)) == 1 - @test minimum(eigvals(ρ)) > -Ket._rtol(R) - @test isa(ρ, Hermitian{T}) + for f in (identity, complex) + Random.seed!(1337) #make ranks behave + ψ = random_state_ket(f(R), 3) + @test ψ' * ψ ≈ 1 + @test isa(ψ, Vector{f(R)}) + ρ = random_state(f(R), 3) + @test tr(ρ) ≈ 1 + @test rank(ρ; rtol = Ket._rtol(f(R))) == 3 + ρ = random_state(f(R), 3, 2) + @test tr(ρ) ≈ 1 + @test rank(ρ; rtol = Ket._rtol(f(R))) == 2 + ρ = random_state(f(R), 3, 1) + @test tr(ρ) ≈ 1 + @test rank(ρ; rtol = Ket._rtol(f(R))) == 1 + @test minimum(eigvals(ρ)) > -Ket._rtol(f(R)) + @test isa(ρ, Hermitian{f(R)}) + end end end @testset "Unitaries" begin U = random_unitary(3) - @test isa(U, Matrix{ComplexF64}) + @test eltype(U) <: ComplexF64 for R in [Float64, Double64, Float128, BigFloat] - U = random_unitary(R, 3) - @test U * U' ≈ I(3) - @test isa(U, Matrix{R}) - T = Complex{R} - U = random_unitary(T, 3) - @test U * U' ≈ I(3) - @test isa(U, Matrix{T}) + for f in (identity, complex) + U = random_unitary(f(R), 3) + @test U * U' ≈ I(3) + @test eltype(U) <: f(R) + end end end @testset "Probability" begin @@ -62,26 +47,17 @@ end @testset "POVM" begin for R in [Float64, Double64, Float128, BigFloat] - E = random_povm(R, 2, 3) - @test test_povm(E) - for i in 1:length(E) - @test rank(E[i]; rtol = Ket._rtol(R)) == 2 - end - E = random_povm(R, 2, 3, 1) - @test test_povm(E) - for i in 1:length(E) - @test rank(E[i]; rtol = Ket._rtol(R)) == 1 - end - T = Complex{R} - E = random_povm(T, 2, 3) - @test test_povm(E) - for i in 1:length(E) - @test rank(E[i]; rtol = Ket._rtol(R)) == 2 - end - E = random_povm(T, 2, 3, 1) - @test test_povm(E) - for i in 1:length(E) - @test rank(E[i]; rtol = Ket._rtol(R)) == 1 + for f in (identity, complex) + E = random_povm(f(R), 2, 3) + @test test_povm(E) + for i in 1:length(E) + @test rank(E[i]; rtol = Ket._rtol(f(R))) == 2 + end + E = random_povm(f(R), 2, 3, 1) + @test test_povm(E) + for i in 1:length(E) + @test rank(E[i]; rtol = Ket._rtol(f(R))) == 1 + end end end end