|
1 | 1 | module QuantumToolboxCairoMakieExt |
2 | 2 |
|
3 | 3 | 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! |
5 | 6 |
|
6 | 7 | @doc raw""" |
7 | 8 | plot_wigner( |
@@ -139,6 +140,84 @@ function _plot_wigner( |
139 | 140 | return fig, ax, surf |
140 | 141 | end |
141 | 142 |
|
| 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 | + |
142 | 221 | raw""" |
143 | 222 | _getFigAndLocation(location::Nothing) |
144 | 223 | |
|
0 commit comments