Skip to content

Commit 9482c63

Browse files
committed
add codereview suggestions
1 parent 00266d5 commit 9482c63

File tree

4 files changed

+164
-262
lines changed

4 files changed

+164
-262
lines changed

Project.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,22 @@ StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
2626
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
2727

2828
[weakdeps]
29-
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
3029
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
3130
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3231
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
3332
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
34-
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
3533
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
3634

3735
[extensions]
3836
QuantumToolboxCUDAExt = "CUDA"
3937
QuantumToolboxChainRulesCoreExt = "ChainRulesCore"
4038
QuantumToolboxGPUArraysExt = ["GPUArrays", "KernelAbstractions"]
41-
QuantumToolboxMakieExt = ["Colors", "LaTeXStrings", "Makie",]
39+
QuantumToolboxMakieExt = "Makie"
4240

4341
[compat]
4442
ArrayInterface = "6, 7"
4543
CUDA = "5"
4644
ChainRulesCore = "1"
47-
Colors = "0.12, 0.13"
4845
DiffEqBase = "6"
4946
DiffEqCallbacks = "4.2.1 - 4"
5047
DiffEqNoiseProcess = "5"
@@ -54,7 +51,6 @@ GPUArrays = "10, 11"
5451
Graphs = "1.7"
5552
IncompleteLU = "0.2"
5653
KernelAbstractions = "0.9.2"
57-
LaTeXStrings = "1.4.0"
5854
LinearAlgebra = "1"
5955
LinearSolve = "2, 3"
6056
Makie = "0.20, 0.21, 0.22"

ext/QuantumToolboxMakieExt.jl

