You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(length(p1) !=3||length(p2) !=3) &&throw(ArgumentError("Points must be 3D vectors"))
276
259
x = [p1[2], p2[2]]
277
260
y = [-p1[1], -p2[1]]
278
261
z = [p1[3], p2[3]]
@@ -283,7 +266,7 @@ end
283
266
@docraw"""
284
267
add_line!(
285
268
b::Bloch,
286
-
start_point_point::QuantumObject,
269
+
start_point::QuantumObject,
287
270
end_point::QuantumObject;
288
271
fmt = "k"
289
272
)
@@ -293,8 +276,8 @@ Add a line between two quantum states on the Bloch sphere visualization.
293
276
# Arguments
294
277
295
278
- `b::Bloch`: The Bloch sphere object to modify.
296
-
- `start_point_point::QuantumObject`: The start_point_pointing quantum state or operator. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
297
-
- `end_point::QuantumObject`: The ending quantum state or operator. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
279
+
- `start_point::QuantumObject`: The starting quantum state. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
280
+
- `end_point::QuantumObject`: The ending quantum state. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
298
281
- `fmt::String="k"`: (optional) A format string specifying the line style and color (default is black `"k"`).
299
282
300
283
# Description
@@ -310,14 +293,14 @@ b = Bloch()
310
293
add_line!(b, ψ₁, ψ₂; fmt = "r--")
311
294
```
312
295
"""
313
-
functionQuantumToolbox.add_line!(
296
+
functionadd_line!(
314
297
b::Bloch,
315
-
p1::QuantumObject{OpType1},
316
-
p2::QuantumObject{OpType2};
298
+
start_point::QuantumObject{OpType1},
299
+
end_point::QuantumObject{OpType2};
317
300
fmt ="k",
318
301
) where {OpType1<:Union{Ket,Bra,Operator},OpType2<:Union{Ket,Bra,Operator}}
@@ -401,16 +386,17 @@ A 3-element `Vector{Float64}` representing the Bloch vector `[x, y, z]`.
401
386
- `ArgumentError` if the state dimension is not 2.
402
387
"""
403
388
function_ket_to_bloch(state::QuantumObject{Ket})
389
+
(size(state) == (2,)) ||
390
+
throw(ArgumentError("Bloch sphere visualization is only supported for qubit states (2-level systems)"))
391
+
404
392
state_norm =norm(state)
405
393
if!isapprox(state_norm, 1.0, atol =1e-6)
406
394
@warn"State is not normalized. Normalizing before Bloch vector conversion."
407
395
ψ = state.data / state_norm
408
396
else
409
397
ψ = state.data
410
398
end
411
-
iflength(ψ) !=2
412
-
error("Bloch sphere visualization is only supported for qubit states (2-level systems)")
413
-
end
399
+
414
400
x =2*real(ψ[1] *conj(ψ[2]))
415
401
y =2*imag(ψ[1] *conj(ψ[2]))
416
402
z =abs2(ψ[1]) -abs2(ψ[2])
@@ -434,17 +420,15 @@ A 3-element `Vector{Float64}` representing the Bloch vector `[x, y, z]`.
434
420
- `ArgumentError` if the matrix dimension is not 2.
435
421
"""
436
422
function_dm_to_bloch(ρ::QuantumObject{Operator})
437
-
if!ishermitian(ρ)
438
-
@warn"Density matrix is not Hermitian. Results may not be meaningful."
439
-
end
440
-
ifsize(ρ, 1) !=2
441
-
error("Bloch sphere visualization is only supported for qubit states (2-level systems)")
442
-
end
423
+
(size(ρ) == (2, 2)) ||
424
+
throw(ArgumentError("Bloch sphere visualization is only supported for qubit states (2-level systems)"))
443
425
444
-
state_norm =norm(state)
426
+
ishermitian(ρ) || (@warn"Density matrix is not Hermitian. Results may not be meaningful.")
427
+
428
+
state_norm =norm(ρ)
445
429
if!isapprox(state_norm, 1.0, atol =1e-6)
446
430
@warn"State is not normalized. Normalizing before Bloch vector conversion."
447
-
ρ2 =ρ2/ state_norm
431
+
ρ2 =ρ/ state_norm
448
432
else
449
433
ρ2 = ρ
450
434
end
@@ -519,27 +503,10 @@ The `library` keyword argument specifies the plotting backend to use. The defaul
519
503
!!! warning "Beware of type-stability!"
520
504
For improved performance and type-stability, prefer passing `Val(:Makie)` instead of `:Makie`. See [Performance Tips](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) for details.
521
505
"""
522
-
plot_bloch(state::QuantumObject{OpType}; library::Union{Symbol,Val}=Val(:Makie), kwargs...) where {OpType<:Union{Ket,Bra,Operator}} =plot_bloch(makeVal(lib_val), state; kwargs...)
523
-
524
-
@docraw"""
525
-
plot_bloch(::Val{T}, state::QuantumObject; kwargs...) where {T}
526
-
527
-
Fallback implementation for unsupported plotting backends.
528
-
529
-
# Arguments
530
-
- `::Val{T}`: The unsupported backend specification.
531
-
- `state::QuantumObject`: The quantum state that was attempted to be plotted.
532
-
- `kwargs...`: Ignored keyword arguments.
533
-
534
-
# Throws
535
-
- `ErrorException`: Always throws an error indicating the backend `T` is unsupported.
536
-
537
-
# Note
538
-
This function serves as a fallback when an unsupported backend is requested. Currently supported backends include:
539
-
- `:Makie` (using `Makie.jl`)
540
-
541
-
See the main `plot_bloch` documentation for supported backends.
542
-
"""
543
-
functionplot_bloch(::Val{T}, state::QuantumObject{OpType}; kwargs...) where {T,OpType<:Union{Ket,Bra,Operator}}
544
-
returnerror("Unsupported backend: $T. Try :Makie or another supported library.")
545
-
end
506
+
plot_bloch(
507
+
state::QuantumObject{OpType};
508
+
library::Union{Symbol,Val}=Val(:Makie),
509
+
kwargs...,
510
+
) where {OpType<:Union{Ket,Bra,Operator}} =plot_bloch(makeVal(library), state; kwargs...)
511
+
plot_bloch(::Val{T}, state::QuantumObject{OpType}; kwargs...) where {T,OpType<:Union{Ket,Bra,Operator}} =
512
+
throw(ArgumentError("The specified plotting library $T is not available. Try running `using $T` first."))
0 commit comments