Skip to content

Conversation

@ytdHuang
Copy link
Member

@ytdHuang ytdHuang commented Feb 18, 2025

Checklist

Thank you for contributing to QuantumToolbox.jl! Please make sure you have finished the following tasks before opening the PR.

  • Please read Contributing to Quantum Toolbox in Julia.
  • Any code changes were done in a way that does not break public API.
  • Appropriate tests were added and tested locally by running: make test.
  • Any code changes should be julia formatted by running: make format.
  • All documents (in docs/ folder) related to code changes were updated and able to build locally by running: make docs.
  • (If necessary) the CHANGELOG.md should be updated (regarding to the code changes) and built by running: make changelog.

Request for a review after you have completed all the tasks. If you have not finished them all, you can also open a Draft Pull Request to let the others know this on-going work.

Description

This PR introduce a new function: groundstate to calculate the ground state eigenvalue and corresponding eigenvector.

This function currently doesn't support sparse eigsolve. Because it seems that sparse eigsolve doesn't find the smallest eigenvalue first.

@codecov
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.25%. Comparing base (300fd5d) to head (2fdcc04).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #412      +/-   ##
==========================================
+ Coverage   94.20%   94.25%   +0.04%     
==========================================
  Files          46       46              
  Lines        2918     2924       +6     
==========================================
+ Hits         2749     2756       +7     
+ Misses        169      168       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +563 to +582
function groundstate(
A::QuantumObject{OpType};
safe::Bool = true,
tol::Real = 1e-8,
kwargs...,
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
# TODO: support for sparse eigsolve
#if !sparse
result = eigen(A; kwargs...)
#else
# eigvals = safe ? 2 : 1 # number of eigenvalues to calculate
# result = eigsolve(A; eigvals = eigvals, tol = tol, kwargs...)
#end

# the tolarence should be less strick than the `tol` for the eigensolver
# so it's numerical errors are not seen as degenerate states.
evals = result.values
safe && (abs(evals[2] - evals[1]) < (10 * tol)) && @warn "Ground state may be degenerate."

return evals[1], QuantumObject(result.vectors[:, 1], _eigenvector_type(OpType), result.dimensions)
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to use eigenstates instead of eigen. In this way we can use all the internal kwargs, including also the sparse one.

In the case of sparse, it automatically finds the largest eigenvalue by the nature of the Arnoldi algorithm. However, this can be fixed by defining a sigma argument, which performs the shift-inverse procedure.

Copy link
Member Author

@ytdHuang ytdHuang Feb 19, 2025

Choose a reason for hiding this comment

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

That is why I didn't use the eigenstates
Because this function is only for calculating ground state. We should assign some of the keyword arguments, make it not adjustable from the users.

Copy link
Member Author

Choose a reason for hiding this comment

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

What value should I set for sigma in eigsolve to make it search for the lowest eigenvalue first?

Copy link
Member

Choose a reason for hiding this comment

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

Because this function is only for calculating ground state. We should assign some of the keyword arguments, make it not adjustable from the users.

What do you mean exactly? If we use eigenstates(A; kwargs...) we can forward all the kwargs to that, even the sparse = ... (which by default is false, so that we fall in this case). If the user specify sparse=true, then he/she is supposed to be more advanced, and should know that the shift-inverse method applies here. By doing so, they should also specify a sigma shift value. We can even write a note on that.

What value should I set for sigma in eigsolve to make it search for the lowest eigenvalue first?

sigma should be a value smaller than the supposed ground state energy. Let say that you expect the ground state energy to me close to 0, then sigma=-0.1 is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, in general, I should set sigma = -Inf ?

Copy link
Member

Choose a reason for hiding this comment

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

No this won't work. This is problem dependent.

The user using sparse=true is supposed to be advanced to be able to set this value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm
Ok
I would probably close this PR.

Seems like we cannot make it.

@ytdHuang ytdHuang closed this Feb 20, 2025
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.

2 participants