-
Notifications
You must be signed in to change notification settings - Fork 5
Exponential #94
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
sanderdemeyer
wants to merge
25
commits into
QuantumKitHub:main
Choose a base branch
from
sanderdemeyer:exponential
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+472
−12
Open
Exponential #94
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
eb913cb
implement exponential
sanderdemeyer a3dc04d
update on exponential
sanderdemeyer c4564ee
Merge branch 'QuantumKitHub:main' into exponential
sanderdemeyer 8dc3ecd
remove comment
sanderdemeyer d9fb748
Merge branch 'exponential' of https://github.com/sanderdemeyer/Matrix…
sanderdemeyer 5095cdb
comments
sanderdemeyer 89dfa23
change name of decompositions.jl to matrixfunctions.jl
sanderdemeyer 996ecb5
revert name change
sanderdemeyer dc78eb0
Merge branch 'main' into exponential
sanderdemeyer f220035
general comments
sanderdemeyer c68afad
bug fix
sanderdemeyer 95ddb06
avoid allocation in diagonal case
sanderdemeyer 5d6f4f3
Merge branch 'main' into exponential
sanderdemeyer c8e811c
include exponentiali(tau, A)
sanderdemeyer 0229417
remove simple test case and make the test more general
sanderdemeyer cbbf813
fix formatting
sanderdemeyer d08d545
add docs
sanderdemeyer 720ada5
remove a bunch of allocations and clean up
lkdvos d738c22
Merge branch 'main' into exponential
lkdvos be111ea
introduce `map_diagonal` to simplify and relax types
lkdvos c760a47
rework tests
lkdvos d0d14e1
revert wrong filename changes
lkdvos cf98bd4
avoid running non-GPU tests through buildkite
lkdvos 1536eb4
correct wrong in-place assumptions
lkdvos 349800e
fixes part II
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # Inputs | ||
| # ------ | ||
| function copy_input(::typeof(exponential), A::AbstractMatrix) | ||
| return copy!(similar(A, float(eltype(A))), A) | ||
| end | ||
|
|
||
| copy_input(::typeof(exponential), A::Diagonal) = copy(A) | ||
|
|
||
| function copy_input(::typeof(exponentiali), τ::Number, A::AbstractMatrix) | ||
| return τ, copy!(similar(A, complex(eltype(A))), A) | ||
| end | ||
|
|
||
| copy_input(::typeof(exponentiali), τ::Number, A::Diagonal) = τ, copy(A) | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| m, n = size(A) | ||
| m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)")) | ||
| @check_size(expA, (m, m)) | ||
| return @check_scalar(expA, A) | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh) | ||
| if !ishermitian(A) | ||
| throw(DomainError(A, "Hermitian matrix was expected. Use `project_hermitian` to project onto the nearest hermitian matrix)")) | ||
| end | ||
| m, n = size(A) | ||
| m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)")) | ||
| @check_size(expA, (m, m)) | ||
| return @check_scalar(expA, A) | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm) | ||
| m, n = size(A) | ||
| @assert m == n && isdiag(A) | ||
| @assert expA isa Diagonal | ||
| @check_size(expA, (m, m)) | ||
| @check_scalar(expA, A) | ||
| return nothing | ||
| end | ||
|
|
||
| function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm) | ||
| m, n = size(A) | ||
| m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)")) | ||
| return @check_size(expA, (m, m)) | ||
| end | ||
|
|
||
| function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh) | ||
| if !ishermitian(A) | ||
| throw(DomainError(A, "Hermitian matrix was expected. Use `project_hermitian` to project onto the nearest hermitian matrix)")) | ||
| end | ||
|
Comment on lines
+48
to
+50
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here |
||
| m, n = size(A) | ||
| m == n || throw(DimensionMismatch("square input matrix expected. Got ($m,$n)")) | ||
| return @check_size(expA, (m, m)) | ||
| end | ||
|
|
||
| function check_input(::typeof(exponentiali!), A::AbstractMatrix, expA::AbstractMatrix, ::DiagonalAlgorithm) | ||
| m, n = size(A) | ||
| @assert m == n && isdiag(A) | ||
| @assert expA isa Diagonal | ||
| return @check_size(expA, (m, m)) | ||
| end | ||
|
|
||
| # Outputs | ||
| # ------- | ||
| initialize_output(::typeof(exponential!), A::AbstractMatrix, ::AbstractAlgorithm) = A | ||
| initialize_output(::typeof(exponentiali!), τ::Number, A::AbstractMatrix, ::AbstractAlgorithm) = | ||
| complex(A) | ||
|
|
||
| # Implementation | ||
| # -------------- | ||
| function exponential!(A, expA, alg::MatrixFunctionViaLA) | ||
| check_input(exponential!, A, expA, alg) | ||
| return LinearAlgebra.exp!(A) | ||
| end | ||
|
|
||
| function exponential!(A, expA, alg::MatrixFunctionViaEigh) | ||
| check_input(exponential!, A, expA, alg) | ||
| D, V = eigh_full!(A, alg.eigh_alg) | ||
| expD = map_diagonal!(x -> exp(x / 2), D, D) | ||
| VexpD = rmul!(V, expD) | ||
| return mul!(expA, VexpD, V') | ||
| end | ||
|
|
||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEig) | ||
| check_input(exponential!, A, expA, alg) | ||
| D, V = eig_full!(A, alg.eig_alg) | ||
| expD = map_diagonal!(exp, D, D) | ||
| iV = inv(V) | ||
| VexpD = rmul!(V, expD) | ||
| if eltype(A) <: Real | ||
| expA .= real.(VexpD * iV) | ||
| else | ||
| mul!(expA, VexpD, iV) | ||
| end | ||
| return expA | ||
| end | ||
|
|
||
| function exponentiali!(τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaLA) | ||
| check_input(exponentiali!, A, expA, alg) | ||
| expA .= A .* (im * τ) | ||
| return LinearAlgebra.exp!(expA) | ||
| end | ||
|
|
||
| function exponentiali!(τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaEigh) | ||
| check_input(exponentiali!, A, expA, alg) | ||
| D, V = eigh_full!(A, alg.eigh_alg) | ||
| expD = map_diagonal(x -> exp(x * im * τ), D) | ||
| if eltype(A) <: Real | ||
| VexpD = V * expD | ||
| return expA .= real.(VexpD * V') | ||
| else | ||
| VexpD = rmul!(V, expD) | ||
| return mul!(expA, VexpD, V') | ||
| end | ||
| end | ||
|
|
||
| function exponentiali!(τ::Number, A, expA, alg::MatrixFunctionViaEig) | ||
| check_input(exponentiali!, A, expA, alg) | ||
| D, V = eig_full!(A, alg.eig_alg) | ||
| expD = map_diagonal!(x -> exp(x * im * τ), D, D) | ||
| iV = inv(V) | ||
| VexpD = rmul!(V, expD) | ||
| return mul!(expA, VexpD, iV) | ||
| end | ||
|
|
||
| # Diagonal logic | ||
| # -------------- | ||
| function exponential!(A, expA, alg::DiagonalAlgorithm) | ||
| check_input(exponential!, A, expA, alg) | ||
| return map_diagonal!(exp, expA, A) | ||
| end | ||
|
|
||
| function exponentiali!(τ::Number, A, expA, alg::DiagonalAlgorithm) | ||
| check_input(exponentiali!, A, expA, alg) | ||
| return map_diagonal!(x -> exp(x * im * τ), expA, A) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Exponential functions | ||
| # -------------- | ||
|
|
||
| """ | ||
| exponential(A; kwargs...) -> expA | ||
| exponential(A, alg::AbstractAlgorithm) -> expA | ||
| exponential!(A, [expA]; kwargs...) -> expA | ||
| exponential!(A, [expA], alg::AbstractAlgorithm) -> expA | ||
| Compute the exponential of the square matrix `A`, | ||
| !!! note | ||
| The bang method `exponential!` optionally accepts the output structure and | ||
| possibly destroys the input matrix `A`. Always use the return value of the function | ||
| as it may not always be possible to use the provided `expA` as output. | ||
| See also [`exponentiali(!)`](@ref exponentiali). | ||
| """ | ||
| @functiondef exponential | ||
|
|
||
| """ | ||
| exponentiali(τ, A; kwargs...) -> expiτA | ||
| exponentiali(τ, A, alg::AbstractAlgorithm) -> expiτA | ||
| exponentiali!(τ, A, [expiτA]; kwargs...) -> expiτA | ||
| exponentiali!(τ, A, [expiτA], alg::AbstractAlgorithm) -> expiτA | ||
| Compute the exponential of `i*τ*A`, where `i` is the imaginary unit, `τ` is a scalar, and `A` is a square matrix. | ||
| This allows the user to use the hermitian eigendecomposition when `A` is hermitian, even when `i*τ*A` is not. | ||
| !!! note | ||
| The bang method `exponentiali!` optionally accepts the output structure and | ||
| possibly destroys the input matrix `A`. | ||
| Always use the return value of the function as it may not always be | ||
| possible to use the provided `expiτA` as output. | ||
| See also [`exponential(!)`](@ref exponential). | ||
| """ | ||
| @functiondef n_args = 2 exponentiali | ||
|
|
||
| # Algorithm selection | ||
| # ------------------- | ||
| default_exponential_algorithm(A; kwargs...) = default_exponential_algorithm(typeof(A); kwargs...) | ||
| function default_exponential_algorithm(T::Type; kwargs...) | ||
| return MatrixFunctionViaLA(; kwargs...) | ||
| end | ||
| function default_exponential_algorithm(::Type{T}; kwargs...) where {T <: Diagonal} | ||
| return DiagonalAlgorithm(; kwargs...) | ||
| end | ||
|
|
||
| for f in (:exponential!,) | ||
| @eval function default_algorithm(::typeof($f), ::Type{A}; kwargs...) where {A} | ||
| return default_exponential_algorithm(A; kwargs...) | ||
| end | ||
| end | ||
|
|
||
| for f in (:exponentiali!,) | ||
| @eval function default_algorithm(::typeof($f), ::Tuple{A, B}; kwargs...) where {A, B} | ||
| return default_exponential_algorithm(B; kwargs...) | ||
| end | ||
| end |
sanderdemeyer marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # ================================ | ||
| # EXPONENTIAL ALGORITHMS | ||
| # ================================ | ||
| """ | ||
| MatrixFunctionViaLA() | ||
| Algorithm type to denote finding the exponential of `A` via the implementation of `LinearAlgebra`. | ||
| """ | ||
| @algdef MatrixFunctionViaLA | ||
|
|
||
| """ | ||
| MatrixFunctionViaEigh() | ||
| Algorithm type to denote finding the exponential `A` by computing the hermitian eigendecomposition of `A`. | ||
| The `eigh_alg` specifies which hermitian eigendecomposition implementation to use. | ||
| """ | ||
| struct MatrixFunctionViaEigh{A <: AbstractAlgorithm} <: AbstractAlgorithm | ||
| eigh_alg::A | ||
| end | ||
| function Base.show(io::IO, alg::MatrixFunctionViaEigh) | ||
| print(io, "MatrixFunctionViaEigh(") | ||
| _show_alg(io, alg.eigh_alg) | ||
| return print(io, ")") | ||
| end | ||
|
|
||
| """ | ||
| MatrixFunctionViaEig() | ||
| Algorithm type to denote finding the exponential `A` by computing the eigendecomposition of `A`. | ||
| The `eig_alg` specifies which eigendecomposition implementation to use. | ||
| """ | ||
| struct MatrixFunctionViaEig{A <: AbstractAlgorithm} <: AbstractAlgorithm | ||
| eig_alg::A | ||
| end | ||
| function Base.show(io::IO, alg::MatrixFunctionViaEig) | ||
| print(io, "MatrixFunctionViaEig(") | ||
| _show_alg(io, alg.eig_alg) | ||
| return print(io, ")") | ||
| end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TODO: do we want to keep this check here, knowing that it will again be checked in the iimplementation of
eigh_full?