Skip to content

Commit b670f9c

Browse files
Align with qutip
1 parent 4c54a89 commit b670f9c

File tree

2 files changed

+54
-70
lines changed

2 files changed

+54
-70
lines changed

ext/QuantumToolboxMakieExt.jl

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import Makie:
3434
update_cam!,
3535
cam3d!
3636

37+
import Makie.GeometryBasics: Tessellation
38+
3739
@doc raw"""
3840
plot_wigner(
3941
library::Val{:Makie},
@@ -349,8 +351,8 @@ Render the Bloch sphere visualization from the given [`Bloch`](@ref) object `b`.
349351
function QuantumToolbox.render(b::Bloch; location = nothing)
350352
fig, lscene = _setup_bloch_plot!(b, location)
351353
_draw_bloch_sphere!(b, lscene)
352-
_draw_reference_circles!(lscene)
353-
_draw_axes!(lscene)
354+
_draw_reference_circles!(b, lscene)
355+
_draw_axes!(b, lscene)
354356
_plot_points!(b, lscene)
355357
_plot_lines!(b, lscene)
356358
_plot_arcs!(b, lscene)
@@ -376,9 +378,7 @@ Sets up the `3D` coordinate system with appropriate limits and view angles.
376378
"""
377379
function _setup_bloch_plot!(b::Bloch, location)
378380
fig, location = _getFigAndLocation(location)
379-
bg_color = parse(RGBf, b.frame_color)
380-
frame_color = RGBAf(bg_color, b.frame_alpha)
381-
lscene = LScene(location, show_axis = false, scenekw = (backgroundcolor = frame_color,))
381+
lscene = LScene(location, show_axis = false, scenekw = (clear = true,))
382382
length(b.view) == 2 || throw(ArgumentError("The length of `Bloch.view` must be 2."))
383383
cam3d!(lscene.scene, center = false)
384384
cam = cameracontrols(lscene)
@@ -393,65 +393,52 @@ raw"""
393393
Draw the translucent sphere representing the Bloch sphere surface.
394394
"""
395395
function _draw_bloch_sphere!(b::Bloch, lscene)
396-
n_lon = 4
397-
n_lat = 4
398396
radius = 1.0f0
399-
base_color = parse(RGBf, b.sphere_color)
400-
sphere_color = RGBAf(base_color, b.sphere_alpha)
401397
sphere_mesh = Sphere(Point3f(0), radius)
402-
mesh!(lscene, sphere_mesh; color = sphere_color, shading = NoShading, transparency = true, rasterize = 3)
403-
θ_vals = range(0.0f0, 2π, length = n_lon + 1)[1:(end-1)]
404-
φ_curve = range(0.0f0, π, length = 600)
405-
line_alpha = max(0.05, b.sphere_alpha * 0.5)
398+
mesh!(
399+
lscene,
400+
sphere_mesh;
401+
color = b.sphere_color,
402+
alpha = b.sphere_alpha,
403+
shading = NoShading,
404+
transparency = true,
405+
rasterize = 3,
406+
)
407+
θ_vals = range(0, π, 5)[1:(end-1)]
408+
φ_curve = range(0, 2π, 600)
406409
for θi in θ_vals
407-
x_line = [radius * sin(ϕ) * cos(θi) for ϕ in φ_curve]
408-
y_line = [radius * sin(ϕ) * sin(θi) for ϕ in φ_curve]
409-
z_line = [radius * cos(ϕ) for ϕ in φ_curve]
410-
lines!(
411-
lscene,
412-
x_line,
413-
y_line,
414-
z_line;
415-
color = RGBAf(0.5, 0.5, 0.5, line_alpha),
416-
linewidth = 1,
417-
transparency = true,
418-
)
410+
x_line = radius * sin.(φ_curve) .* cos(θi)
411+
y_line = radius * sin.(φ_curve) .* sin(θi)
412+
z_line = radius * cos.(φ_curve)
413+
lines!(lscene, x_line, y_line, z_line; color = b.frame_color, alpha = b.frame_alpha)
419414
end
420-
φ_vals = range(0.0f0, π, length = n_lat + 2)
421-
θ_curve = range(0.0f0, 2π, length = 600)
415+
φ_vals = range(0, π, 6)
416+
θ_curve = range(0, 2π, 600)
422417
for ϕ in φ_vals
423-
x_ring = [radius * sin(ϕ) * cos(θi) for θi in θ_curve]
424-
y_ring = [radius * sin(ϕ) * sin(θi) for θi in θ_curve]
418+
x_ring = radius * sin(ϕ) .* cos.(θ_curve)
419+
y_ring = radius * sin(ϕ) .* sin.(θ_curve)
425420
z_ring = fill(radius * cos(ϕ), length(θ_curve))
426-
lines!(
427-
lscene,
428-
x_ring,
429-
y_ring,
430-
z_ring;
431-
color = RGBAf(0.5, 0.5, 0.5, line_alpha),
432-
linewidth = 1,
433-
transparency = true,
434-
)
421+
lines!(lscene, x_ring, y_ring, z_ring; color = b.frame_color, alpha = b.frame_alpha)
435422
end
436423
end
437424

