- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -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 | ||
|  | ||
|  | @@ -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}) | ||
| ρ = ψ * ψ' | ||
| 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) | ||
|          | ||
|  | ||
| return ket | ||
| end | ||
| # *************************************************************************** # | ||
|  | ||
| @testitem "Test ket isomorphisms" begin | ||
|  | @@ -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 | ||
There was a problem hiding this comment.
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?