Skip to content

Commit 62937fd

Browse files
Add Wigner plotting via CairoMakie
1 parent c8c9479 commit 62937fd

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
2828

2929
[weakdeps]
3030
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
31+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
3132

3233
[extensions]
3334
QuantumToolboxCUDAExt = "CUDA"
35+
QuantumToolboxCairoMakieExt = "CairoMakie"
3436

3537
[compat]
3638
Aqua = "0.8"
3739
ArrayInterface = "6, 7"
3840
CUDA = "5"
41+
CairoMakie = "0.12.15"
3942
DiffEqBase = "6"
4043
DiffEqCallbacks = "2 - 3.1, 3.8, 4"
4144
DiffEqNoiseProcess = "5"

ext/QuantumToolboxCairoMakieExt.jl

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
module QuantumToolboxCairoMakieExt
2+
3+
using QuantumToolbox
4+
using CairoMakie
5+
6+
function QuantumToolbox.plot_wigner(
7+
library::Val{:CairoMakie},
8+
state::QuantumObject{<:AbstractArray{T},OpType},
9+
xvec::Union{Nothing,AbstractVector} = nothing,
10+
yvec::Union{Nothing,AbstractVector} = nothing;
11+
g::Real = 2,
12+
method::WignerSolver = WignerClenshaw(),
13+
projection::String = "2d",
14+
fig::Union{Figure,Nothing} = nothing,
15+
ax::Union{Axis,Nothing} = nothing,
16+
colorbar::Bool = false,
17+
kwargs...,
18+
) where {T,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
19+
projection == "2d" || projection == "3d" || throw(ArgumentError("Unsupported projection: $projection"))
20+
21+
return _plot_wigner(
22+
library,
23+
state,
24+
xvec,
25+
yvec,
26+
Val(Symbol(projection)),
27+
g,
28+
method,
29+
fig,
30+
ax,
31+
colorbar;
32+
kwargs...
33+
)
34+
end
35+
36+
function _plot_wigner(
37+
::Val{:CairoMakie},
38+
state::QuantumObject{<:AbstractArray{T},OpType},
39+
xvec::AbstractVector,
40+
yvec::AbstractVector,
41+
projection::Val{Symbol("2d")},
42+
g::Real,
43+
method::WignerSolver,
44+
fig::Union{Figure,Nothing},
45+
ax::Union{Axis,Nothing},
46+
colorbar::Bool;
47+
kwargs...,
48+
) where {T,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
49+
fig, ax = _getFigAx(fig, ax)
50+
51+
gridPos = _gridPosFromAx(ax)
52+
CairoMakie.delete!(ax)
53+
54+
lyt = GridLayout(gridPos)
55+
ax = Axis(lyt[1, 1])
56+
57+
wig = wigner(state, xvec, yvec; g = g, method = method)
58+
wlim = maximum(abs, wig)
59+
60+
kwargs = merge(Dict(:colormap => :RdBu, :colorrange => (-wlim, wlim)), kwargs)
61+
hm = heatmap!(ax, xvec, yvec, wig; kwargs...)
62+
63+
if colorbar
64+
Colorbar(lyt[1, 2], hm)
65+
end
66+
67+
ax.xlabel = L"\Re(\alpha)"
68+
ax.ylabel = L"\Im(\alpha)"
69+
return fig, ax, hm
70+
end
71+
72+
function _plot_wigner(
73+
::Val{:CairoMakie},
74+
state::QuantumObject{<:AbstractArray{T},OpType},
75+
xvec::AbstractVector,
76+
yvec::AbstractVector,
77+
projection::Val{Symbol("3d")},
78+
g::Real,
79+
method::WignerSolver,
80+
fig::Union{Figure,Nothing},
81+
ax::Union{Axis,Nothing},
82+
colorbar::Bool;
83+
kwargs...,
84+
) where {T,OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
85+
fig, ax = _getFigAx(fig, ax)
86+
87+
gridPos = _gridPosFromAx(ax)
88+
CairoMakie.delete!(ax)
89+
90+
lyt = GridLayout(gridPos)
91+
ax = Axis3(lyt[1, 1], azimuth = 1.775pi, elevation = pi / 16, protrusions = (30, 90, 30, 30), viewmode = :stretch)
92+
93+
wig = wigner(state, xvec, yvec; g = g, method = method)
94+
wlim = maximum(abs, wig)
95+
96+
kwargs = merge(Dict(:colormap => :RdBu, :colorrange => (-wlim, wlim)), kwargs)
97+
surf = surface!(ax, xvec, yvec, wig; kwargs...)
98+
99+
if colorbar
100+
Colorbar(lyt[1, 2], surf)
101+
end
102+
103+
ax.xlabel = L"\Re(\alpha)"
104+
ax.ylabel = L"\Im(\alpha)"
105+
ax.zlabel = "Wigner function"
106+
return fig, ax, surf
107+
end
108+
109+
_getFigAx(fig::Figure, ax::Axis) = fig, ax
110+
_getFigAx(fig::Figure, ::Nothing) = fig, Axis(fig[1, 1])
111+
_getFigAx(::Nothing, ax::Axis) = _figFromChildren(ax), ax
112+
function _getFigAx(::Nothing, ::Nothing)
113+
fig = Figure()
114+
ax = Axis(fig[1, 1])
115+
return fig, ax
116+
end
117+
118+
_figFromChildren(children) = _figFromChildren(children.parent)
119+
_figFromChildren(fig::Figure) = fig
120+
121+
function _gridPosFromAx(ax::Axis)
122+
content = CairoMakie.Makie.GridLayoutBase.gridcontent(ax)
123+
gl, sp, si = content.parent, content.span, content.side
124+
return GridPosition(gl, sp, si)
125+
end
126+
127+
end

src/QuantumToolbox.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ include("arnoldi.jl")
104104
include("metrics.jl")
105105
include("negativity.jl")
106106
include("steadystate.jl")
107+
include("visualization.jl")
107108

108109
# deprecated functions
109110
include("deprecated.jl")

src/visualization.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export plot_wigner
2+
3+
plot_wigner(library::Val{T}, args...; kwargs...) where {T} =
4+
throw(ArgumentError("Unsupported visualization library: $(getVal(library))"))
5+
plot_wigner(library::Symbol, args...; kwargs...) = plot_wigner(Val(library), args...; kwargs...)

0 commit comments

Comments
 (0)