438425
raw"""
439-
_draw_reference_circles!(lscene)
426+
_draw_reference_circles!(b::Bloch, lscene)
440427
441-
Draw the three great circles `(XY, YZ, XZ planes)` on the Bloch sphere.
428+
Draw the three great circles `(XY, XZ planes)` on the Bloch sphere.
442429
443430
# Arguments
431+
- `b::Bloch`: Bloch sphere object containing frame color
444432
- `lscene`: Makie LScene object for drawing
445433
446434
Adds faint circular guidelines representing the three principal planes.
447435
"""
448-
function _draw_reference_circles!(lscene)
449-
wire_color = RGBAf(0.5, 0.5, 0.5, 0.4)
436+
function _draw_reference_circles!(b::Bloch, lscene)
437+
wire_color = b.frame_color
450438
φ = range(0, 2π, length = 100)
451-
# XY, YZ, XZ circles
439+
# XY, XZ circles
452440
circles = [
453441
[Point3f(cos(φi), sin(φi), 0) for φi in φ], # XY
454-
[Point3f(0, cos(φi), sin(φi)) for φi in φ], # YZ
455442
[Point3f(cos(φi), 0, sin(φi)) for φi in φ], # XZ
456443
]
457444
for circle in circles
@@ -460,25 +447,25 @@ function _draw_reference_circles!(lscene)
460447
end
461448

462449
raw"""
463-
_draw_axes!(lscene)
450+
_draw_axes!(b::Bloch, lscene)
464451
465452
Draw the three principal axes `(x, y, z)` of the Bloch sphere.
466453
467454
# Arguments
455+
- `b::Bloch`: Bloch sphere object containing axis color
468456
- `lscene`: Makie LScene object for drawing
469457
470458
Creates visible axis lines extending slightly beyond the unit sphere.
471459
"""
472-
function _draw_axes!(lscene)
473-
axis_color = RGBAf(0.3, 0.3, 0.3, 0.8)
474-
axis_width = 0.8
460+
function _draw_axes!(b::Bloch, lscene)
461+
axis_color = b.frame_color
475462
axes = [
476-
([Point3f(1.0, 0, 0), Point3f(-1.0, 0, 0)], "x"), # X-axis
477-
([Point3f(0, 1.0, 0), Point3f(0, -1.0, 0)], "y"), # Y-axis
478-
([Point3f(0, 0, 1.0), Point3f(0, 0, -1.0)], "z"), # Z-axis
463+
[Point3f(1.0, 0, 0), Point3f(-1.0, 0, 0)], # X-axis
464+
[Point3f(0, 1.0, 0), Point3f(0, -1.0, 0)], # Y-axis
465+
[Point3f(0, 0, 1.0), Point3f(0, 0, -1.0)], # Z-axis
479466
]
480-
for (points, _) in axes
481-
lines!(lscene, points; color = axis_color, linewidth = axis_width)
467+
for points in axes
468+
lines!(lscene, points; color = axis_color)
482469
end
483470
end
484471

@@ -667,52 +654,51 @@ function _add_labels!(b::Bloch, lscene)
667654

668655
label_color = parse(RGBf, b.font_color)
669656
label_size = b.font_size
670-
offset_scale = b.frame_limit
671657

