Skip to content

Krylov and Lanczos for Density Matrices#2725

Merged
Ericgig merged 48 commits into
qutip:masterfrom
Langhaarzombie:feature/kry_density
Mar 12, 2026
Merged

Krylov and Lanczos for Density Matrices#2725
Ericgig merged 48 commits into
qutip:masterfrom
Langhaarzombie:feature/kry_density

Conversation

@Langhaarzombie

@Langhaarzombie Langhaarzombie commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Description
Enables the calculation of time dynamics for density matrices using the Lanczos algorithm for the Krylov subspace method.

Comment thread qutip/solver/krylovsolve.py Outdated
Comment thread qutip/solver/krylovsolve.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Langhaarzombie and others added 19 commits September 22, 2025 11:42
- Generalize lanczos to states not normalized acc. to L2 (e.g.
  vectorized dens. matrices)
- Simplify Lanczos by index fixes in matrices, little code restructuring
- This is intended to enable manual usage of the Kyrlov algorihtm
  without too much overhead
- WARNING: This break the default behavior of the Integrator when simply
  initialized and run
- Will be reverted when closing the PR
- To be smoothed out later, just added for completeness
- kyrlov solve can handle matrix states now
- fixed loss of normalization for matrix states
- reverted to old max_step cal for now
- added tests, fixed error messages
Co-authored-by: Eric Giguère <[email protected]>
@coveralls

coveralls commented Jan 30, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 86.805% (-0.2%) from 86.981%
when pulling efd3063 on Langhaarzombie:feature/kry_density
into 8d4bb69 on qutip:master.

@Langhaarzombie Langhaarzombie marked this pull request as ready for review February 2, 2026 17:29
@Langhaarzombie

Langhaarzombie commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

Since a lot has changed and I am formally opening this for review, I am putting the checklist and a more detailled description for this PR.

The krylovsolve method gets extended to support calculations for both closed and open (Lindblad-like) systems. For that, I added the fully reorthogonalized Lanczos and Arnoldi algorithm. The former improves the standard Lanczos by greatly reducing numerical errors. The latter is required to optain a Krylov space for open systems. The user can still choose which algorithm to use, since they differ in computation time.

Additionally the solver was fixed in terms of handling different system dimensions and the happy breakdown correctly.

Technical TODOs / ideas

  • We can discuss an increase in tests to make the method more robust
  • Potential code style changes for more efficient interplay with data layer and built in qutip functions
  • Change error estimation method (to be discussed)

Contribution Checklist

  • pep8 codestyle checks
  • Update docs

@Ericgig Ericgig 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.

It does not work for simple example anymore:

N = 20
a = qt.destroy(N)
H = qt.num(N) + a + a.dag()
psi0 = qt.basis(N, N-1)
expected = qt.sesolve(H, psi0, [0, 1]).final_state -
krylov = qt.krylovsolve(H, psi0, [0, 1], krylov_dim=5).final_state
print(expected - krylov).norm()

In master: 2.958730598870145e-06
With this: 1.1326590031741184

Comment thread qutip/tests/solver/test_mesolve.py Outdated
Comment on lines +720 to +723
# Kyrlov method works best with dense Hamiltonians.
# Since rand_herm(.) usually provides sparse matrices, we are summing
# multiple samplings.
H = np.sum([qutip.rand_herm(20) for _ in range(30)]) / 30

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 not the way to do this...

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 it's replaced with the proper density argument.

I had to increase the tolerance to 1e-5. to accomodate a small error (compared to mesolve) in the expectation value over time that is oscillating around 0. At longer times (1000 times longer than in the test), the error stays withing 1e-3, so I deemed this increase reasonable. This error is essentially the same for the master version of the code.

Do let me know if that is still withing desired bounds.

Comment thread qutip/solver/krylovsolve.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
- Fix condition when happy breakdown detected
- Readd min step failsafe
- Improved error messages
@Langhaarzombie

Copy link
Copy Markdown
Contributor Author

The latest version of the code should fix this simple code example.

Be aware that when you are using the master version of krylov solve, there is a bug that the actual krylov dimension that is used, is +1 of what you request (so 6 in your example).
Therefore, when you put dimension 5 in the new version of the solver, you get an error since there are too many integration steps (but at least the dimension is what you wanted). When you change the dimension to 6, you will get exactly the same result as with the master verison.

@Ericgig Ericgig 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.

Looking better.

It would be good if there was an explanation on what would make a good krylov dim in the documentation.

Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread qutip/solver/krylovsolve.py Outdated
Comment thread qutip/solver/integrator/krylov.py Outdated
Comment thread doc/guide/dynamics/dynamics-krylov.rst
@Langhaarzombie

Copy link
Copy Markdown
Contributor Author

I implemented all the changes, thanks a lot!

As for the krylov dimension in the documentation: I added a paragraph to give some intuition, but it is really hard to determine the "best possible" dimension. I hope it helps a bit.

@Ericgig Ericgig 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.

Seems good.
Thank you.

@Ericgig

Ericgig commented Mar 9, 2026

Copy link
Copy Markdown
Member

@Langhaarzombie
Could you fix the conflict.

@Ericgig Ericgig merged commit 299a401 into qutip:master Mar 12, 2026
16 checks passed
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.

3 participants