@@ -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"""
307301function _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]
377316end
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"""
385323function _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]
407336end
408337
409338function _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
429358end
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... )
456369end
457370
458371end
0 commit comments