@@ -12,12 +12,15 @@ export operator_to_iso_vec
1212export iso_operator_to_iso_vec
1313export iso_operator_to_operator
1414export operator_to_iso_operator
15+ export ket_to_bloch
16+ export bloch_to_ket
1517
1618# Do not export Hamiltonian isomorphisms
1719
1820using LinearAlgebra
1921using SparseArrays
2022using TestItems
23+ using .. Gates
2124
2225
2326@doc raw """
@@ -249,6 +252,51 @@ function var_G(
249252 return G_0 + G_V
250253end
251254
255+ # ----------------------------------------------------------------------------- #
256+ # Bloch Sphere #
257+ # ----------------------------------------------------------------------------- #
258+
259+ @doc raw """
260+ ket_to_bloch(ψ::AbstractVector{<:Number})
261+
262+ Convert a ket to a Bloch vector representation.
263+ """
264+ function ket_to_bloch (ψ:: AbstractVector{<:Number} )
265+ @assert length (ψ) == 2
266+ ρ = ψ * ψ'
267+ bloch_vector = [real (tr (ρ * P)) for P in [PAULIS. X, PAULIS. Y, PAULIS. Z]]
268+
269+ return bloch_vector / norm (bloch_vector)
270+ end
271+
272+ @doc raw """
273+ bloch_to_ket(v::AbstractVector{<:Real}; digits=6)
274+
275+
276+ Convert a Bloch vector to a ket (up to global phase).
277+ """
278+ function bloch_to_ket (bloch:: AbstractVector{R} ; digits:: Integer = 6 ) where R <: Real
279+ @assert length (bloch) == 3
280+ x, y, z = bloch
281+
282+ θ = acos (z)
283+ φ = atan (y, x)
284+
285+ return Complex{R}[cos (θ/ 2 ), exp (im * φ) * sin (θ/ 2 )]
286+
287+ end
288+
289+ function density_to_bloch (ψ:: AbstractVector{<:Number} )
290+ @assert length (ψ) == 2
291+ ρ = ψ * ψ'
292+ return [real (tr (ρ * P)) for P in [PAULIS. X, PAULIS. Y, PAULIS. Z]]
293+ end
294+
295+ function bloch_to_density (v:: AbstractVector{<:Real} )
296+ @assert length (v) == 3
297+ return 0.5 * (I (2 ) + v[1 ]* PAULIS. X + v[2 ]* PAULIS. Y + v[3 ]* PAULIS. Z)
298+ end
299+
252300# *************************************************************************** #
253301
254302@testitem " Test ket isomorphisms" begin
354402 1.0 1.0 0.0 0.0 3.0 4.0 ]
355403end
356404
405+ @testitem " Test Bloch vector to ket and ket to Bloch vector" begin
406+ using LinearAlgebra: dot
407+ using PiccoloQuantumObjects: Isomorphisms. ket_to_bloch, Isomorphisms. bloch_to_ket
408+
409+ ψ₁ = [1.0 , 0.0 ]
410+ ψ₂ = [0.0 , 1.0 ]
411+ ψ₃ = [1 / √ 2 , 1 / √ 2 ]
412+ ψ₄ = [1 / √ 2 , - 1im / √ 2 ]
413+
414+ for ψ in (ψ₁, ψ₂, ψ₃, ψ₄)
415+ bloch = ket_to_bloch (ψ)
416+ ψ′ = bloch_to_ket (bloch)
417+ @test abs2 (dot (ψ′, ψ)) ≈ 1.0 atol= 1e-10
418+ end
419+ end
420+
357421end
0 commit comments