-
Notifications
You must be signed in to change notification settings - Fork 40
Makie bloch sphere extension #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aniket-umass
wants to merge
18
commits into
qojulia:master
Choose a base branch
from
Aniket-umass:makie-bloch-extension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
27dc6dc
Add Makie Bloch sphere extension using Ket and update Project.toml
Aniket-umass 52258f4
Add Makie Bloch sphere extension using Ket
Aniket-umass 041567b
Made requested changes to Bloch sphere extension
Aniket-umass 5cacd7e
Update Bloch sphere extension and add TestItemRunner tests
Aniket-umass acf1ca9
Remove accidentally committed local dev Project.toml
Aniket-umass b4e919d
Add .gitignore to exclude local Julia dev paths
Aniket-umass 214a214
Merge remote-tracking branch 'origin/master' into makie-bloch-extension
Aniket-umass ac3a93c
Address all PR review comments
Aniket-umass ea795c3
Merge remote-tracking branch 'upstream/master' into makie-bloch-exten…
Aniket-umass f1c60fe
Resolve Project.toml merge conflict with upstream
Aniket-umass 85d0a33
Update ext, test, and QuantumOpticsBase files with all review fixes
Aniket-umass b92a70d
Fix export block and remove GeometryBasics from compat
Aniket-umass 726c762
Add explicit Makie import for CI compatibility
Aniket-umass 70ebacd
fix: use AbstractPlot instead of Makie.Plot to avoid bare Makie import
Aniket-umass 2da4ae6
fix: use AbstractPlot instead of Makie.Plot to avoid bare Makie import
Aniket-umass dd57194
fix: extract limits kwarg explicitly, use c.r/g/b instead of Makie.re…
Aniket-umass 8dd3d5a
refactor: put recipe functions in QuantumOpticsBase namespace, rename…
Aniket-umass 7ff3bfa
refactor: put recipe functions in QuantumOpticsBase namespace, rename…
Aniket-umass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| module QuantumOpticsBaseMakieExt | ||
|
|
||
| import QuantumOpticsBase | ||
| import QuantumOpticsBase: Ket, blochsphereplot, blochsphereplot!, blochsphereplot_axis | ||
| import Makie | ||
| using Makie: Figure, @recipe, Attributes, Axis3 | ||
| using Makie: surface!, arrows3d!, lines!, text!, meshscatter! | ||
| using Makie: Point3f, Vec3f | ||
|
|
||
| @recipe(BlochSpherePlot, state) do scene | ||
| Attributes( | ||
| arrowcolor = :red, | ||
| spherecolor = :lightblue, | ||
| spherealpha = 0.15, | ||
| showwireframe = true, | ||
| showaxes = true, | ||
| showlabels = true, | ||
| labelsize = 18, | ||
| shaftradius = 0.018, | ||
| tipradius = 0.050, | ||
| tiplength = 0.10, | ||
| ) | ||
| end | ||
|
|
||
| function Makie.plot!(p::BlochSpherePlot) | ||
| state_obs = p[1] | ||
|
|
||
| blochvec = Makie.@lift begin | ||
| s = $state_obs | ||
| length(s.data) == 2 || | ||
| error("BlochSphere requires a 2-level (spin-1/2) state") | ||
| α, β = s.data | ||
| Vec3f( | ||
| Float32(2 * real(conj(α) * β)), | ||
| Float32(2 * imag(conj(α) * β)), | ||
| Float32(abs2(α) - abs2(β)), | ||
| ) | ||
| end | ||
|
|
||
| let npts = 200 | ||
| θ = LinRange(0f0, 2f0π, npts) | ||
| φ = LinRange(0f0, Float32(π), npts) | ||
| xs = Float32[cos(t) * sin(q) for t in θ, q in φ] | ||
| ys = Float32[sin(t) * sin(q) for t in θ, q in φ] | ||
| zs = Float32[cos(q) for _ in θ, q in φ] | ||
| c = Makie.to_color(p[:spherecolor][]) | ||
| α = Float32(p[:spherealpha][]) | ||
| rgba = Makie.RGBAf(c.r, c.g, c.b, α) | ||
| surface!(p, xs, ys, zs; | ||
| color = fill(rgba, npts, npts), | ||
| transparency = true, | ||
| ) | ||
| end | ||
|
|
||
| if p[:showwireframe][] | ||
| ncirc = 120 | ||
| θc = LinRange(0f0, 2f0π, ncirc) | ||
| for pts in ( | ||
| [Point3f( cos(t), sin(t), 0f0) for t in θc], | ||
| [Point3f( cos(t), 0f0, sin(t)) for t in θc], | ||
| [Point3f(0f0, cos(t), sin(t)) for t in θc], | ||
| ) | ||
| lines!(p, pts; color = (:black, 0.70), linewidth = 1.2) | ||
| end | ||
| end | ||
|
|
||
| if p[:showaxes][] | ||
| r = 1.18f0 | ||
| for (a, b) in ( | ||
| (Point3f(-r, 0, 0), Point3f(r, 0, 0)), | ||
| (Point3f(0, -r, 0), Point3f(0, r, 0)), | ||
| (Point3f(0, 0, -r), Point3f(0, 0, r)), | ||
| ) | ||
| lines!(p, [a, b]; color = :black, linewidth = 1, linestyle = :dash) | ||
| end | ||
| end | ||
|
|
||
| arrows3d!(p, | ||
| [Point3f(0, 0, 0)], | ||
| Makie.@lift([$blochvec]); | ||
| shaftradius = p[:shaftradius], | ||
| tipradius = p[:tipradius], | ||
| tiplength = p[:tiplength], | ||
| color = p[:arrowcolor], | ||
| ) | ||
|
|
||
| meshscatter!(p, | ||
| Makie.@lift([Point3f($blochvec)]); | ||
| color = p[:arrowcolor], | ||
| markersize = 0.06, | ||
| ) | ||
|
|
||
| if p[:showlabels][] | ||
| ls = p[:labelsize][] | ||
| off = 1.40f0 | ||
| for (pos, lbl, align) in ( | ||
| (Point3f( 0f0, 0f0, off), "|0⟩", (:center, :bottom)), | ||
| (Point3f( 0f0, 0f0, -off), "|1⟩", (:center, :top )), | ||
| (Point3f( off, 0f0, 0f0), "|+⟩", (:left, :center)), | ||
| (Point3f(-off, 0f0, 0f0), "|-⟩", (:right, :center)), | ||
| (Point3f( 0f0, off, 0f0), "|+i⟩", (:left, :center)), | ||
| (Point3f( 0f0, -off, 0f0), "|-i⟩", (:right, :center)), | ||
| ) | ||
| text!(p, pos; text = lbl, fontsize = ls, align = align) | ||
| end | ||
| end | ||
|
|
||
| return p | ||
| end | ||
|
|
||
|
|
||
| function QuantumOpticsBase.blochsphereplot_axis(ax::Makie.AbstractAxis, state; limits=1.6, kwargs...) | ||
| ax.perspectiveness = 0f0 | ||
| lim = Float32(limits) | ||
| Makie.limits!(ax, -lim, lim, -lim, lim, -lim, lim) | ||
| blochsphereplot!(ax, state; kwargs...) | ||
| end | ||
|
|
||
| function QuantumOpticsBase.blochsphereplot_axis(state; limits=1.6, kwargs...) | ||
| fig = Figure(size = (700, 700)) | ||
| ax = Axis3(fig[1, 1]; | ||
| aspect = :data, | ||
| viewmode = :fit, | ||
| perspectiveness = 0f0, | ||
| xticksvisible = false, | ||
| yticksvisible = false, | ||
| zticksvisible = false, | ||
| xticklabelsvisible = false, | ||
| yticklabelsvisible = false, | ||
| zticklabelsvisible = false, | ||
| xlabelvisible = false, | ||
| ylabelvisible = false, | ||
| zlabelvisible = false, | ||
| xspinesvisible = false, | ||
| yspinesvisible = false, | ||
| zspinesvisible = false, | ||
| xgridvisible = false, | ||
| ygridvisible = false, | ||
| zgridvisible = false, | ||
| xypanelvisible = false, | ||
| xzpanelvisible = false, | ||
| yzpanelvisible = false, | ||
| ) | ||
| plt = QuantumOpticsBase.blochsphereplot_axis(ax, state; limits, kwargs...) | ||
| return fig, ax, plt | ||
| end | ||
|
|
||
| end # module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """ | ||
| blochsphereplot(state::Ket; kwargs...) | ||
|
|
||
| Visualize a pure qubit state as an arrow on a Bloch sphere. | ||
|
|
||
| Requires a Makie backend be already imported. | ||
| """ | ||
| function blochsphereplot end | ||
|
|
||
| """ | ||
| blochsphereplot!(ax, state::Ket; kwargs...) | ||
|
|
||
| In-place version of [`blochsphereplot`](@ref). Plots onto an existing Makie axis. | ||
|
|
||
| Requires a Makie backend be already imported. | ||
| """ | ||
| function blochsphereplot! end | ||
|
|
||
| """ | ||
| blochsphereplot_axis([ax,] state::Ket; kwargs...) -> (Figure, Axis3, Plot) | ||
|
|
||
| Visualize a pure qubit state on a Bloch sphere, creating a new Figure and Axis3 | ||
| or plotting onto an existing one. | ||
|
|
||
| Requires a Makie backend be already imported. | ||
| """ | ||
| function blochsphereplot_axis end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| @testitem "Bloch Sphere Plotting" tags=[:plotting] begin | ||
| using QuantumOpticsBase | ||
| using CairoMakie | ||
|
|
||
| b = SpinBasis(1//2) | ||
|
|
||
| # ── Return types ────────────────────────────────────────────────────────── | ||
| @testset "blochsphereplot_axis returns Figure, Axis3, and plot object" begin | ||
| fig, ax, plt = blochsphereplot_axis(spinup(b)) | ||
| @test fig isa Figure | ||
| @test ax isa Axis3 | ||
| @test plt isa AbstractPlot | ||
| end | ||
|
|
||
| # ── Render test ─────────────────────────────────────────────────────────── | ||
| @testset "arbitrary state renders without error" begin | ||
| ψ = cos(π/6)*spinup(b) + exp(im*π/4)*sin(π/6)*spindown(b) | ||
| fig, _, _ = blochsphereplot_axis(ψ) | ||
| save("test_bloch.png", fig) | ||
| @test isfile("test_bloch.png") | ||
| rm("test_bloch.png") | ||
| end | ||
|
|
||
| # ── Render tests: custom attributes ────────────────────────────────────── | ||
| @testset "Custom arrowcolor and spherealpha" begin | ||
| fig, _, _ = blochsphereplot_axis(spinup(b); arrowcolor=:blue, spherealpha=0.3) | ||
| save("test_bloch_custom_color.png", fig) | ||
| @test isfile("test_bloch_custom_color.png") | ||
| rm("test_bloch_custom_color.png") | ||
| end | ||
|
|
||
| @testset "Custom spherecolor" begin | ||
| fig, _, _ = blochsphereplot_axis(spindown(b); spherecolor=:pink, spherealpha=0.2) | ||
| save("test_bloch_custom_sphere.png", fig) | ||
| @test isfile("test_bloch_custom_sphere.png") | ||
| rm("test_bloch_custom_sphere.png") | ||
| end | ||
|
|
||
| @testset "Wireframe, labels and axes toggled off" begin | ||
| fig, _, _ = blochsphereplot_axis(spinup(b); showwireframe=false, showlabels=false, showaxes=false) | ||
| save("test_bloch_minimal.png", fig) | ||
| @test isfile("test_bloch_minimal.png") | ||
| rm("test_bloch_minimal.png") | ||
| end | ||
|
|
||
| # ── Error handling ──────────────────────────────────────────────────────── | ||
| @testset "Wrong dimension state throws error" begin | ||
| b3 = SpinBasis(1) | ||
| ψ_3 = basisstate(b3, 1) | ||
| @test_throws ErrorException blochsphereplot_axis(ψ_3) | ||
| end | ||
|
|
||
| # ── Observable reactivity ───────────────────────────────────────────────── | ||
| @testset "Observable state updates reactively" begin | ||
| using Makie: Observable | ||
| state_obs = Observable(spinup(b)) | ||
| fig, ax, _ = blochsphereplot_axis(state_obs) | ||
| state_obs[] = spindown(b) | ||
| save("test_bloch_observable.png", fig) | ||
| @test isfile("test_bloch_observable.png") | ||
| rm("test_bloch_observable.png") | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.