From 3bdc42c3a72c64bc342f02f6979ecb4f330d1d99 Mon Sep 17 00:00:00 2001 From: Jessica Chen Date: Sat, 23 Apr 2022 22:49:25 +0800 Subject: [PATCH 1/5] Implement value test for pdf --- Project.toml | 5 ++--- test/pdf.jl | 44 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 21b819b..1217193 100644 --- a/Project.toml +++ b/Project.toml @@ -19,10 +19,9 @@ QuantumStateBase = "1.2" julia = "1.6" [extras] +LsqFit = "2fda8390-95c7-5789-9bda-21331edee243" QuantumStateBase = "73ce9c4f-35d1-4161-b9e6-26915895bfed" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b" [targets] -test = ["Test", "QuantumStateBase", "KernelDensity"] - +test = ["Test", "QuantumStateBase", "LsqFit"] diff --git a/test/pdf.jl b/test/pdf.jl index 52e7401..e3e0544 100644 --- a/test/pdf.jl +++ b/test/pdf.jl @@ -1,14 +1,44 @@ +using LsqFit + @testset "qpdf" begin r, θ, dim = 0.8, 2π, 100 ρ = SqueezedState(r, θ, Matrix, dim=dim) d = GaussianStateBHD(ρ) + xs = LinRange(-3, 3, 1000) + θs = LinRange(0, 2π, 10) + g_pdf = zeros(1000) + g_pdfs = zeros(10, 1000) + + for i in 1:1000 + g_pdf[i] = qpdf(d, π/3, xs[i]) + end + + g_pdfs = qpdf(d, θs, xs) + + param = [QuantumStateDistributions.mean(d, π/3), QuantumStateDistributions.std(d, π/3)] + @. model(x, p) = 1 / (p[2] * √(2π)) * exp(-(x - p[1])^2 / (2 * p[2]^2)) + p0 = [0.5, 0.5] + + g_fit = curve_fit(model, xs, g_pdf, p0) + @test coef(g_fit) ≈ param + + for i in 1:10 + g_fits = curve_fit(model, xs, g_pdfs[i, :], p0) + params = [QuantumStateDistributions.mean(d, θs[i]), QuantumStateDistributions.std(d, θs[i])] + + @test coef(g_fits) ≈ params + end + + q = QuantumStateBHD(ρ) + q_pdf = zeros(1000) + q_pdfs = zeros(10, 1000) + + for i in 1:1000 + q_pdf[i] = qpdf(q, π/3, xs[i]) + end - θs = LinRange(0, 2π, 101) - xs = LinRange(-3, 3, 101) + q_pdfs = qpdf(q, θs, xs) - @test size(qpdf(d, θs, xs)) == (101, 101) - @test length(qpdf(d, π/2, 1)) == 1 - @test size(qpdf(Float32, d, θs, xs)) == (101, 101) - @test length(qpdf(Float32, d ,π/3, 2)) == 1 - @test size(qpdf(d, LinRange(0, 2π, 10), LinRange(-3, 3, 10))) == (10, 10) + @test q_pdf ≈ g_pdf + @test q_pdfs ≈ g_pdfs end From ca1b333ec9dc0dd3ae02b2905844979095b6184a Mon Sep 17 00:00:00 2001 From: Jessica Chen Date: Sat, 23 Apr 2022 22:58:10 +0800 Subject: [PATCH 2/5] Fix methods conflect --- src/pdf.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pdf.jl b/src/pdf.jl index a00fe4c..191c8da 100644 --- a/src/pdf.jl +++ b/src/pdf.jl @@ -55,9 +55,9 @@ Quadrature prabability in intensity-to-measurement-phase quadrature coordinate. ``p(\\rho, \\theta, x) = tr(\\hat{\\Pi}(\\theta, x) \\rho)`` """ -qpdf(ρ, θ, x) = qpdf(Float64, ρ, θ, x) +qpdf(ρ::AbstractArray, θ, x) = qpdf(Float64, ρ, θ, x) -function qpdf(T::Type{<:Real}, ρ, θ::Real, x::Real) +function qpdf(T::Type{<:Real}, ρ::AbstractArray, θ::Real, x::Real) dim = size(ρ, 1) 𝛑̂_res = Matrix{Complex{T}}(undef, dim, dim) @@ -70,7 +70,7 @@ function qpdf!(𝛑̂_res::AbstractMatrix, ρ::AbstractArray, θ::Real, x::Real) return real_tr_mul(𝛑̂!(𝛑̂_res, θ, x, dim=dim), ρ) end -function qpdf(T::Type{<:Real}, ρ, θs::AbstractRange, xs::AbstractRange) +function qpdf(T::Type{<:Real}, ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange) dim = size(ρ, 1) 𝛑̂_res_vec = [Matrix{Complex{T}}(undef, dim, dim) for _ in 1:Threads.nthreads()] 𝐩 = Matrix{T}(undef, length(θs), length(xs)) From e6d3f39fef659c93cfefb99823877b7a2d5e1e04 Mon Sep 17 00:00:00 2001 From: Jessica Chen Date: Mon, 25 Apr 2022 11:41:53 +0800 Subject: [PATCH 3/5] Refactor Trim API for pdf. --- src/pdf.jl | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/pdf.jl b/src/pdf.jl index 191c8da..dacb2bc 100644 --- a/src/pdf.jl +++ b/src/pdf.jl @@ -7,10 +7,14 @@ export qpdf([T=Float64], d::QuantumStateBHD, θs::AbstractRange, xs::AbstractRange) qpdf([T=Float64], d::GaussianStateBHD, θ::Real, x::Real) qpdf([T=Float64], d::GaussianStateBHD, θs::AbstractRange, xs::AbstractRange) + qpdf([T=Float64], ρ::AbstractArray, θ::Real, x::Real) + qpdf([T=Float64], ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange) + +Quadrature prabability in intensity-to-measurement-phase quadrature coordinate. -Quadrature prabability at point (θ, x) or points (θs, xs) +``p(\\rho, \\theta, x) = tr(\\hat{\\Pi}(\\theta, x) \\rho)`` """ -qpdf(d, θ, x) = qpdf(Float64, d, θ, x) +qpdf(state, θ, x) = qpdf(Float64, state, θ, x) function qpdf(T::Type{<:Real}, d::GaussianStateBHD, θ::Real, x::Real) μ = QuantumStateDistributions.mean(d, θ) @@ -47,16 +51,6 @@ end qpdf(T::Type{<:Real}, d::QuantumStateBHD, θ, x) = qpdf(T, d.ρ, θ, x) -""" - qpdf([T=Float64], ρ::AbstractArray, θ::Real, x::Real) - qpdf([T=Float64], ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange) - -Quadrature prabability in intensity-to-measurement-phase quadrature coordinate. - -``p(\\rho, \\theta, x) = tr(\\hat{\\Pi}(\\theta, x) \\rho)`` -""" -qpdf(ρ::AbstractArray, θ, x) = qpdf(Float64, ρ, θ, x) - function qpdf(T::Type{<:Real}, ρ::AbstractArray, θ::Real, x::Real) dim = size(ρ, 1) 𝛑̂_res = Matrix{Complex{T}}(undef, dim, dim) From fb4d13dffa0511ceadeb8e78c930c32d097aee4f Mon Sep 17 00:00:00 2001 From: Jessica Chen Date: Mon, 25 Apr 2022 12:03:43 +0800 Subject: [PATCH 4/5] Enhance doc string --- src/pdf.jl | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/pdf.jl b/src/pdf.jl index dacb2bc..e5ac5d2 100644 --- a/src/pdf.jl +++ b/src/pdf.jl @@ -3,15 +3,22 @@ export qpdf! """ - qpdf([T=Float64], d::QuantumStateBHD, θ::Real, x::Real) - qpdf([T=Float64], d::QuantumStateBHD, θs::AbstractRange, xs::AbstractRange) qpdf([T=Float64], d::GaussianStateBHD, θ::Real, x::Real) qpdf([T=Float64], d::GaussianStateBHD, θs::AbstractRange, xs::AbstractRange) + qpdf([T=Float64], d::QuantumStateBHD, θ::Real, x::Real) + qpdf([T=Float64], d::QuantumStateBHD, θs::AbstractRange, xs::AbstractRange) qpdf([T=Float64], ρ::AbstractArray, θ::Real, x::Real) qpdf([T=Float64], ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange) Quadrature prabability in intensity-to-measurement-phase quadrature coordinate. +## Arguments + +* `T`: Data type, default as Float64. +* `state`: State can be `GaussianStateBHD` distribution, `QuantumStateBHD` distribution or density matrix. +* `θ`: Measurement phase, can be `Real` or `AbstractRange`. +* `x`: Intensity in quadrature coordinate, can be `Real` or `AbstractRange`. + ``p(\\rho, \\theta, x) = tr(\\hat{\\Pi}(\\theta, x) \\rho)`` """ qpdf(state, θ, x) = qpdf(Float64, state, θ, x) @@ -51,20 +58,20 @@ end qpdf(T::Type{<:Real}, d::QuantumStateBHD, θ, x) = qpdf(T, d.ρ, θ, x) -function qpdf(T::Type{<:Real}, ρ::AbstractArray, θ::Real, x::Real) +function qpdf(T::Type{<:Real}, ρ::AbstractMatrix, θ::Real, x::Real) dim = size(ρ, 1) 𝛑̂_res = Matrix{Complex{T}}(undef, dim, dim) return qpdf!(𝛑̂_res, ρ, θ, x) end -function qpdf!(𝛑̂_res::AbstractMatrix, ρ::AbstractArray, θ::Real, x::Real) +function qpdf!(𝛑̂_res::AbstractMatrix, ρ::AbstractMatrix, θ::Real, x::Real) dim = size(ρ, 1) return real_tr_mul(𝛑̂!(𝛑̂_res, θ, x, dim=dim), ρ) end -function qpdf(T::Type{<:Real}, ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange) +function qpdf(T::Type{<:Real}, ρ::AbstractMatrix, θs::AbstractRange, xs::AbstractRange) dim = size(ρ, 1) 𝛑̂_res_vec = [Matrix{Complex{T}}(undef, dim, dim) for _ in 1:Threads.nthreads()] 𝐩 = Matrix{T}(undef, length(θs), length(xs)) @@ -74,7 +81,7 @@ end function qpdf!( 𝛑̂_res_vec::AbstractVector{Matrix{Complex{T}}}, 𝐩::Matrix{T}, - ρ::AbstractArray, θs::AbstractRange, xs::AbstractRange + ρ::AbstractMatrix, θs::AbstractRange, xs::AbstractRange ) where {T} @sync for (j, x) in enumerate(xs) for (i, θ) in enumerate(θs) From 6495154001b617a1ba8a4491e07069a0dbc61299 Mon Sep 17 00:00:00 2001 From: Jessica Chen Date: Mon, 25 Apr 2022 14:13:30 +0800 Subject: [PATCH 5/5] Refactor --- test/pdf.jl | 89 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/test/pdf.jl b/test/pdf.jl index e3e0544..79a1e7a 100644 --- a/test/pdf.jl +++ b/test/pdf.jl @@ -1,44 +1,101 @@ using LsqFit -@testset "qpdf" begin +@testset "qpdf for GaussianStateBHD" begin r, θ, dim = 0.8, 2π, 100 ρ = SqueezedState(r, θ, Matrix, dim=dim) d = GaussianStateBHD(ρ) - xs = LinRange(-3, 3, 1000) - θs = LinRange(0, 2π, 10) - g_pdf = zeros(1000) - g_pdfs = zeros(10, 1000) - for i in 1:1000 + m = 10 + n = 1000 + θs = LinRange(0, 2π, m) + xs = LinRange(-3, 3, n) + + g_pdf = Vector{Float64}(undef, n) + + for i in 1:n g_pdf[i] = qpdf(d, π/3, xs[i]) end g_pdfs = qpdf(d, θs, xs) - param = [QuantumStateDistributions.mean(d, π/3), QuantumStateDistributions.std(d, π/3)] + moments = [QuantumStateDistributions.mean(d, π/3), QuantumStateDistributions.std(d, π/3)] @. model(x, p) = 1 / (p[2] * √(2π)) * exp(-(x - p[1])^2 / (2 * p[2]^2)) p0 = [0.5, 0.5] g_fit = curve_fit(model, xs, g_pdf, p0) - @test coef(g_fit) ≈ param + @test coef(g_fit) ≈ moments - for i in 1:10 + for i in 1:m g_fits = curve_fit(model, xs, g_pdfs[i, :], p0) - params = [QuantumStateDistributions.mean(d, θs[i]), QuantumStateDistributions.std(d, θs[i])] + moments = [QuantumStateDistributions.mean(d, θs[i]), QuantumStateDistributions.std(d, θs[i])] - @test coef(g_fits) ≈ params + @test coef(g_fits) ≈ moments end +end +@testset "qpdf for QuantumStateBHD" begin + r, θ, dim = 0.8, 2π, 100 + ρ = SqueezedState(r, θ, Matrix, dim=dim) q = QuantumStateBHD(ρ) - q_pdf = zeros(1000) - q_pdfs = zeros(10, 1000) + d = GaussianStateBHD(ρ) + + m = 10 + n = 1000 + θs = LinRange(0, 2π, m) + xs = LinRange(-3, 3, n) + + q_pdf = Vector{Float64}(undef, n) - for i in 1:1000 + for i in 1:n q_pdf[i] = qpdf(q, π/3, xs[i]) end q_pdfs = qpdf(q, θs, xs) - @test q_pdf ≈ g_pdf - @test q_pdfs ≈ g_pdfs + moments = [QuantumStateDistributions.mean(d, π/3), QuantumStateDistributions.std(d, π/3)] + @. model(x, p) = 1 / (p[2] * √(2π)) * exp(-(x - p[1])^2 / (2 * p[2]^2)) + p0 = [0.5, 0.5] + + q_fit = curve_fit(model, xs, q_pdf, p0) + @test coef(q_fit) ≈ moments + + for i in 1:m + q_fits = curve_fit(model, xs, q_pdfs[i, :], p0) + moments = [QuantumStateDistributions.mean(d, θs[i]), QuantumStateDistributions.std(d, θs[i])] + + @test coef(q_fits) ≈ moments + end +end + +@testset "pdf for density matrix" begin + r, θ, dim = 0.8, 2π, 100 + ρ = SqueezedState(r, θ, Matrix, dim=dim) + d = GaussianStateBHD(ρ) + + m = 10 + n = 1000 + θs = LinRange(0, 2π, m) + xs = LinRange(-3, 3, n) + + ρ_pdf = Vector{Float64}(undef, n) + + for i in 1:n + ρ_pdf[i] = qpdf(ρ, π/3, xs[i]) + end + + ρ_pdfs = qpdf(ρ, θs, xs) + + moments = [QuantumStateDistributions.mean(d, π/3), QuantumStateDistributions.std(d, π/3)] + @. model(x, p) = 1 / (p[2] * √(2π)) * exp(-(x - p[1])^2 / (2 * p[2]^2)) + p0 = [0.5, 0.5] + + ρ_fit = curve_fit(model, xs, ρ_pdf, p0) + @test coef(ρ_fit) ≈ moments + + for i in 1:m + ρ_fits = curve_fit(model, xs, ρ_pdfs[i, :], p0) + moments = [QuantumStateDistributions.mean(d, θs[i]), QuantumStateDistributions.std(d, θs[i])] + + @test coef(ρ_fits) ≈ moments + end end