Skip to content
Open
Changes from 1 commit
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
53 changes: 53 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,42 @@ 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})
ρ = ψ * ψ'
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?

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{<:Number}; digits::Integer=6)
x, y, z = bloch # extract bloch vector components

# compute angles
θ = acos(z) # polar angle
φ = atan(y, x) # azimuthal angle

# construct the quantum state
ket = [cos(θ/2), exp(im * φ) * sin(θ/2)]

ket = map(c -> round(real(c), digits=digits) + (isreal(c) ? 0.0 : im * round(imag(c), digits=digits)), ket)
Copy link
Member

Choose a reason for hiding this comment

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

The bloch vector should be real values, so try:
bloch_to_ket(bloch::AbstractVector{R}) where R <: Real

and then we can take a quick shortcut ket = Complex{R}[cos(θ/2), exp(im * φ) * sin(θ/2)]

Copy link
Member

Choose a reason for hiding this comment

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

also, any reason for needing to round to some specific digits?

Copy link
Author

Choose a reason for hiding this comment

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

this was just to get exactly 0 or exactly 1, etc when converting, not exactly necessary though, should I delete?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah let's switch to the alternative implementation suggested above (I think we'll just end up with tiny numerical errors for any plotting applications, which shouldn't matter)


return ket
end
# *************************************************************************** #

@testitem "Test ket isomorphisms" begin
Expand Down Expand Up @@ -354,4 +392,19 @@ 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 PiccoloQuantumObjects.Isomorphisms: ket_to_bloch, 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
Loading