Skip to content

Exponential#94

Open
sanderdemeyer wants to merge 53 commits into
QuantumKitHub:mainfrom
sanderdemeyer:exponential
Open

Exponential#94
sanderdemeyer wants to merge 53 commits into
QuantumKitHub:mainfrom
sanderdemeyer:exponential

Conversation

@sanderdemeyer

@sanderdemeyer sanderdemeyer commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

This implements the exponential of a matrix for both BLASFloats and BigFloats.

I have named these functions exponential and exponential!, instead of the usual exp and exp! from LinearAlgebra. Extending these methods while keeping the current structure using @algdef and @ functiondef results in some naming conflicts. The default for BLASFloats is to use LinearAlgebra.exp!. In TensorKit, we can still stick to the exp naming convention.

@codecov

codecov Bot commented Nov 12, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 109 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/implementations/exponential.jl 0.00% 87 Missing ⚠️
src/interface/exponential.jl 0.00% 9 Missing ⚠️
src/interface/matrixfunctions.jl 0.00% 8 Missing ⚠️
ext/MatrixAlgebraKitGenericSchurExt.jl 0.00% 3 Missing ⚠️
src/common/view.jl 0.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/MatrixAlgebraKit.jl 100.00% <ø> (ø)
src/common/view.jl 7.40% <0.00%> (-92.60%) ⬇️
ext/MatrixAlgebraKitGenericSchurExt.jl 0.00% <0.00%> (-100.00%) ⬇️
src/interface/matrixfunctions.jl 0.00% <0.00%> (ø)
src/interface/exponential.jl 0.00% <0.00%> (ø)
src/implementations/exponential.jl 0.00% <0.00%> (ø)

... and 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/interface/exponential.jl Outdated
Comment thread test/exponential.jl Outdated
Comment thread test/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated

function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh)
D, V = eigh_full(A, alg.eigh_alg)
copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reduced allocation strategy:

Suggested change
copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V))
iV = inv(V)
map!(exp, diagview(D))
mul!(expA, rmul!(V, D), iV)

@sanderdemeyer sanderdemeyer Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It has to be map!(exp, diagview(D), diagview(D)) instead of map!(exp, diagview(D)), but good suggestion otherwise. I have also added it for the ExponentialViaEig.
EDIT: the suggested change works only for Julia 1.12 onwards. That's why I will keep the version with
3 arguments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just diagview(D) .= exp.(diagview(D))?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is that more efficient than the current code? If not, I'd prefer to keep it that way, since it feels a bit more natural to me.

Comment thread src/implementations/exponential.jl Outdated
Comment thread src/interface/decompositions.jl Outdated
change some input tests
remove redundant comment
include ComplexF16 in tests
fix unchanged test names and docs
improve allocations

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall I'm not fully convinced by the interface of exponential(!), especially in its current form and implementation this looks slightly strange.

LinearAlgebra uses an in-place version, i.e. it reuses the input array to return exp!, and looking at the different implementations you have here, it is not obvious that trying to fit this into a exponentiate!(A, expA, alg) signature is really helping us - on the contrary, all this is really doing is creating an additional copy at the end just to make sure that it is allocated in the provided output.
As we discussed for your previous PR, this really is not the purpose of being able to provide the output argument.

For the algorithms, thinking a bit ahead, it might be appropriate to just call these something along the lines of matrix functions via eig, since presumably these approaches are actually generic for all of these implementations.

Comment thread src/interface/matrixfunctions.jl
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated

function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh)
D, V = eigh_full(A, alg.eigh_alg)
copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not just diagview(D) .= exp.(diagview(D))?

Comment thread ext/MatrixAlgebraKitGenericSchurExt.jl Outdated
Comment thread src/implementations/exponential.jl
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
@sanderdemeyer

sanderdemeyer commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

Overall I'm not fully convinced by the interface of exponential(!), especially in its current form and implementation this looks slightly strange.

LinearAlgebra uses an in-place version, i.e. it reuses the input array to return exp!, and looking at the different implementations you have here, it is not obvious that trying to fit this into a exponentiate!(A, expA, alg) signature is really helping us - on the contrary, all this is really doing is creating an additional copy at the end just to make sure that it is allocated in the provided output. As we discussed for your previous PR, this really is not the purpose of being able to provide the output argument.

