Skip to content

isomorphisms for bloch plotting #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/isomorphisms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export operator_to_iso_vec
export iso_operator_to_iso_vec
export iso_operator_to_operator
export operator_to_iso_operator
export ket_to_bloch
export bloch_to_ket

# Do not export Hamiltonian isomorphisms

Expand Down Expand Up @@ -249,6 +251,44 @@ function var_G(
return G_0 + G_V
end

# ----------------------------------------------------------------------------- #
# Bloch Sphere #
# ----------------------------------------------------------------------------- #

@doc raw"""
ket_to_bloch(ψ::AbstractVector{<:Number})

Convert a ket to a Bloch vector representation.
"""
function ket_to_bloch(ψ::AbstractVector{<:Number})
@assert length(ψ) == 2
ρ = ψ * ψ'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a quick assert for the size we expect?

PAULIS = (
X = [0 1; 1 0],
Y = [0 -im; im 0],
Z = [1 0; 0 -1],
)
bloch_vector = [real(tr(ρ * P)) for P in [PAULIS.X, PAULIS.Y, PAULIS.Z]]

return bloch_vector / norm(bloch_vector)
end

@doc raw"""
bloch_to_ket(v::AbstractVector{<:Real}; digits=6)


Convert a Bloch vector to a ket (up to global phase).
"""
function bloch_to_ket(bloch::AbstractVector{R}; digits::Integer=6) where R <: Real
@assert length(bloch) == 3
x, y, z = bloch

θ = acos(z)
φ = atan(y, x)

return Complex{R}[cos(θ/2), exp(im * φ) * sin(θ/2)]

end
# *************************************************************************** #

@testitem "Test ket isomorphisms" begin
Expand Down Expand Up @@ -354,4 +394,20 @@ end
1.0 1.0 0.0 0.0 3.0 4.0]
end

@testitem "Test Bloch vector to ket and ket to Bloch vector" begin
using LinearAlgebra: dot
using PiccoloQuantumObjects: Isomorphisms.ket_to_bloch, Isomorphisms.bloch_to_ket

ψ₁ = [1.0, 0.0]
ψ₂ = [0.0, 1.0]
ψ₃ = [1 / √2, 1 / √2]
ψ₄ = [1 / √2, -1im / √2]

for ψ in (ψ₁, ψ₂, ψ₃, ψ₄)
bloch = ket_to_bloch(ψ)
ψ′ = bloch_to_ket(bloch)
@test abs2(dot(ψ′, ψ)) ≈ 1.0 atol=1e-10
end
end

end