Skip to content

Commit cc03634

Browse files
committed
add kwargs kind for add_states!
1 parent 5927483 commit cc03634

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/visualization.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,14 @@ function add_arc!(
381381
end
382382

383383
@doc raw"""
384-
add_states!(b::Bloch, states::Vector{QuantumObject})
384+
add_states!(b::Bloch, states::Vector{QuantumObject}; kind::Symbol = :vector, kwargs...)
385385
386386
Add one or more quantum states to the Bloch sphere visualization by converting them into Bloch vectors.
387387
388388
# Arguments
389389
- `b::Bloch`: The Bloch sphere object to modify
390390
- `states::Vector{QuantumObject}`: One or more quantum states ([`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref))
391+
- `kind::Symbol`: Type of object to plot (can be either `:vector` or `:point`). Default: `:vector`
391392
392393
# Example
393394
@@ -399,16 +400,18 @@ b = Bloch();
399400
add_states!(b, [x, y, z])
400401
```
401402
"""
402-
function add_states!(b::Bloch, states::Vector{<:QuantumObject})
403+
function add_states!(b::Bloch, states::Vector{<:QuantumObject}; kind::Symbol = :vector, kwargs...)
403404
vecs = map(state -> _state_to_bloch(state), states)
404-
append!(b.vectors, vecs)
405-
return b.vectors
406-
end
407-
408-
function add_states!(b::Bloch, state::QuantumObject)
409-
push!(b.vectors, _state_to_bloch(state))
410-
return b.vectors
405+
if kind == :vector
406+
add_vectors!(b, vecs)
407+
elseif kind == :point
408+
add_points!(b, hcat(vecs...), kwargs...)
409+
else
410+
throw(ArgumentError("Invalid kind = :$kind"))
411+
end
412+
return nothing
411413
end
414+
add_states!(b::Bloch, state::QuantumObject; kind::Symbol = :vector, kwargs...) = add_states!(b, [state], kind = kind, kwargs...)
412415

413416
_state_to_bloch(state::QuantumObject{Ket}) = _ket_to_bloch(state)
414417
_state_to_bloch(state::QuantumObject{Bra}) = _ket_to_bloch(state')

test/ext-test/cpu/makie/makie_ext.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ end
136136
@test isempty(b.vectors)
137137
@test isempty(b.lines)
138138
@test isempty(b.arcs)
139+
139140
b = Bloch()
140141
add_points!(b, hcat([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]))
141142
add_vectors!(b, [[1, 1, 0], [0, 1, 1]])
@@ -149,6 +150,9 @@ end
149150
@test false
150151
@info "Render threw unexpected error" exception=e
151152
end
153+
b.vector_arrowsize = [0.7, 0.8, 1.5] # 1.5 is the length of arrow head (too long)
154+
@test_throws ArgumentError render(b)
155+
152156
b = Bloch()
153157
ψ₁ = normalize(basis(2, 0) + basis(2, 1))
154158
ψ₂ = normalize(basis(2, 0) - im * basis(2, 1))
@@ -167,14 +171,20 @@ end
167171
Pauli_Ops = [sigmax(), sigmay(), sigmaz()]
168172
ψ = rand_ket(2)
169173
ρ = rand_dm(2)
174+
states = [ψ, ρ]
170175
x = basis(2, 0) + basis(2, 1) # unnormalized Ket
171176
ρ1 = 0.3 * rand_dm(2) + 0.4 * rand_dm(2) # unnormalized density operator
172177
ρ2 = Qobj(rand(ComplexF64, 2, 2)) # unnormalized and non-Hermitian Operator
173-
add_states!(b, [ψ, ρ])
174-
@test_logs (:warn,) (:warn,) (:warn,) (:warn,) add_states!(b, [x, ρ1, ρ2])
178+
add_states!(b, states, kind = :vector)
179+
add_states!(b, states, kind = :point)
180+
@test length(b.vectors) == 2
181+
@test length(b.points) == 1
175182
@test all(expect(Pauli_Ops, ψ) .≈ (b.vectors[1]))
176183
@test all(expect(Pauli_Ops, ρ) .≈ (b.vectors[2]))
184+
@test all([b.vectors[j][k] b.points[1][k,j] for j in (1, 2) for k in (1, 2, 3)])
185+
@test_logs (:warn,) (:warn,) (:warn,) (:warn,) add_states!(b, [x, ρ1, ρ2])
177186
@test length(b.vectors) == 5
187+
@test_throws ArgumentError add_states!(b, states, kind = :wrong)
178188

179189
th = range(0, 2π; length = 20)
180190
xp = cos.(th);

0 commit comments

Comments
 (0)