The idea of putting it in this framework is to allow for different sorts of algorithms, i.e. ones that would also work for BigFloats. I get that in the BLASFloat case, we should avoid extra allocations, but could you elaborate on your suggestion for ExponentialViaLA? Do you just want to get rid of the preallocated output?

For the algorithms, thinking a bit ahead, it might be appropriate to just call these something along the lines of matrix functions via eig, since presumably these approaches are actually generic for all of these implementations.

I may be missing your point here, but that was the idea behind the ExponentialViaEigh and ExponentialViaEig naming conventions? Or do you really want separate names for each eig_alg?

@lkdvos

lkdvos commented Nov 13, 2025

Copy link
Copy Markdown
Member

The idea of putting it in this framework is to allow for different sorts of algorithms, i.e. ones that would also work for BigFloats. I get that in the BLASFloat case, we should avoid extra allocations, but could you elaborate on your suggestion for ExponentialViaLA? Do you just want to get rid of the preallocated output?

I am indeed referring to the preallocated output, not the algorithms part. It really only makes sense to have the option of giving a preallocated output if we are actually able to use this, and for the current implementations you have this is not saving us any work, rather it is increasing it because you add an extra allocation at the beginning and an extra copy at the end.

While it is definitely possible to have initialize_output(exponentiate!, A) = A to just allocate the result in-place, I still wonder if it is then useful to have to implement the boiler plate for fitting it in a framework that we are mostly going to bypass anyways.

I may be missing your point here, but that was the idea behind the ExponentialViaEigh and ExponentialViaEig naming conventions? Or do you really want separate names for each eig_alg?

Sorry I should have explained that better, I meant that I would like to avoid having to also define SqrtViaEig, SinViaEig, ..., and simply have some algorithm that signifies "solve this by doing an eigenvalue decomposition".

@sanderdemeyer

sanderdemeyer commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

I am indeed referring to the preallocated output, not the algorithms part. It really only makes sense to have the option of giving a preallocated output if we are actually able to use this, and for the current implementations you have this is not saving us any work, rather it is increasing it because you add an extra allocation at the beginning and an extra copy at the end.

While it is definitely possible to have initialize_output(exponentiate!, A) = A to just allocate the result in-place, I still wonder if it is then useful to have to implement the boiler plate for fitting it in a framework that we are mostly going to bypass anyways.

Is your suggestion then to just skip the whole @ functiondef and other general frameworks we have to define exponentiate and exponentiate!, or to keep the current framework somewhat, but just remove the preallocated output argument?

Sorry I should have explained that better, I meant that I would like to avoid having to also define SqrtViaEig, SinViaEig, ..., and simply have some algorithm that signifies "solve this by doing an eigenvalue decomposition".

Okay, I see. I agree and will change this.

@Jutho

Jutho commented Nov 13, 2025

Copy link
Copy Markdown
Member

Regarding the algorithm names, I agree with Lukas and also think we want to have a general
MatrixFunctionViaEig, though it is useful to know about which function exactly, because some functions map real matrices to real matrices (even if complex eigenvalues are involved), and others do not. I think these are the two major categories, so we could just have two different algorithms for that, but I don't know good names for that.

Regarding the role of the output arguments, I only partially agree. The whole point of why we started MatrixAlgebraKit.jl, is because in TensorKit we first want to define the output tensor, and then compute block per block the result, where we want to store the result in the corresponding block of the output tensor. Ideally, yes, the computation is such that we also use that output data as storage during the computation, in such a way that the end result "naturally" ends up there, but if that is difficult, a final copy! can still be useful. We can then always try to improve this later on behind the scenes, but at least TensorKit can be agnostic about this.

Note that the LinearAlgebra exp! is also cheating, and typically also ends up just copying the final result back into A. There is no way to actually compute exp(A) fully in-place. The typical approach via Pade approximations has a ton of allocations. I think I actually have a slightly allocation-friendlier implementation lying around for some things we did in CMPSKit.jl

@Jutho

Jutho commented Nov 13, 2025

Copy link
Copy Markdown
Member

