Skip to content

Commit

Permalink
add schmidt decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoms committed Jun 29, 2024
1 parent a5d38ed commit eee8b5e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ binary_relative_entropy
conditional_entropy
```

## Entanglement

```@docs
schmidt_decomposition
```

## Measurements

```@docs
Expand Down
1 change: 1 addition & 0 deletions src/Ket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include("measurements.jl")
include("entropy.jl")
include("norms.jl")
include("partial_tra.jl")
include("entanglement.jl")

import Requires
function __init__()
Expand Down
25 changes: 25 additions & 0 deletions src/entanglement.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
schmidt_decomposition(ψ::AbstractVector, dims::AbstractVector{<:Integer})
Produces the Schmidt decomposition of `ψ` with subsystem dimensions `dims`. Returns the (sorted) Schmidt coefficients λ and isometries U, V such that
kron(U', V')*`ψ` is of Schmidt form.
"""
function schmidt_decomposition::AbstractVector, dims::AbstractVector{<:Integer})
length(dims) != 2 && throw(ArgumentError("Two subsystem sizes must be specified."))
m = transpose(reshape(ψ, dims[2], dims[1])) #necessary because the natural reshaping would be row-major, but Julia does it col-major
U, λ, V = LA.svd(m)
return λ, U, conj(V)
end
"""
schmidt_decomposition(ψ::AbstractVector, dims::AbstractVector{<:Integer})
Produces the Schmidt decomposition of `ψ` assuming equally-sized subsystems. Returns the (sorted) Schmidt coefficients λ and isometries U, V such that
kron(U', V')*`ψ` is of Schmidt form.
"""
function schmidt_decomposition::AbstractVector)
n = length(ψ)
d = isqrt(n)
d^2 != n && throw(ArgumentError("Subsystems are not equally-sized, please specify sizes."))
return schmidt_decomposition(ψ, [d, d])
end
export schmidt_decomposition
2 changes: 1 addition & 1 deletion test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@test gell_mann(Complex{R}, 3, 1) == [0 0 -im; 0 0 0; im 0 0]
@test gell_mann(Complex{R}, 3, 2) == [0 0 0; 0 0 -im; 0 im 0]
end
@test gell_mann(3, 3) == Diagonal([1/sqrt(3), 1/sqrt(3), -2/sqrt(3)])
@test gell_mann(3, 3) == Diagonal([1, 1, -2] / sqrt(3))
@test gell_mann(1, 1, 4) == Matrix{Float64}(I, 4, 4)
end
@testset "Cleanup" begin
Expand Down
11 changes: 11 additions & 0 deletions test/entanglement.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testset "Entanglement " begin
for R in [Float64, Double64, Float128, BigFloat]
T = Complex{R}
ψ = random_state_vector(T, 6)
λ, U, V = schmidt_decomposition(ψ, [2, 3])
@test vec(Diagonal(λ)) kron(U', V') * ψ
ψ = random_state_vector(T, 4)
λ, U, V = schmidt_decomposition(ψ)
@test vec(Diagonal(λ)) kron(U', V') * ψ
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Test

include("basic.jl")
include("entropy.jl")
include("entanglement.jl")
include("measurements.jl")
include("nonlocal.jl")
include("norms.jl")
Expand Down

0 comments on commit eee8b5e

Please sign in to comment.