Makie bloch sphere extension #216
Conversation
Krastanov
left a comment
There was a problem hiding this comment.
Thanks, this is a good start!
Check the comments I left in.
A few other things:
- this new functionality needs tests (check PBCCompiler.jl for an example of how to structure the tests)
- and it needs docs
- for ease of review, it would be helpful to post in the comments what a rendering looks like so we can discuss it before a full in-depth review.
There was a problem hiding this comment.
You seem to have included some files from your local dev environment -- these should be removed
You should not include extensions manually. They are supposed to be loaded when the library is imported together with the weakdeps for the extension. Check out the details in the Pkg.jl manual. |
Krastanov
left a comment
There was a problem hiding this comment.
looks like it is on a good track
could you address comments, resolve the merge conflict, verify that tests actually pass in CI, and post a few example plots in the PR description so that we know how it is being rendered
| uuid = "4f57444f-1401-5e15-980d-4471b28d5678" | ||
| version = "0.5.9" | ||
|
|
||
There was a problem hiding this comment.
don't add "lint"
always check the file tab in the PR to look for unrelated changes that you have made by mistake
| FastExpm = "7868e603-8603-432e-a1a1-694bd70b01f2" | ||
| FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838" | ||
| FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" | ||
| GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" |
There was a problem hiding this comment.
you have added a new dependency -- this is supposed to be an extension
| error("Blochsphere visualization requires a Makie extension.\n" * | ||
| "Load QuantumOpticsBase together with CairoMakie or GLMakie.") |
There was a problem hiding this comment.
instead of creating a method for a function, just declare a function without any methods
function blochsphere end
| ChiMatrix, DenseChiMatrix, avg_gate_fidelity, | ||
| SumBasis, directsum, ⊕, LazyDirectSum, getblock, setblock!, | ||
| qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2 | ||
| qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2, blochsphere |
There was a problem hiding this comment.
if you are exporting a function, it should be documented
also, notice that the exports are organized, so maybe add a comment header for this new category of functions, e.g. visualizations
| #apply | ||
| apply! | ||
|
|
||
| ########### Bloch Sphere API Stub ########### |
There was a problem hiding this comment.
do not make up new organization style comments
put this is a new file (notice that the exports above are organized by file) and include the file at the bottom
| @testset "|0⟩ renders without error" begin | ||
| fig = blochsphere(spinup(b)) | ||
| save("test_bloch_0.png", fig) | ||
| @test isfile("test_bloch_0.png") | ||
| rm("test_bloch_0.png") | ||
| end | ||
|
|
||
| @testset "|1⟩ renders without error" begin | ||
| fig = blochsphere(spindown(b)) | ||
| save("test_bloch_1.png", fig) | ||
| @test isfile("test_bloch_1.png") | ||
| rm("test_bloch_1.png") | ||
| end | ||
|
|
||
| @testset "|+⟩ renders without error" begin | ||
| fig = blochsphere(normalize(spinup(b) + spindown(b))) | ||
| save("test_bloch_plus.png", fig) | ||
| @test isfile("test_bloch_plus.png") | ||
| rm("test_bloch_plus.png") | ||
| end | ||
|
|
||
| @testset "|-⟩ renders without error" begin | ||
| fig = blochsphere(normalize(spinup(b) - spindown(b))) | ||
| save("test_bloch_minus.png", fig) | ||
| @test isfile("test_bloch_minus.png") | ||
| rm("test_bloch_minus.png") | ||
| end | ||
|
|
||
| @testset "|+i⟩ renders without error" begin | ||
| fig = blochsphere(normalize(spinup(b) + im*spindown(b))) | ||
| save("test_bloch_plusi.png", fig) | ||
| @test isfile("test_bloch_plusi.png") | ||
| rm("test_bloch_plusi.png") | ||
| end | ||
|
|
||
| @testset "|-i⟩ renders without error" begin | ||
| fig = blochsphere(normalize(spinup(b) - im*spindown(b))) | ||
| save("test_bloch_minusi.png", fig) | ||
| @test isfile("test_bloch_minusi.png") | ||
| rm("test_bloch_minusi.png") | ||
| end |
There was a problem hiding this comment.
just pick one random arbitrary unnamed state and test for that -- these repeated tests do not really add much value to the test runner
| lim = Float32(get(kwargs, :lim, 1.6)) | ||
| Makie.limits!(ax, -lim, lim, -lim, lim, -lim, lim) | ||
| blochsphereplot!(ax, state; kwargs...) | ||
| return fig |
There was a problem hiding this comment.
return also the axis and the plot object
| xzpanelvisible = false, | ||
| yzpanelvisible = false, | ||
| ) | ||
| lim = Float32(get(kwargs, :lim, 1.6)) |
There was a problem hiding this comment.
this seems like a non-standard name -- pick a name that matches the Makie API
| function Makie.convert_arguments(::Type{<:Makie.Plot{blochsphereplot}}, state::Ket) | ||
| return (state,) | ||
| end |
| arrowcolor = :red, | ||
| spherecolor = :lightblue, | ||
| spherealpha = 0.15, | ||
| showwireframe = true, | ||
| showaxes = true, | ||
| showlabels = true, | ||
| labelsize = 18, | ||
| title = "Bloch Sphere", | ||
| shaftradius = 0.018, | ||
| tipradius = 0.050, | ||
| tiplength = 0.10, | ||
| lim = 1.6, |
There was a problem hiding this comment.
lim does not seem like a standard name, verify it is correct
a bunch of these do not seem to be used (e.g. title) -- just remove them
| end | ||
|
|
||
|
|
||
| function QuantumOpticsBase.blochsphere(state::Ket; limits=1.6, kwargs...) |
There was a problem hiding this comment.
The API here is a bit unidiomatic.
Let's focus first on having the default Makie recipe functions be in the QuantumOpticsBase namespace. See https://docs.makie.org/stable/explanations/recipes#Makie-Package-Extension
Then, after we have the default functions, we can provide an extra API that is not idiomatic to Makie that provides not just a "plot" but also "axis" manipulation.
There was a problem hiding this comment.
see https://github.com/QuantumSavory/QuantumSavory.jl/blob/master/src/plots.jl, in particular registernetplot_axis
There was a problem hiding this comment.
rename to blochsphereplot_axis as in: https://github.com/QuantumSavory/QuantumSavory.jl/blob/766f69c7f5e595328d46883fb3bd3f945e373b0d/ext/QuantumSavoryMakie/QuantumSavoryMakie.jl#L340-L348
| /docs/build/ | ||
| Manifest.toml | ||
| .vscode | ||
| ~/.julia/ |
There was a problem hiding this comment.
a tilda inside of a gitignore does not really make sense to me -- if you need it, you probably should reorganize how you have set up your work environment
|
Use error hints so that the user is told that they need to import a makie backend. See here https://github.com/QuantumSavory/QuantumSavory.jl/blob/766f69c7f5e595328d46883fb3bd3f945e373b0d/src/QuantumSavory.jl#L103 |
… blochsphere to blochsphereplot_axis, add Observable reactivity test
… blochsphere to blochsphereplot_axis, add Observable reactivity test
Some rendering examples:



