Skip to content

Commit

Permalink
Bloch vector (#103)
Browse files Browse the repository at this point in the history
* bloch vector with tests

* version bump
  • Loading branch information
lpawela committed May 2, 2023
1 parent 7644f36 commit 548455c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumInformation"
uuid = "3c0b384b-479c-5684-b2ef-9d7a46dd931e"
authors = ["Dariusz Kurzyk <dkurzyk@iitis.pl>", "Łukasz Pawela <lpawela@iitis.pl>", "Piotr Gawron <gawron@iitis.pl>", "Marcin Przewięźlikowski <m.przewie@gmail.com>"]
version = "0.4.9"
version = "0.4.10"

[deps]
Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
Expand Down
14 changes: 13 additions & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export ket, bra, ketbra, proj, res, unres, max_mixed, max_entangled,
export ket, bra, ketbra, proj, bloch_vector, res, unres, max_mixed, max_entangled,
werner_state, permutesystems

function ket(::Type{T}, val::Int, dim::Int) where T<:AbstractVector{<:Number}
Expand Down Expand Up @@ -75,7 +75,19 @@ Return outer product \$|ket\\rangle\\langle ket|\$ of `ket`.
"""
proj::AbstractVector{<:Number}) = ψ * ψ'

"""
$(SIGNATURES)
- `ρ`: input qubit density matrix.
Return the Bloch vector corresponding to the inpu quit state.
"""
function bloch_vector::AbstractMatrix{T}) where {T <: Number}
@assert size(ρ) == (2, 2)
x = 2real(ρ[1, 2])
y = 2imag(ρ[1, 2])
z = 2real(ρ[1, 1]) - 1
T[x, y, z]
end

"""
$(SIGNATURES)
Expand Down
27 changes: 27 additions & 0 deletions test/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ end
@test norm(ϕϕ - ψψ) 0.
end

@testset "bloch_vector" begin
@testset "Test 1: Identity matrix" begin
ρ = Matrix{ComplexF64}(I, 2, 2)
@test bloch_vector(ρ) ComplexF64[0.0, 0.0, 1.0]
end

@testset "Test 2: X basis state" begin
ρ = [0.5 0.5; 0.5 0.5]
@test bloch_vector(ρ) Float64[1.0, 0.0, 0.0]
end

@testset "Test 3: Y basis state" begin
ρ = [0.5 0.5im; -0.5im 0.5]
@test bloch_vector(ρ) ComplexF64[0.0, 1.0, 0.0]
end

@testset "Test 4: Z basis state" begin
ρ = [1.0 0.0; 0.0 0.0]
@test bloch_vector(ρ) Float64[0.0, 0.0, 1.0]
end

@testset "Test 5: Arbitrary density matrix" begin
ρ = [0.6 0.3-0.2im; 0.3+0.2im 0.4]
@test bloch_vector(ρ) ComplexF64[0.6, -0.4, 0.2]
end
end

@testset "res" begin
ρ = [0.25 0.25im; -0.25im 0.75]
ϕ = res(ρ)
Expand Down

2 comments on commit 548455c

@lpawela
Copy link
Member Author

@lpawela lpawela commented on 548455c May 2, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/82701

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.10 -m "<description of version>" 548455c58b2d37ae8fb9284b829c4b758177d926
git push origin v0.4.10

Please sign in to comment.