Lines changed: 33 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -296,123 +296,52 @@ _figFromChildren(::Nothing) = throw(ArgumentError("No Figure has been found at t
296296
"""
297297
_state_to_bloch(state::QuantumObject{<:Ket}) -> Vector{Float64}
298298
299-
Convert a quantum state (Ket) to its Bloch vector representation.
300-
301-
For a 2-level system (qubit), the Bloch vector components are calculated as:
302-
r_i = ⟨ψ|σ_i|ψ⟩
303-
where σ_i are the Pauli matrices.
304-
305-
For higher-dimensional systems, projects onto the generalized Bloch sphere.
299+
Convert a quantum state (Ket) to its Bloch vector representation for a qubit.
306300
"""
307301
function _state_to_bloch(state::QuantumObject{<:Ket})
308-
if !isapprox(norm(state), 1.0, atol = 1e-6)
302+
if !isapprox(norm(state), 1.0, atol=1e-6)
309303
@warn "State is not normalized. Normalizing before Bloch vector conversion."
310304
state = normalize(state)
311305
end
312-
N = length(state)
313-
if N == 2 # Qubit case
314-
ψ = state.data
315-
x = 2 * real(ψ[1] * conj(ψ[2]))
316-
y = 2 * imag(ψ[1] * conj(ψ[2]))
317-
z = abs2(ψ[1]) - abs2(ψ[2])
318-
return [x, y, z]
319-
else
320-
return _higher_dim_bloch_vector(state)
321-
end
322-
end
323-
324-
"""
325-
_higher_dim_bloch_vector(state::QuantumObject{<:Ket}) -> Vector{Float64}
326-
327-
Compute the generalized Bloch vector for higher-dimensional systems using Gell-Mann basis.
328-
"""
329-
function _higher_dim_bloch_vector(state::QuantumObject{<:Ket})
330-
N = length(state)
306+
331307
ψ = state.data
332-
# Number of generalized Bloch vector components: N²-1
333-
bloch_vec = zeros(Float64, N^2 - 1)
334-
# Symmetric (off-diagonal) components
335-
idx = 1
336-
for j in 1:N
337-
for k in (j+1):N
338-
bloch_vec[idx] = 2 * real(ψ[j] * conj(ψ[k]))
339-
bloch_vec[idx+1] = 2 * imag(ψ[j] * conj(ψ[k]))
340-
idx += 2
341-
end
308+
if length(ψ) != 2
309+
error("Bloch sphere visualization is only supported for qubit states (2-level systems)")
342310
end
343-
# Diagonal components
344-
for l in 2:N
345-
for m in 1:(l-1)
346-
bloch_vec[idx] = sqrt(2/(l*(l-1))) * (abs2(ψ[m]) - (l-1)*abs2(ψ[l]))
347-
idx += 1
348-
end
349-
end
350-
return bloch_vec
351-
end
352-
353-
"""
354-
plot_bloch(::Val{:Makie}, state::QuantumObject{<:Union{Ket,Bra}}; kwargs...)
355-
356-
Plot the state on a Bloch sphere using Makie.jl.
357-
358-
# Arguments
359-
360-
- `state`: Quantum state to visualize (must be a Ket or Bra)
361-
- `show_axes`: Whether to show x/y/z axes (default: true)
362-
- `show_labels`: Whether to show axis labels (default: true)
363-
- `sphere_alpha`: Transparency of the sphere (default: 0.1)
364-
- `vector_color`: Color of the state vector (default: :red)
365-
- `kwargs...`: Additional arguments passed to the Bloch sphere renderer
366-
367-
# Returns
368-
369-
- `fig`: The Makie Figure object
370-
- `ax`: The Axis3 object
371-
- `bloch`: The Bloch sphere object
372-
"""
373-
function QuantumToolbox.plot_bloch(::Val{:Makie}, state::QuantumObject{<:Union{Ket,Bra}}; kwargs...)
374-
state = isbra(state) ? dag(state) : state
375-
bloch_vec = _state_to_bloch(state)
376-
return _render_bloch_makie(bloch_vec; kwargs...)
311+
312+
x = 2 * real(ψ[1] * conj(ψ[2]))
313+
y = 2 * imag(ψ[1] * conj(ψ[2]))
314+
z = abs2(ψ[1]) - abs2(ψ[2])
315+
return [x, y, z]
377316
end
378317

379318
"""
380319
_dm_to_bloch(ρ::QuantumObject{<:Operator}) -> Vector{Float64}
381320
382-
Convert a density matrix to its Bloch vector representation.
383-
For qubits, uses Pauli matrices. For higher dimensions, uses generalized Gell-Mann basis.
321+
Convert a density matrix to its Bloch vector representation for a qubit.
384322
"""
385323
function _dm_to_bloch::QuantumObject{<:Operator})
386324
if !ishermitian(ρ)
387325
@warn "Density matrix is not Hermitian. Results may not be meaningful."
388326
end
389-
N = size(ρ, 1)
390-
391-
if N == 2 # Qubit case
392-
σx = sigmax()
393-
σy = sigmay()
394-
σz = sigmaz()
395-
x = real(expect(σx, ρ))
396-
y = real(expect(σy, ρ))
397-
z = real(expect(σz, ρ))
398-
return [x, y, z]
399-
else
400-
return _higher_dim_bloch_vector(ρ)
327+
328+
if size(ρ, 1) != 2
329+
error("Bloch sphere visualization is only supported for qubit states (2-level systems)")
401330
end
402-
end
403331

404-
function QuantumToolbox.plot_bloch(::Val{:Makie}, ρ::QuantumObject{<:Operator}; kwargs...)
405-
bloch_vec = _dm_to_bloch(ρ)
406-
return _render_bloch_makie(bloch_vec; kwargs...)
332+
x = real(ρ[1,2] + ρ[2,1])
333+
y = imag(ρ[2,1] - ρ[1,2])
334+
z = real(ρ[1,1] - ρ[2,2])
335+
return [x, y, z]
407336
end
408337

409338
function _render_bloch_makie(
410339
bloch_vec::Vector{Float64};
411-
location = nothing,
412-
show_axes = true,
413-
show_labels = true,
414-
sphere_alpha = 0.1,
415-
vector_color = :red,
340+
location=nothing,
341+
show_axes=true,
342+
show_labels=true,
343+
sphere_alpha=0.1,
344+
vector_color=:red,
416345
kwargs...,
417346
)
418347
b = Bloch()
@@ -424,35 +353,19 @@ function _render_bloch_makie(
424353
add_vectors!(b, bloch_vec)
425354

426355
fig, location = _getFigAndLocation(location)
427-
fig, ax = render(b; location = location, kwargs...)
428-
return fig, ax
356+
fig, ax = render(b; location=location, kwargs...)
357+
return fig, ax, b
429358
end
430359

431-
"""
432-
_higher_dim_bloch_vector(ρ::QuantumObject{<:Operator}) -> Vector{Float64}
360+
function QuantumToolbox.plot_bloch(::Val{:Makie}, state::QuantumObject{<:Union{Ket,Bra}}; kwargs...)
361+
state = isbra(state) ? dag(state) : state
362+
bloch_vec = _state_to_bloch(state)
363+
return _render_bloch_makie(bloch_vec; kwargs...)
364+
end
433365

434-
Compute the generalized Bloch vector for higher-dimensional density matrices.
435-
"""
436-
function _higher_dim_bloch_vector::QuantumObject{<:Operator})
437-
N = size(ρ, 1)
438-
bloch_vec = zeros(Float64, N^2 - 1)
439-
# Symmetric (off-diagonal) components
440-
idx = 1
441-
for j in 1:N
442-
for k in (j+1):N
443-
bloch_vec[idx] = real(ρ[j, k] + ρ[k, j])
444-
bloch_vec[idx+1] = imag(ρ[j, k] - ρ[k, j])
445-
idx += 2
446-
end
447-
end
448-
# Diagonal components
449-
for l in 2:N
450-
for m in 1:(l-1)
451-
bloch_vec[idx] = sqrt(2/(l*(l-1))) * (real(ρ[m, m]) - (l-1)*real(ρ[l, l]))
452-
idx += 1
453-
end
454-
end
455-
return bloch_vec
366+
function QuantumToolbox.plot_bloch(::Val{:Makie}, ρ::QuantumObject{<:Operator}; kwargs...)
367+
bloch_vec = _dm_to_bloch(ρ)
368+
return _render_bloch_makie(bloch_vec; kwargs...)
456369
end
457370

458371
end

0 commit comments

Comments
 (0)