Arbitrary:
Spindown:
Spinup:
Adds a Bloch sphere visualization for pure qubit states (Ket) via a Makie extension. The extension loads automatically when the user loads any Makie backend (CairoMakie or GLMakie) — no extra dependencies are added to the base package.
How it works
A pure qubit state |ψ⟩ = α|0⟩ + β|1⟩ is mapped to a point on the unit sphere via the Pauli expectation values: x = 2Re(ᾱβ), y = 2Im(ᾱβ), z = |α|² − |β|². The arrow points to the state on the sphere surface.
Customizable attributes
arrowcolor (default :red) — color of the state vector arrow
spherecolor (default :lightblue) — color of the sphere surface
spherealpha (default 0.15) — transparency of the sphere
showwireframe (default true) — show equator and meridian circles
showaxes (default true) — show dashed x/y/z axis lines
showlabels (default true) — show pole labels |0⟩ |1⟩ |+⟩ |-⟩ |+i⟩ |-i⟩
labelsize (default 18) — font size for pole labels
Files changed
ext/QuantumOpticsBaseMakieExt.jl — Makie recipe and blochsphere convenience constructor
src/visualization.jl — docstring and function stub for blochsphere
src/QuantumOpticsBase.jl — export and include for visualization.jl
Project.toml — Makie added as weak dependency
test/test_plotting.jl — tests using @testitem / TestItemRunner