672658
(b.xlabel[1] == "") || text!(
673659
lscene,
674660
b.xlabel[1],
675-
position = Point3f(offset_scale * b.xlpos[1], 0, 0),
661+
position = Point3f(b.xlpos[1], 0, 0),
676662
color = label_color,
677663
fontsize = label_size,
678664
align = (:center, :center),
679665
)
680666
(b.xlabel[2] == "") || text!(
681667
lscene,
682668
b.xlabel[2],
683-
position = Point3f(offset_scale * b.xlpos[2], 0, 0),
669+
position = Point3f(b.xlpos[2], 0, 0),
684670
color = label_color,
685671
fontsize = label_size,
686672
align = (:center, :center),
687673
)
688674
(b.ylabel[1] == "") || text!(
689675
lscene,
690676
b.ylabel[1],
691-
position = Point3f(0, offset_scale * b.ylpos[1], 0),
677+
position = Point3f(0, b.ylpos[1], 0),
692678
color = label_color,
693679
fontsize = label_size,
694680
align = (:center, :center),
695681
)
696682
(b.ylabel[2] == "") || text!(
697683
lscene,
698684
b.ylabel[2],
699-
position = Point3f(0, offset_scale * b.ylpos[2], 0),
685+
position = Point3f(0, b.ylpos[2], 0),
700686
color = label_color,
701687
fontsize = label_size,
702688
align = (:center, :center),
703689
)
704690
(b.zlabel[1] == "") || text!(
705691
lscene,
706692
b.zlabel[1],
707-
position = Point3f(0, 0, offset_scale * b.zlpos[1]),
693+
position = Point3f(0, 0, b.zlpos[1]),
708694
color = label_color,
709695
fontsize = label_size,
710696
align = (:center, :center),
711697
)
712698
(b.zlabel[2] == "") || text!(
713699
lscene,
714700
b.zlabel[2],
715-
position = Point3f(0, 0, offset_scale * b.zlpos[2]),
701+
position = Point3f(0, 0, b.zlpos[2]),
716702
color = label_color,
717703
fontsize = label_size,
718704
align = (:center, :center),

src/visualization.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ A structure representing a Bloch sphere visualization for quantum states.
8181
8282
- `font_color::String`: Color of axis labels and text
8383
- `font_size::Int`: Font size for labels. Default: `15`
84-
- `frame_alpha::Float64`: Transparency of the frame background
85-
- `frame_color::String`: Background color of the frame
86-
- `frame_limit::Float64`: Axis limits for the 3D frame (symmetric around origin)
84+
- `frame_alpha::Float64`: Transparency of the wireframe
85+
- `frame_color::String`: Color of the wireframe
8786
8887
## Point properties
8988
@@ -111,11 +110,11 @@ A structure representing a Bloch sphere visualization for quantum states.
111110
112111
## Label properties
113112
- `xlabel::Vector{AbstractString}`: Labels for x-axis. Default: `[L"x", ""]`
114-
- `xlpos::Vector{Float64}`: Positions of x-axis labels. Default: `[1.0, -1.0]`
113+
- `xlpos::Vector{Float64}`: Positions of x-axis labels. Default: `[1.2, -1.2]`
115114
- `ylabel::Vector{AbstractString}`: Labels for y-axis. Default: `[L"y", ""]`
116-
- `ylpos::Vector{Float64}`: Positions of y-axis labels. Default: `[1.0, -1.0]`
115+
- `ylpos::Vector{Float64}`: Positions of y-axis labels. Default: `[1.2, -1.2]`
117116
- `zlabel::Vector{AbstractString}`: Labels for z-axis. Default: `[L"|0\rangle", L"|1\rangle"]`
118-
- `zlpos::Vector{Float64}`: Positions of z-axis labels. Default: `[1.0, -1.0]`
117+
- `zlpos::Vector{Float64}`: Positions of z-axis labels. Default: `[1.2, -1.2]`
119118
"""
120119
@kwdef mutable struct Bloch
121120
points::Vector{Matrix{Float64}} = Vector{Matrix{Float64}}()
@@ -126,7 +125,6 @@ A structure representing a Bloch sphere visualization for quantum states.
126125
font_size::Int = 15
127126
frame_alpha::Float64 = 0.1
128127
frame_color::String = "gray"
129-
frame_limit::Float64 = 1.2
130128
point_default_color::Vector{String} = ["blue", "red", "green", "#CC6600"]
131129
point_color::Vector{Union{Nothing,String}} = Union{Nothing,String}[]
132130
point_marker::Vector{Symbol} = [:circle, :rect, :diamond, :utriangle]
@@ -140,11 +138,11 @@ A structure representing a Bloch sphere visualization for quantum states.
140138
vector_arrowsize::Vector{Float64} = [0.07, 0.08, 0.08]
141139
view::Vector{Int} = [30, 30]
142140
xlabel::Vector{AbstractString} = [L"x", ""]
143-
xlpos::Vector{Float64} = [1.0, -1.0]
141+
xlpos::Vector{Float64} = [1.2, -1.2]
144142
ylabel::Vector{AbstractString} = [L"y", ""]
145-
ylpos::Vector{Float64} = [1.0, -1.0]
143+
ylpos::Vector{Float64} = [1.2, -1.2]
146144
zlabel::Vector{AbstractString} = [L"|0\rangle", L"|1\rangle"]
147-
zlpos::Vector{Float64} = [1.0, -1.0]
145+
zlpos::Vector{Float64} = [1.2, -1.2]
148146
end
149147

150148
const BLOCH_DATA_FIELDS = (:points, :vectors, :lines, :arcs)

0 commit comments

Comments
 (0)