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 "\R e(\a lpha)"
68+ ax. ylabel = L "\I m(\a lpha)"
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 "\R e(\a lpha)"
104+ ax. ylabel = L "\I m(\a lpha)"
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
0 commit comments