Regardless of the comment about general matrix functions, it is a fact that the exponential is by far the most useful and common one that we need, so I am also not opposed to first thinking carefully about this one, and having some part of the implementation be specific for matrix exponentials.

In particular, one important consideration that we might want to include in this design, that is specific to our use case, is that we also might be interested in computing exp(-im* δt*H) for real time evolution, where it is probably useful to use eigh for the Hermitian matrix H, but still the end result will be complex. Julia has a cis function for cis(x) = exp(im * x), but unfortunately that comes without a minus sign in the argument. But maybe cis(-δt * H) is not too bad.

@lkdvos

lkdvos commented Nov 13, 2025

Copy link
Copy Markdown
Member

To comment on the TensorKit interaction, I definitely agree with the purpose, but this is not actually currently the design we ended up with.
Our interface very clearly states that we may use the provided output, but are in no way bound to do so.
This is reflected in the TensorKit implementation, see e.g. https://github.com/QuantumKitHub/TensorKit.jl/blob/feab2072b2909b9a127d871ef58c36e83623c2fc/src/factorizations/matrixalgebrakit.jl#L39
I'm of course happy to rediscuss that design decision, but I'm actually quite happy with how that is shaping out.

So basically there are two comments I have:

On the one hand, there is the question about whether or not there are implementations that benefit from providing an additional output array.
From looking at the implementations here, this is not obvious at all, actually it seems like instead we are simply allocating an additional array at the end to copy the results into, which we might as well have reused the A for, since we already "destroyed" that data anyways.
I am not saying that the implementations have to be fully allocation-free (the LAPACK routines still have workspaces too), I just think that if providing the output is a way to avoid having to do an allocation, it really shouldn't be increasing the allocations instead.
If we can't come up with a case where having this additional array actually helps us out, it might be reasonable to simplify define exponentiate!(A, alg) -> expA where expA === A instead, which would still work for TensorKit in the exact same way.
I definitely agree that this can be achieved by hacking it into the current interface as well, by simply having initialize_output(exponentiate!, A) = A, I just wanted to open up the discussion about whether or not that is something we like, or think this is just boilerplate we aren't using.
Giving it some thought, I'm definitely okay with the initialize_output(...) = A default and going from there, i.e. keeping @functiondef and "hacking" this into that.

On the other hand, given that interface, if there is no way of naturally making the output end up in the provided destination, I would really like to avoid ending up with a final copy!(provided_dest, computed_exp) at the end, even as a "for now" implementation that we can improve on later.
Given that we have to check for in-placeness in TensorKit anyways, I would in this case much rather have initialize_output() = nothing, and simply return the computed_exp instead.
This is the exact same comment and solution as for the generic linear algebra wrappers.

@Jutho

Jutho commented Nov 13, 2025

Copy link
Copy Markdown
Member

I guess I am a bit confused, because most of the implementations now do actually perform the final step in the calculation in such a way that the result is directly stored in the output array, no? It is only the algorithm that goes via Base/LinearAlgebra that requires the extra copy! step, and that implementation I will happily replace with my own Pade implementation.

But it is also true that, by the time the final step of the calculation is reached; the memory of A, which has already been destroyed and is no longer containing active information, can be reclaimed to store the result, so initialize_output(...) = A could indeed be the right design choice.

sanderdemeyer and others added 4 commits November 19, 2025 15:07
change name to `MatrixFunctionViaEig` etc
change `decompositions` to `matrixfunctions`
add default algorithm for Diagonal matrices
add input checks
add @testthrows to catch non-hermitian matrices being given to MatrixFunctionViaEigh
change default exponential algorithm to e.g. `MatrixFunctionViaEig` of the default `eig_alg`

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall this is looking quite nice! Apologies if this is a premature review, just showed up in my notifications and figured I'd have a look.

The only thing I'm still wondering is if we want to just replace exponentialr(t, A) = exponential((t, A)) and not have the two functions. I know we had this discussion before, I'm not sure what came out of that to be honest.

Comment thread ext/MatrixAlgebraKitGenericSchurExt.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
# --------------
function exponential!(A, expA, alg::MatrixFunctionViaLA)
check_input(exponential!, A, expA, alg)
return LinearAlgebra.exp!(A)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know we had a lot of discussions about this in the past, but I think we now settled on actually copying over the data (see also the GLA extension, I think), so this should probably become:

