Skip to content

Commit 7780973

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

File tree

4 files changed

+128
-124
lines changed

4 files changed

+128
-124
lines changed

ext/QuantumToolboxMakieExt.jl

Lines changed: 76 additions & 69 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,7 +539,7 @@ 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.
557545
@@ -560,13 +548,39 @@ Draw the translucent sphere representing the Bloch sphere surface.
560548
561549
Creates a semi-transparent spherical surface at the origin with radius 1.
562550
"""
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)
551+
function _draw_bloch_sphere!(ax)
552+
n_lon = 3
553+
n_lat = 3
554+
resolution=100
555+
center = (0.0f0, 0.0f0, 0.0f0)
556+
radius = 1.0f0
557+
u = range(0, 2π, length = resolution)
558+
v = range(0, π, length = resolution)
559+
x = [center[1] + radius * sin(φ) * cos(θ) for φ in v, θ in u]
560+
y = [center[2] + radius * sin(φ) * sin(θ) for φ in v, θ in u]
561+
z = [center[3] + radius * cos(φ) for φ in v, θ in u]
562+
sphere_color = fill(RGBAf(0.9, 0.9, 0.9, 1.0), size(x))
563+
surface!(ax, x, y, z, color = sphere_color, transparency = true)
564+
θ_vals = range(0, 2π, length = n_lon+1)[1:(end-1)]
565+
φ_curve = range(0, π, length = resolution)
566+
for θ in θ_vals
567+
x_line = [center[1] + radius * sin(φ) * cos(θ) for φ in φ_curve]
568+
y_line = [center[2] + radius * sin(φ) * sin(θ) for φ in φ_curve]
569+
z_line = [center[3] + radius * cos(φ) for φ in φ_curve]
570+
lines!(ax, x_line, y_line, z_line, color = RGBAf(0.5, 0.5, 0.5, 0.5), transparency = true)
571+
end
572+
φ_vals = range(0, π, length = n_lat+2)[2:(end-1)]
573+
θ_curve = range(0, 2π, length = resolution)
574+
for φ in φ_vals
575+
x_ring = [center[1] + radius * sin(φ) * cos(θ) for θ in θ_curve]
576+
y_ring = [center[2] + radius * sin(φ) * sin(θ) for θ in θ_curve]
577+
z_ring = fill(center[3] + radius * cos(φ), resolution)
578+
lines!(ax, x_ring, y_ring, z_ring, color = RGBAf(0.5, 0.5, 0.5, 0.5), transparency = true)
579+
end
566580
end
567581

568582
raw"""
569-
_draw_reference_circles(ax)
583+
_draw_reference_circles!(ax)
570584
571585
Draw the three great circles `(XY, YZ, XZ planes)` on the Bloch sphere.
572586
@@ -575,7 +589,7 @@ Draw the three great circles `(XY, YZ, XZ planes)` on the Bloch sphere.
575589
576590
Adds faint circular guidelines representing the three principal planes.
577591
"""
578-
function _draw_reference_circles(ax)
592+
function _draw_reference_circles!(ax)
579593
wire_color = RGBAf(0.5, 0.5, 0.5, 0.4)
580594
φ = range(0, 2π, length = 100)
581595
# XY, YZ, XZ circles
@@ -590,7 +604,7 @@ function _draw_reference_circles(ax)
590604
end
591605

592606
raw"""
593-
_draw_axes(ax)
607+
_draw_axes!(ax)
594608
595609
Draw the three principal axes `(x, y, z)` of the Bloch sphere.
596610
@@ -599,21 +613,21 @@ Draw the three principal axes `(x, y, z)` of the Bloch sphere.
599613
600614
Creates visible axis lines extending slightly beyond the unit sphere.
601615
"""
602-
function _draw_axes(ax)
616+
function _draw_axes!(ax)
603617
axis_color = RGBAf(0.3, 0.3, 0.3, 0.8)
604618
axis_width = 0.8
605619
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
620+
([Point3f(0, -1.0, 0), Point3f(0, 1.0, 0)], "y"), # Y-axis
621+
([Point3f(-1.0, 0, 0), Point3f(1.0, 0, 0)], "x"), # X-axis
622+
([Point3f(0, 0, -1.0), Point3f(0, 0, 1.0)], "z"), # Z-axis
609623
]
610624
for (points, _) in axes
611625
lines!(ax, points; color = axis_color, linewidth = axis_width)
612626
end
613627
end
614628

615629
raw"""
616-
_plot_points(b::Bloch, ax)
630+
_plot_points!(b::Bloch, ax)
617631
618632
Plot all quantum state points on the Bloch sphere.
619633
@@ -623,7 +637,7 @@ Plot all quantum state points on the Bloch sphere.
623637
624638
Handles both scatter points and line traces based on style specifications.
625639
"""
626-
function _plot_points(b::Bloch, ax)
640+
function _plot_points!(b::Bloch, ax)
627641
for (k, points) in enumerate(b.points)
628642
style = b.point_style[k]
629643
color = b.point_color[k]
@@ -650,7 +664,7 @@ function _plot_points(b::Bloch, ax)
650664
end
651665

652666
raw"""
653-
_plot_lines(b::Bloch, ax)
667+
_plot_lines!(b::Bloch, ax)
654668
655669
Draw all connecting lines between points on the Bloch sphere.
656670
@@ -660,13 +674,14 @@ Draw all connecting lines between points on the Bloch sphere.
660674
661675
Processes line style specifications and color mappings.
662676
"""
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
677+
function _plot_lines!(b::Bloch, ax)
678+
color_map =
679+
Dict("k" => :black, "r" => :red, "g" => :green, "b" => :blue, "c" => :cyan, "m" => :magenta, "y" => :yellow)
680+
for (line, fmt) in b.lines
666681
x, y, z = line
667-
c = get(color_map, first(fmt), :black)
668-
# Determine line style
669-
ls = if occursin("--", fmt)
682+
color_char = first(fmt)
683+
color = get(color_map, color_char, :black)
684+
linestyle = if occursin("--", fmt)
670685
:dash
671686
elseif occursin(":", fmt)
672687
:dot
@@ -675,20 +690,12 @@ function _plot_lines(b::Bloch, ax)
675690
else
676691
:solid
677692
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-
)
693+
lines!(ax, x, y, z; color = color, linewidth = 1.0, linestyle = linestyle)
687694
end
688695
end
689696

690697
raw"""
691-
_plot_arcs(b::Bloch, ax)
698+
_plot_arcs!(b::Bloch, ax)
692699
693700
Draw circular arcs connecting points on the Bloch sphere surface.
694701
@@ -698,7 +705,7 @@ Draw circular arcs connecting points on the Bloch sphere surface.
698705
699706
Calculates great circle arcs between specified points.
700707
"""
701-
function _plot_arcs(b::Bloch, ax)
708+
function _plot_arcs!(b::Bloch, ax)
702709
for arc_pts in b.arcs
703710
length(arc_pts) >= 2 || continue
704711
v1 = normalize(arc_pts[1])
@@ -716,7 +723,7 @@ function _plot_arcs(b::Bloch, ax)
716723
end
717724

718725
raw"""
719-
_plot_vectors(b::Bloch, ax)
726+
_plot_vectors!(b::Bloch, ax)
720727
721728
Draw vectors from origin representing quantum states.
722729
@@ -726,7 +733,7 @@ Draw vectors from origin representing quantum states.
726733
727734
Scales vectors appropriately and adds `3D` arrow markers.
728735
"""
729-
function _plot_vectors(b::Bloch, ax)
736+
function _plot_vectors!(b::Bloch, ax)
730737
isempty(b.vectors) && return
731738
r = 1.0
732739
for (i, v) in enumerate(b.vectors)
@@ -748,7 +755,7 @@ function _plot_vectors(b::Bloch, ax)
748755
end
749756

750757
raw"""
751-
_add_labels(ax)
758+
_add_labels!(ax)
752759
753760
Add axis labels and state labels to the Bloch sphere.
754761
@@ -757,16 +764,16 @@ Add axis labels and state labels to the Bloch sphere.
757764
758765
Positions standard labels `(x, y, |0⟩, |1⟩)` at appropriate locations.
759766
"""
760-
function _add_labels(ax)
767+
function _add_labels!(ax)
761768
label_color = RGBf(0.2, 0.2, 0.2)
762769
label_size = 16
763770
label_font = "TeX Gyre Heros"
764771

765772
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}"),
773+
(Point3f(1.34, -0.1, 0.02), L"\textbf{y}"),
774+
(Point3f(0.07, -1.39, 0.02), L"\textbf{x}"),
775+
(Point3f(-0.08, 0.02, -1.4), L"\mathbf{|1\rangle}"),
776+
(Point3f(-0.08, 0.0, 1.2), L"\mathbf{|0\rangle}"),
770777
]
771778
for (pos, text) in labels
772779
text!(ax, text; position = pos, color = label_color, fontsize = label_size, font = label_font)

0 commit comments

Comments
 (0)