Skip to content

Commit d20a08e

Browse files
committed
add codereview suggestions
1 parent 06e9bed commit d20a08e

File tree

4 files changed

+122
-129
lines changed

4 files changed

+122
-129
lines changed

ext/QuantumToolboxMakieExt.jl

Lines changed: 70 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,14 @@ add_line!(b, ψ₁, ψ₂; fmt = "r--")
415415
```
416416
"""
417417
function QuantumToolbox.add_line!(
418-
b::QuantumToolbox.Bloch,
419-
start_point_point::QuantumObject{<:Union{Ket,Bra,Operator}},
420-
end_point::QuantumObject{<:Union{Ket,Bra,Operator}};
418+
b::Bloch,
419+
p1::QuantumObject{<:Union{Ket,Bra,Operator}},
420+
p2::QuantumObject{<:Union{Ket,Bra,Operator}};
421421
fmt = "k",
422-
kwargs...,
423422
)
424-
p1 = if isket(start_point_point) || isbra(start_point_point)
425-
_state_to_bloch(start_point_point)
426-
else
427-
_dm_to_bloch(start_point_point)
428-
end
429-
p2 = if isket(end_point) || isbra(end_point)
430-
_state_to_bloch(end_point)
431-
else
432-
_dm_to_bloch(end_point)
433-
end
434-
x = [p1[2], p2[2]]
435-
y = [-p1[1], -p2[1]]
436-
z = [p1[3], p2[3]]
437-
return push!(b.lines, ([x, y, z], fmt, kwargs))
423+
coords1 = isket(p1) || isbra(p1) ? _state_to_bloch(p1) : _dm_to_bloch(p1)
424+
coords2 = isket(p2) || isbra(p2) ? _state_to_bloch(p2) : _dm_to_bloch(p2)
425+
return add_line!(b, coords1, coords2; fmt = fmt)
438426
end
439427

440428
raw"""
@@ -490,20 +478,20 @@ Render the Bloch sphere visualization from the given `Bloch` object `b`.
490478
These can be further manipulated or saved by the user.
491479
"""
492480
function QuantumToolbox.render(b::Bloch; location = nothing)
493-
fig, ax = _setup_bloch_plot(b, location)
494-
_draw_bloch_sphere(ax)
495-
_draw_reference_circles(ax)
496-
_draw_axes(ax)
497-
_plot_points(b, ax)
498-
_plot_lines(b, ax)
499-
_plot_arcs(b, ax)
500-
_plot_vectors(b, ax)
501-
_add_labels(ax)
481+
fig, ax = _setup_bloch_plot!(b, location)
482+
_draw_bloch_sphere!(ax)
483+
_draw_reference_circles!(ax)
484+
_draw_axes!(ax)
485+
_plot_points!(b, ax)
486+
_plot_lines!(b, ax)
487+
_plot_arcs!(b, ax)
488+
_plot_vectors!(b, ax)
489+
_add_labels!(ax)
502490
return fig, ax
503491
end
504492

505493
raw"""
506-
_setup_bloch_plot(b::Bloch, location) -> (fig, ax)
494+
_setup_bloch_plot!(b::Bloch, location) -> (fig, ax)
507495
508496
Initialize the figure and `3D` axis for Bloch sphere visualization.
509497
@@ -517,12 +505,12 @@ Initialize the figure and `3D` axis for Bloch sphere visualization.
517505
518506
Sets up the `3D` coordinate system with appropriate limits and view angles.
519507
"""
520-
function _setup_bloch_plot(b::Bloch, location)
508+
function _setup_bloch_plot!(b::Bloch, location)
521509
fig, location = _getFigAndLocation(location)
522510
ax = Axis3(
523511
location;
524512
aspect = :data,
525-
limits = (-1.1, 1.1, -1.1, 1.1, -1.1, 1.1),
513+
limits = (-1.4, 1.4, -1.4, 1.4, -1.4, 1.4),
526514
xgridvisible = false,
527515
ygridvisible = false,
528516
zgridvisible = false,
@@ -551,22 +539,37 @@ function _setup_bloch_plot(b::Bloch, location)
551539
end
552540

553541
raw"""
554-
_draw_bloch_sphere(ax)
542+
_draw_bloch_sphere!(ax)
555543
556544
Draw the translucent sphere representing the Bloch sphere surface.
557-
558-
# Arguments
559-
- `ax`: Makie Axis3 object where the sphere will be drawn
560-
561-
Creates a semi-transparent spherical surface at the origin with radius 1.
562545
"""
563-
function _draw_bloch_sphere(ax)
564-
sphere_color = RGBAf(1.0, 0.86, 0.86, 0.2)
565-
return mesh!(ax, Sphere(Point3f(0, 0, 0), 1.0f0); color = sphere_color, transparency = true)
546+
function _draw_bloch_sphere!(ax)
547+
n_lon = 4
548+
n_lat = 4
549+
radius = 1.0f0
550+
sphere_mesh = Sphere(Point3f(0), radius)
551+
mesh!(ax, sphere_mesh; color = RGBAf(0.9, 0.9, 0.9, 0.3), shading = false, transparency = true)
552+
θ_vals = range(0.0f0, 2π, length = n_lon + 1)[1:(end-1)]
553+
φ_curve = range(0.0f0, π, length = 600)
554+
for θi in θ_vals
555+
x_line = [radius * sin(ϕ) * cos(θi) for ϕ in φ_curve]
556+
y_line = [radius * sin(ϕ) * sin(θi) for ϕ in φ_curve]
557+
z_line = [radius * cos(ϕ) for ϕ in φ_curve]
558+
lines!(ax, x_line, y_line, z_line; color = RGBAf(0.5, 0.5, 0.5, 0.1), linewidth = 1, transparency = true)
559+
end
560+
φ_vals = range(0.0f0, π, length = n_lat + 2)
561+
θ_curve = range(0.0f0, 2π, length = 600)
562+
for ϕ in φ_vals
563+
x_ring = [radius * sin(ϕ) * cos(θi) for θi in θ_curve]
564+
y_ring = [radius * sin(ϕ) * sin(θi) for θi in θ_curve]
565+
z_ring = fill(radius * cos(ϕ), length(θ_curve))
566+
lines!(ax, x_ring, y_ring, z_ring; color = RGBAf(0.5, 0.5, 0.5, 0.1), linewidth = 1, transparency = true)
567+
end
568+
return nothing
566569
end
567570

568571
raw"""
569-
_draw_reference_circles(ax)
572+
_draw_reference_circles!(ax)
570573
571574
Draw the three great circles `(XY, YZ, XZ planes)` on the Bloch sphere.
572575
@@ -575,7 +578,7 @@ Draw the three great circles `(XY, YZ, XZ planes)` on the Bloch sphere.
575578
576579
Adds faint circular guidelines representing the three principal planes.
577580
"""
578-
function _draw_reference_circles(ax)
581+
function _draw_reference_circles!(ax)
579582
wire_color = RGBAf(0.5, 0.5, 0.5, 0.4)
580583
φ = range(0, 2π, length = 100)
581584
# XY, YZ, XZ circles
@@ -590,7 +593,7 @@ function _draw_reference_circles(ax)
590593
end
591594

592595
raw"""
593-
_draw_axes(ax)
596+
_draw_axes!(ax)
594597
595598
Draw the three principal axes `(x, y, z)` of the Bloch sphere.
596599
@@ -599,21 +602,21 @@ Draw the three principal axes `(x, y, z)` of the Bloch sphere.
599602
600603
Creates visible axis lines extending slightly beyond the unit sphere.
601604
"""
602-
function _draw_axes(ax)
605+
function _draw_axes!(ax)
603606
axis_color = RGBAf(0.3, 0.3, 0.3, 0.8)
604607
axis_width = 0.8
605608
axes = [
606-
([Point3f(0, -1.01, 0), Point3f(0, 1.01, 0)], "y"), # Y-axis
607-
([Point3f(-1.01, 0, 0), Point3f(1.01, 0, 0)], "x"), # X-axis
608-
([Point3f(0, 0, -1.01), Point3f(0, 0, 1.01)], "z"), # Z-axis
609+
([Point3f(0, -1.0, 0), Point3f(0, 1.0, 0)], "y"), # Y-axis
610+
([Point3f(-1.0, 0, 0), Point3f(1.0, 0, 0)], "x"), # X-axis
611+
([Point3f(0, 0, -1.0), Point3f(0, 0, 1.0)], "z"), # Z-axis
609612
]
610613
for (points, _) in axes
611614
lines!(ax, points; color = axis_color, linewidth = axis_width)
612615
end
613616
end
614617

615618
raw"""
616-
_plot_points(b::Bloch, ax)
619+
_plot_points!(b::Bloch, ax)
617620
618621
Plot all quantum state points on the Bloch sphere.
619622
@@ -623,7 +626,7 @@ Plot all quantum state points on the Bloch sphere.
623626
624627
Handles both scatter points and line traces based on style specifications.
625628
"""
626-
function _plot_points(b::Bloch, ax)
629+
function _plot_points!(b::Bloch, ax)
627630
for (k, points) in enumerate(b.points)
628631
style = b.point_style[k]
629632
color = b.point_color[k]
@@ -650,7 +653,7 @@ function _plot_points(b::Bloch, ax)
650653
end
651654

652655
raw"""
653-
_plot_lines(b::Bloch, ax)
656+
_plot_lines!(b::Bloch, ax)
654657
655658
Draw all connecting lines between points on the Bloch sphere.
656659
@@ -660,13 +663,14 @@ Draw all connecting lines between points on the Bloch sphere.
660663
661664
Processes line style specifications and color mappings.
662665
"""
663-
function _plot_lines(b::Bloch, ax)
664-
color_map = Dict("k"=>:black, "r"=>:red, "g"=>:green, "b"=>:blue, "c"=>:cyan, "m"=>:magenta, "y"=>:yellow)
665-
for (line, fmt, kwargs) in b.lines
666+
function _plot_lines!(b::Bloch, ax)
667+
color_map =
668+
Dict("k" => :black, "r" => :red, "g" => :green, "b" => :blue, "c" => :cyan, "m" => :magenta, "y" => :yellow)
669+
for (line, fmt) in b.lines
666670
x, y, z = line
667-
c = get(color_map, first(fmt), :black)
668-
# Determine line style
669-
ls = if occursin("--", fmt)
671+
color_char = first(fmt)
672+
color = get(color_map, color_char, :black)
673+
linestyle = if occursin("--", fmt)
670674
:dash
671675
elseif occursin(":", fmt)
672676
:dot
@@ -675,20 +679,12 @@ function _plot_lines(b::Bloch, ax)
675679
else
676680
:solid
677681
end
678-
lines!(
679-
ax,
680-
x,
681-
y,
682-
z;
683-
color = get(kwargs, :color, c),
684-
linewidth = get(kwargs, :linewidth, 1.0),
685-
linestyle = get(kwargs, :linestyle, ls),
686-
)
682+
lines!(ax, x, y, z; color = color, linewidth = 1.0, linestyle = linestyle)
687683
end
688684
end
689685

690686
raw"""
691-
_plot_arcs(b::Bloch, ax)
687+
_plot_arcs!(b::Bloch, ax)
692688
693689
Draw circular arcs connecting points on the Bloch sphere surface.
694690
@@ -698,7 +694,7 @@ Draw circular arcs connecting points on the Bloch sphere surface.
698694
699695
Calculates great circle arcs between specified points.
700696
"""
701-
function _plot_arcs(b::Bloch, ax)
697+
function _plot_arcs!(b::Bloch, ax)
702698
for arc_pts in b.arcs
703699
length(arc_pts) >= 2 || continue
704700
v1 = normalize(arc_pts[1])
@@ -716,7 +712,7 @@ function _plot_arcs(b::Bloch, ax)
716712
end
717713

718714
raw"""
719-
_plot_vectors(b::Bloch, ax)
715+
_plot_vectors!(b::Bloch, ax)
720716
721717
Draw vectors from origin representing quantum states.
722718
@@ -726,7 +722,7 @@ Draw vectors from origin representing quantum states.
726722
727723
Scales vectors appropriately and adds `3D` arrow markers.
728724
"""
729-
function _plot_vectors(b::Bloch, ax)
725+
function _plot_vectors!(b::Bloch, ax)
730726
isempty(b.vectors) && return
731727
r = 1.0
732728
for (i, v) in enumerate(b.vectors)
@@ -748,7 +744,7 @@ function _plot_vectors(b::Bloch, ax)
748744
end
749745

750746
raw"""
751-
_add_labels(ax)
747+
_add_labels!(ax)
752748
753749
Add axis labels and state labels to the Bloch sphere.
754750
@@ -757,16 +753,16 @@ Add axis labels and state labels to the Bloch sphere.
757753
758754
Positions standard labels `(x, y, |0⟩, |1⟩)` at appropriate locations.
759755
"""
760-
function _add_labels(ax)
756+
function _add_labels!(ax)
761757
label_color = RGBf(0.2, 0.2, 0.2)
762758
label_size = 16
763759
label_font = "TeX Gyre Heros"
764760

765761
labels = [
766-
(Point3f(1.04, 0, 0), L"\textbf{y}"),
767-
(Point3f(0, -1.10, 0), L"\textbf{x}"),
768-
(Point3f(0, 0, -1.10), L"\mathbf{|1\rangle}"),
769-
(Point3f(0, 0, 1.08), L"\mathbf{|0\rangle}"),
762+
(Point3f(1.34, -0.1, 0.02), L"\textbf{y}"),
763+
(Point3f(0.07, -1.39, 0.02), L"\textbf{x}"),
764+
(Point3f(-0.08, 0.02, -1.4), L"\mathbf{|1\rangle}"),
765+
(Point3f(-0.08, 0.0, 1.2), L"\mathbf{|0\rangle}"),
770766
]
771767
for (pos, text) in labels
772768
text!(ax, text; position = pos, color = label_color, fontsize = label_size, font = label_font)

0 commit comments

Comments
 (0)