Skip to content

Commit ab258f0

Browse files
author
cailixun
committed
improve bloch sphere visualization
1 parent 13709a1 commit ab258f0

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

ext/QuantumToolboxMakieExt.jl

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,24 @@ Render the Bloch sphere visualization from the given [`Bloch`](@ref) object `b`.
337337
# Arguments
338338
339339
- `b::Bloch`: The Bloch sphere object containing states, vectors, and settings to visualize.
340-
- `location::Union{GridPosition,Nothing}`: The location of the plot in the layout. If `nothing`, the plot is created in a new figure. Default is `nothing`.
340+
- `location::Union{GridPosition,LScene,Nothing}`: The location of the plot in the layout or `Makie.LScene`
341+
341342
342343
# Returns
343344
344345
- A tuple `(fig, lscene)` where `fig` is the figure object and `lscene` is the LScene object used for plotting. These can be further manipulated or saved by the user.
346+
347+
# Notes
348+
349+
The keyword argument `location` can be in the either type:
350+
351+
- `Nothing` (default): Create a new figure and plot the Bloch sphere.
352+
- `GridPosition`: Plot the Bloch sphere in the specified location of the plot in the layout.
353+
- `LScene`: Update the existing Bloch sphere using new data and settings in `b::Bloch` without creating new `Figure` and `LScene` (efficient for drawing animation).
345354
"""
346355
function QuantumToolbox.render(b::Bloch; location = nothing)
347-
fig, lscene = _setup_bloch_plot!(b, location)
356+
fig, lscene = _setup_bloch_plot!(location)
357+
_setup_bloch_camara!(b, lscene)
348358
_draw_bloch_sphere!(b, lscene)
349359
_add_labels!(b, lscene)
350360

@@ -358,30 +368,46 @@ function QuantumToolbox.render(b::Bloch; location = nothing)
358368
end
359369

360370
raw"""
361-
_setup_bloch_plot!(b::Bloch, location) -> (fig, lscene)
371+
_setup_bloch_plot!(location) -> (fig, lscene)
362372
363373
Initialize the figure and `3D` axis for Bloch sphere visualization.
364374
365375
# Arguments
366-
- `b::Bloch`: Bloch sphere object containing view parameters
367-
- `location`: Figure layout position specification
376+
- `location`: Figure layout position specification, or directly `Makie.LScene` for update
368377
369378
# Returns
370379
- `fig`: Created Makie figure
371380
- `lscene`: Configured LScene object
372381
373382
Sets up the `3D` coordinate system with appropriate limits and view angles.
374383
"""
375-
function _setup_bloch_plot!(b::Bloch, location)
384+
function _setup_bloch_plot!(location)
376385
fig, location = _getFigAndLocation(location)
377386
lscene = LScene(location, show_axis = false, scenekw = (clear = true,))
387+
return fig, lscene
388+
end
389+
390+
function _setup_bloch_plot!(lscene::LScene)
391+
# this function only removes all existing Plots in lscene
392+
# it is useful for users to just update Bloch sphere without creating new figure and lscene (efficient for drawing animation)
393+
fig = lscene.parent
394+
empty!(lscene.scene.plots)
395+
return fig, lscene
396+
end
397+
398+
raw"""
399+
_setup_bloch_camara!(b::Bloch, lscene)
400+
401+
Setup the distance and viewing angle of the camara.
402+
"""
403+
function _setup_bloch_camara!(b::Bloch, lscene)
378404
length(b.view) == 2 || throw(ArgumentError("The length of `Bloch.view` must be 2."))
379405
cam3d!(lscene.scene, center = false)
380406
cam = cameracontrols(lscene)
381407
cam.fov[] = 12 # Set field of view to 12 degrees
382408
dist = 12 # Set distance from the camera to the Bloch sphere
383409
update_cam!(lscene.scene, cam, deg2rad(b.view[1]), deg2rad(b.view[2]), dist)
384-
return fig, lscene
410+
return nothing
385411
end
386412

387413
raw"""

0 commit comments

Comments
 (0)