Skip to content

Commit 0c928cb

Browse files
committed
add plot_fock_distribution to CairoMakie extension
1 parent 14c9e13 commit 0c928cb

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

docs/src/users_guide/extensions/cairomakie.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ The supported plotting functions are listed as follows:
1818

1919
| **Plotting Function** | **Description** |
2020
|:----------------------|:----------------|
21-
| [`plot_wigner`](@ref) | [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wigner_quasiprobability_distribution) |
21+
| [`plot_wigner`](@ref) | [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wigner_quasiprobability_distribution) |
22+
| [`plot_fock_distribution`](@ref) | [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution |

ext/QuantumToolboxCairoMakieExt.jl

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module QuantumToolboxCairoMakieExt
22

33
using QuantumToolbox
4-
using CairoMakie: Axis, Axis3, Colorbar, Figure, GridLayout, heatmap!, surface!, GridPosition, @L_str, Reverse
4+
using CairoMakie:
5+
Axis, Axis3, Colorbar, Figure, GridLayout, heatmap!, surface!, barplot!, GridPosition, @L_str, Reverse, ylims!
56

67
@doc raw"""
78
plot_wigner(
@@ -139,6 +140,84 @@ function _plot_wigner(
139140
return fig, ax, surf
140141
end
141142

143+
@doc raw"""
144+
plot_fock_distribution(
145+
library::Val{:CairoMakie},
146+
ρ::QuantumObject{SType};
147+
fock_numbers::Union{Nothing, AbstractVector} = nothing,
148+
unit_y_range::Bool = true,
149+
location::Union{GridPosition,Nothing} = nothing,
150+
kwargs...
151+
) where {SType<:Union{KetQuantumObject,OperatorQuantumObject}}
152+
153+
Plot the [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution of `ρ`.
154+
155+
# Arguments
156+
- `library::Val{:CairoMakie}`: The plotting library to use.
157+
- `ρ::QuantumObject`: The quantum state for which the Fock state distribution is to be plotted. It can be either a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
158+
- `location::Union{GridPosition,Nothing}`: The location of the plot in the layout. If `nothing`, the plot is created in a new figure. Default is `nothing`.
159+
- `fock_numbers::Union{Nothing, AbstractVector}`: list of x ticklabels to represent fock numbers, default is `nothing`.
160+
- `unit_y_range::Bool`: Set y-axis limits [0, 1] or not, default is `true`.
161+
- `kwargs...`: Additional keyword arguments to pass to the plotting function.
162+
163+
# Returns
164+
- `fig`: The figure object.
165+
- `ax`: The axis object.
166+
- `hm`: Either the heatmap or surface object, depending on the projection.
167+
168+
!!! note "Import library first"
169+
[`CairoMakie`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie) must first be imported before using this function.
170+
171+
!!! warning "Beware of type-stability!"
172+
If you want to keep type stability, it is recommended to use `Val(:two_dim)` and `Val(:three_dim)` instead of `:two_dim` and `:three_dim`, respectively. Also, specify the library as `Val(:CairoMakie)` See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
173+
"""
174+
function QuantumToolbox.plot_fock_distribution(
175+
library::Val{:CairoMakie},
176+
ρ::QuantumObject{SType};
177+
fock_numbers::Union{Nothing,AbstractVector} = nothing,
178+
unit_y_range::Bool = true,
179+
location::Union{GridPosition,Nothing} = nothing,
180+
kwargs...,
181+
) where {SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
182+
return _plot_fock_distribution(
183+
library,
184+
ρ;
185+
fock_numbers = fock_numbers,
186+
unit_y_range = unit_y_range,
187+
location = location,
188+
kwargs...,
189+
)
190+
end
191+
192+
function _plot_fock_distribution(
193+
::Val{:CairoMakie},
194+
ρ::QuantumObject{SType};
195+
fock_numbers::Union{Nothing,AbstractVector} = nothing,
196+
unit_y_range::Bool = true,
197+
location::Union{GridPosition,Nothing} = nothing,
198+
kwargs...,
199+
) where {SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
200+
ρ = ket2dm(ρ)
201+
D = prod.dims)
202+
(real(tr(ρ)) > 1) && (@warn "The input ρ should be normalized.")
203+
204+
xvec = 0:(D-1)
205+
isnothing(fock_numbers) && (fock_numbers = string.(collect(xvec)))
206+
207+
fig, location = _getFigAndLocation(location)
208+
lyt = GridLayout(location)
209+
ax = Axis(lyt[1, 1])
210+
211+
barplot!(ax, xvec, real(diag(ρ)); kwargs...)
212+
213+
ax.xticks = (xvec, fock_numbers)
214+
ax.xlabel = "Fock number"
215+
ax.ylabel = "Occupation probability"
216+
unit_y_range && ylims!(ax, 0, 1)
217+
218+
return fig, ax
219+
end
220+
142221
raw"""
143222
_getFigAndLocation(location::Nothing)
144223

src/visualization.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export plot_wigner
1+
export plot_wigner, plot_fock_distribution
22

33
@doc raw"""
44
plot_wigner(
@@ -35,3 +35,39 @@ plot_wigner(
3535
kwargs...,
3636
) where {T,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}} =
3737
throw(ArgumentError("The specified plotting library $T is not available. Try running `using $T` first."))
38+
39+
@doc raw"""
40+
plot_fock_distribution(
41+
ρ::QuantumObject{SType};
42+
library::Union{Val, Symbol} = Val(:CairoMakie),
43+
kwargs...
44+
) where {SType<:Union{KetQuantumObject,OperatorQuantumObject}}
45+
46+
Plot the [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution of `ρ`.
47+
48+
The `library` keyword argument specifies the plotting library to use, defaulting to [`CairoMakie`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie).
49+
50+
# Arguments
51+
- `ρ::QuantumObject`: The quantum state for which to plot the Fock state distribution.
52+
- `library::Union{Val,Symbol}`: The plotting library to use. Default is `Val(:CairoMakie)`.
53+
- `kwargs...`: Additional keyword arguments to pass to the plotting function. See the documentation for the specific plotting library for more information.
54+
55+
!!! note "Import library first"
56+
The plotting libraries must first be imported before using them with this function.
57+
58+
!!! warning "Beware of type-stability!"
59+
If you want to keep type stability, it is recommended to use `Val(:CairoMakie)` instead of `:CairoMakie` as the plotting library. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
60+
"""
61+
plot_fock_distribution(
62+
ρ::QuantumObject{SType};
63+
library::Union{Val,Symbol} = Val(:CairoMakie),
64+
kwargs...,
65+
) where {SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}} =
66+
plot_fock_distribution(makeVal(library), ρ; kwargs...)
67+
68+
plot_fock_distribution(
69+
::Val{T},
70+
ρ::QuantumObject{SType};
71+
kwargs...,
72+
) where {T,SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}} =
73+
throw(ArgumentError("The specified plotting library $T is not available. Try running `using $T` first."))

test/ext-test/cairomakie/cairomakie_ext.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
@test_throws ArgumentError plot_wigner(ψ; library = :CairoMakie, xvec = xvec, yvec = yvec)
77

8+
@test_throws ArgumentError plot_fock_distribution(ψ; library = :CairoMakie)
9+
810
using CairoMakie
911

1012
fig, ax, hm = plot_wigner(
@@ -60,4 +62,14 @@
6062
)
6163
@test fig1 === fig
6264
@test fig[2, 3].layout.content[1].content[1, 1].layout.content[1].content === ax
65+
66+
fig = Figure()
67+
pos = fig[2, 3]
68+
fig1, ax = plot_fock_distribution(ψ; library = Val(:CairoMakie), location = pos)
69+
@test fig1 === fig
70+
@test fig[2, 3].layout.content[1].content[1, 1].layout.content[1].content === ax
71+
72+
fig = Figure()
73+
pos = fig[2, 3]
74+
fig1, ax = @test_logs (:warn,) plot_fock_distribution* 2; library = Val(:CairoMakie), location = pos)
6375
end

0 commit comments

Comments
 (0)