Suggested change
return LinearAlgebra.exp!(A)
LinearAlgebra.exp!(A)
A === expA || copy!(expA, A)
return expA

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I know we had a lot of discussions about this in the past, but I think we now settled on actually copying over the data (see also the GLA extension, I think), so this should probably become:

This does not work, for the very bizarre reason that LinearAlgebra.exp!(A) does not guarantee that the result is directly stored in the matrix A (at least when I test it locally with version 1.12.0). Everything is fine if I replace line 74 with A = LinearAlgebra.exp!(A), but probably there is a better way to get around this?

Comment thread test/genericlinearalgebra/exponential.jl Outdated
Comment thread test/genericlinearalgebra/exponential.jl Outdated
sanderdemeyer and others added 3 commits June 12, 2026 11:09
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
@sanderdemeyer

Copy link
Copy Markdown
Contributor Author

The only thing I'm still wondering is if we want to just replace exponentialr(t, A) = exponential((t, A)) and not have the two functions. I know we had this discussion before, I'm not sure what came out of that to be honest.

I also don't know whether there was a consensus. I would be in favour of indeed just using exponential((t,A)), since that seems to be the most convenient/logical option. Especially if we don't support exponentiali, it feels weird to call it 'exponentialr'. So if you agree with this, I will make the necessary changes.

@lkdvos

lkdvos commented Jun 12, 2026

Copy link
Copy Markdown
Member

Yes, let's do this and then push this PR over the edge 🎉

Comment thread src/implementations/exponential.jl Outdated
Comment on lines +15 to +17
if scalar_check
@check_scalar(expA, A)
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added these lines to be able to differentiate the check_input test between exponential(A) and exponential(tau,A), since the latter does not require @check_scalar. Feel free to make suggestions to do this in a different way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does the latter not require that? If A is real and tau is complex, expA has to be complex right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, I forgot about that. I added a method check_input for exponential((tau,A)), where I dispatch on whether tau is real.

Comment thread src/implementations/exponential.jl Outdated
end

function check_input(type::typeof(exponential!), τ, A::AbstractMatrix, expA::AbstractMatrix, alg)
return check_input(type, complex(A), expA, alg)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is probably not entirely how we should handle this, as we don't want to copy A to make the check. I think the easiest is just to copy some of the code, and I think @check_scalar has the option to give complex as an extra parameter. See e.g. the eig_ implementations, where we have a similar thing happening

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now, I copy some of the code, and changed @check_scalar(expA, A) to (τ isa Real) ? @check_scalar(expA, A) : @check_scalar(expA, A, complex)

Comment thread src/implementations/exponential.jl Outdated

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I left a final set of comments, otherwise this is good to go for me. You'll see it's quite nitpicky, so feel free to share your opinions as well!

Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
Comment thread src/implementations/exponential.jl Outdated
return nothing
end

function check_input(::typeof(exponential!), τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another question that is kind of irrelevant, but just want to check if you have a specific reasoning for choosing check_input(exponential!, (τ, A), expA, alg) vs check_input(exponential!, τ, A, expA, alg). I guess there is some annoyance with the tuple and dispatch, but this is somewhat less in line with the other single-argument functions where f!(in, out, alg) calls check_input(f!, in, out, alg), instead of here check_input(f!, in..., out, alg). I assume this is a remnant of using the 2-arg @functiondef from before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree that check_input(exponential!, (τ, A), expA, alg) is the more logical choice. I've changed that now.

sanderdemeyer and others added 9 commits June 12, 2026 22:01
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
@sanderdemeyer

Copy link
Copy Markdown
Contributor Author

I think I left a final set of comments, otherwise this is good to go for me. You'll see it's quite nitpicky, so feel free to share your opinions as well!

Thanks for the final comments. I agreed with all of them!

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for all the work @sanderdemeyer, this looks like a great addition!

@Jutho, @kshyatt I'll leave this open in case you still want to have a look, and at the latest I'll merge this Monday evening to keep things moving, while not imposing on weekends 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants