Skip to content

Commit 38a473c

Browse files
ytdHuangLorenzoFioronialbertomercurio
authored
Introduce visualization (#347)
* Add Wigner plotting via CairoMakie * CairoMakie default lib * docs * formatting * Fix typo * Add error handling for unavailable plotting libraries in plot_wigner function * Docs * Update CairoMakie dependency and add CairoMakie extension tests * Update documentation to reflect changes in Wigner function plotting and computation * formatting * Update changelog * Improve docs * Update CairoMakie imports and change axis labels * fix CHANGELOG * setup CI pipeline config for `CairoMakie` extension * rebase accidently removed lines * fix typo * add `CairoMakie` extension doc page * fix typo * minor change in docs * minor change in docs --------- Co-authored-by: Lorenzo Fioroni <lorenzo.fioroni@epfl.ch> Co-authored-by: Alberto Mercurio <alberto.mercurio96@gmail.com>
1 parent e754148 commit 38a473c

File tree

12 files changed

+385
-21
lines changed

12 files changed

+385
-21
lines changed

.github/workflows/CI.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- 'ext/**'
1111
- 'test/runtests.jl'
1212
- 'test/core-test/**'
13+
- 'test/ext-test/cairomakie_ext.jl'
1314
- 'Project.toml'
1415
pull_request:
1516
branches:
@@ -20,6 +21,7 @@ on:
2021
- 'ext/**'
2122
- 'test/runtests.jl'
2223
- 'test/core-test/**'
24+
- 'test/ext-test/cairomakie_ext.jl'
2325
- 'Project.toml'
2426
types:
2527
- opened
@@ -52,14 +54,21 @@ jobs:
5254
group:
5355
- 'Core'
5456

55-
# include:
57+
include:
5658
# for core tests (intermediate versions)
5759
# - version: '1.x'
5860
# node:
5961
# os: 'ubuntu-latest'
6062
# arch: 'x64'
6163
# group: 'Core'
6264

65+
# for extension tests
66+
- version: '1'
67+
node:
68+
os: 'ubuntu-latest'
69+
arch: 'x64'
70+
group: 'CairoMakie_Ext'
71+
6372
steps:
6473
- uses: actions/checkout@v4
6574
- uses: julia-actions/setup-julia@v2

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Improve the construction of `QobjEvo`. ([#338], [#339])
1111
- Support `Base.zero` and `Base.one` for `AbstractQuantumObject`. ([#342], [#346])
12+
- Introduce visualization and function `plot_wigner` for easy plotting of Wigner functions. ([#86], [#292], [#347])
1213

1314
## [v0.23.1]
1415
Release date: 2024-12-06
@@ -45,7 +46,9 @@ Release date: 2024-11-13
4546
[v0.22.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.22.0
4647
[v0.23.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.23.0
4748
[v0.23.1]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.23.1
49+
[#86]: https://github.com/qutip/QuantumToolbox.jl/issues/86
4850
[#139]: https://github.com/qutip/QuantumToolbox.jl/issues/139
51+
[#292]: https://github.com/qutip/QuantumToolbox.jl/issues/292
4952
[#306]: https://github.com/qutip/QuantumToolbox.jl/issues/306
5053
[#309]: https://github.com/qutip/QuantumToolbox.jl/issues/309
5154
[#311]: https://github.com/qutip/QuantumToolbox.jl/issues/311
@@ -58,3 +61,4 @@ Release date: 2024-11-13
5861
[#339]: https://github.com/qutip/QuantumToolbox.jl/issues/339
5962
[#342]: https://github.com/qutip/QuantumToolbox.jl/issues/342
6063
[#346]: https://github.com/qutip/QuantumToolbox.jl/issues/346
64+
[#347]: https://github.com/qutip/QuantumToolbox.jl/issues/347

Project.toml

Lines changed: 5 additions & 1 deletion
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"
3942
DiffEqBase = "6"
4043
DiffEqCallbacks = "4.2.1 - 4"
4144
DiffEqNoiseProcess = "5"
@@ -62,8 +65,9 @@ julia = "1.10"
6265

6366
[extras]
6467
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
68+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
6569
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
6670
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
6771

6872
[targets]
69-
test = ["Aqua", "JET", "Test"]
73+
test = ["Aqua", "CairoMakie", "JET", "Test"]

docs/make.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ using DocumenterVitepress
77
using DocumenterCitations
88
using Changelog
99

10+
# Load of packages required to compile the extension documentation
11+
using CairoMakie
12+
1013
DocMeta.setdocmeta!(QuantumToolbox, :DocTestSetup, :(using QuantumToolbox); recursive = true)
1114

1215
# some options for `makedocs`
@@ -56,6 +59,7 @@ const PAGES = [
5659
"Two-time correlation functions" => "users_guide/two_time_corr_func.md",
5760
"Extensions" => [
5861
"users_guide/extensions/cuda.md",
62+
"users_guide/extensions/cairomakie.md",
5963
],
6064
],
6165
"Tutorials" => [
@@ -76,7 +80,10 @@ const PAGES = [
7680
]
7781

7882
makedocs(;
79-
modules = [QuantumToolbox],
83+
modules = [
84+
QuantumToolbox,
85+
Base.get_extension(QuantumToolbox, :QuantumToolboxCairoMakieExt),
86+
],
8087
authors = "Alberto Mercurio and Yi-Te Huang",
8188
repo = Remotes.GitHub("qutip", "QuantumToolbox.jl"),
8289
sitename = "QuantumToolbox.jl",

docs/src/resources/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,9 @@ convert_unit
283283
row_major_reshape
284284
meshgrid
285285
```
286+
287+
## [Visualization](@id doc-API:Visualization)
288+
289+
```@docs
290+
plot_wigner
291+
```

docs/src/tutorials/logo.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,16 @@ Next, we construct the triangular cat state as a normalized superposition of thr
6767
normalize!(ψ)
6868
```
6969

70-
### Defining the Grid and calculating the Wigner function
70+
### Defining the Grid and plotting the Wigner function
7171

72-
We define the grid for the Wigner function and calculate it using the [`wigner`](@ref) function. We shift the grid in the imaginary direction to ensure that the Wigner function is centered around the origin of the figure. The [`wigner`](@ref) function also supports the `g` scaling factor, which we put here equal to ``2``.
72+
We define the grid for the Wigner function and plot it using the [`plot_wigner`](@ref) function. This, internally calls the [`wigner`](@ref) function for the computation. We shift the grid in the imaginary direction to ensure that the Wigner function is centered around the origin of the figure. The [`wigner`](@ref) function also supports the `g` scaling factor, which we put here equal to ``2``.
7373

7474
```@example logo
7575
xvec = range(-ρ, ρ, 500) .* 1.5
7676
yvec = xvec .+ (abs(imag(α1)) - abs(imag(α2))) / 2
7777
78-
wig = wigner(ψ, xvec, yvec, g = 2)
79-
```
80-
81-
### Plotting the Wigner function
82-
83-
Finally, we plot the Wigner function using the `heatmap` function from the `CairoMakie` package.
84-
85-
```@example logo
8678
fig = Figure(size = (250, 250), figure_padding = 0)
87-
ax = Axis(fig[1, 1])
88-
heatmap!(ax, xvec, yvec, wig', colormap = :RdBu, interpolate = true, rasterize = 1)
79+
fig, ax, hm = plot_wigner(ψ, xvec = xvec, yvec = yvec, g = 2, library = Val(:CairoMakie), location = fig[1,1])
8980
hidespines!(ax)
9081
hidexdecorations!(ax)
9182
hideydecorations!(ax)
@@ -118,12 +109,8 @@ nothing # hide
118109
And the Wigner function becomes more uniform:
119110

120111
```@example logo
121-
wig = wigner(sol.states[end], xvec, yvec, g = 2)
122-
123112
fig = Figure(size = (250, 250), figure_padding = 0)
124-
ax = Axis(fig[1, 1])
125-
126-
img_wig = heatmap!(ax, xvec, yvec, wig', colormap = :RdBu, interpolate = true, rasterize = 1)
113+
fig, ax, hm = plot_wigner(sol.states[end], xvec = xvec, yvec = yvec, g = 2, library = Val(:CairoMakie), location = fig[1,1])
127114
hidespines!(ax)
128115
hidexdecorations!(ax)
129116
hideydecorations!(ax)
@@ -135,7 +122,7 @@ At this stage, we have finished to use the `QuantumToolbox` package. From now on
135122

136123
### Custom Colormap
137124

138-
We define a custom colormap that changes depending on the Wigner function and spatial coordinates. Indeed, we want the three different colormaps, in the regions corresponding to the three coherent states, to match the colors of the Julia logo. We also want the colormap change to be smooth, so we use a Gaussian function to blend the colors. We introduce also a Wigner function dependent transparency to make the logo more appealing.
125+
We define a custom colormap that changes depending on the Wigner function and spatial coordinates. Indeed, we want the three different colormaps, in the regions corresponding to the three coherent states, to match the colors of the Julia logo. We also want the colormap change to be smooth, so we use a Gaussian function to blend the colors. We introduce also a Wigner function dependent transparency to make the logo more appealing. In order to do so, we are going to need the value of the wigner function at each point of the grid, rather than its plot. We will thus call the [`wigner`](@ref) function directly.
139126

140127
```@example logo
141128
function set_color_julia(x, y, wig::T, α1, α2, α3, cmap1, cmap2, cmap3, δ) where {T}
@@ -156,6 +143,7 @@ function set_color_julia(x, y, wig::T, α1, α2, α3, cmap1, cmap2, cmap3, δ) w
156143
return RGBAf(c_tot.r, c_tot.g, c_tot.b, alpha)
157144
end
158145
146+
wig = wigner(sol.states[end], xvec, yvec, g = 2)
159147
X, Y = meshgrid(xvec, yvec)
160148
δ = 1.25 # Smoothing parameter for the Gaussian functions
161149
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# [Extension for CairoMakie.jl](@id doc:CairoMakie)
2+
3+
This is an extension to support visualization (plotting functions) using [`CairoMakie.jl`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie) library.
4+
5+
This extension will be automatically loaded if user imports both `QuantumToolbox.jl` and [`CairoMakie.jl`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie):
6+
7+
```julia
8+
using QuantumToolbox
9+
using CairoMakie
10+
```
11+
12+
To plot with [`CairoMakie.jl`](https://github.com/MakieOrg/Makie.jl/tree/master/CairoMakie) library, specify the keyword argument `library = Val(:CairoMakie)` for the plotting functions.
13+
14+
!!! warning "Beware of type-stability!"
15+
If you want to keep type stability, it is recommended to use `Val(:CairoMakie)` instead of `: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.
16+
17+
The supported plotting functions are listed as follows:
18+
19+
| **Plotting Function** | **Description** |
20+
|:----------------------|:----------------|
21+
| [`plot_wigner`](@ref) | [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wigner_quasiprobability_distribution) |

0 commit comments

Comments
 (0)