Skip to content

Makie bloch sphere extension #216

Open
Aniket-umass wants to merge 18 commits into
qojulia:masterfrom
Aniket-umass:makie-bloch-extension
Open

Makie bloch sphere extension #216
Aniket-umass wants to merge 18 commits into
qojulia:masterfrom
Aniket-umass:makie-bloch-extension

Conversation

@Aniket-umass
Copy link
Copy Markdown

@Aniket-umass Aniket-umass commented Feb 2, 2026

Some rendering examples:
Arbitrary:
bloch_arbitrary
Spindown:
bloch_spindown
Spinup:
bloch_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

Copy link
Copy Markdown
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have included some files from your local dev environment -- these should be removed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not seem to be resolved

Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
Comment thread Project.toml Outdated
Comment thread Project.toml Outdated
Comment thread Project.toml
@Krastanov Krastanov marked this pull request as draft February 2, 2026 20:39
@Krastanov
Copy link
Copy Markdown
Member

include("ext/QuantumOpticsBaseMakieExt.jl")

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.

Copy link
Copy Markdown
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread Project.toml Outdated
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
version = "0.5.9"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't add "lint"

always check the file tab in the PR to look for unrelated changes that you have made by mistake

Comment thread Project.toml Outdated
FastExpm = "7868e603-8603-432e-a1a1-694bd70b01f2"
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have added a new dependency -- this is supposed to be an extension

Comment thread src/QuantumOpticsBase.jl Outdated
Comment on lines +80 to +81
error("Blochsphere visualization requires a Makie extension.\n" *
"Load QuantumOpticsBase together with CairoMakie or GLMakie.")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of creating a method for a function, just declare a function without any methods

function blochsphere end

Comment thread src/QuantumOpticsBase.jl Outdated
ChiMatrix, DenseChiMatrix, avg_gate_fidelity,
SumBasis, directsum, ⊕, LazyDirectSum, getblock, setblock!,
qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2
qfunc, wigner, coherentspinstate, qfuncsu2, wignersu2, blochsphere
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/QuantumOpticsBase.jl Outdated
#apply
apply!

########### Bloch Sphere API Stub ###########
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread test/test_plotting.jl Outdated
Comment on lines +90 to +130
@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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just pick one random arbitrary unnamed state and test for that -- these repeated tests do not really add much value to the test runner

Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
lim = Float32(get(kwargs, :lim, 1.6))
Makie.limits!(ax, -lim, lim, -lim, lim, -lim, lim)
blochsphereplot!(ax, state; kwargs...)
return fig
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return also the axis and the plot object

Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
xzpanelvisible = false,
yzpanelvisible = false,
)
lim = Float32(get(kwargs, :lim, 1.6))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a non-standard name -- pick a name that matches the Makie API

Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
Comment on lines +28 to +30
function Makie.convert_arguments(::Type{<:Makie.Plot{blochsphereplot}}, state::Ket)
return (state,)
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
Comment on lines +13 to +24
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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Aniket-umass Aniket-umass marked this pull request as ready for review May 18, 2026 01:51
Comment thread ext/QuantumOpticsBaseMakieExt.jl Outdated
end


function QuantumOpticsBase.blochsphere(state::Ket; limits=1.6, kwargs...)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread .gitignore Outdated
/docs/build/
Manifest.toml
.vscode
~/.julia/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread test/test_plotting.jl
@Krastanov
Copy link
Copy Markdown
Member

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants