Skip to content
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

Migrate linear algebra code from faer-rs to nalgebra #13665

Open
mtreinish opened this issue Jan 14, 2025 · 0 comments
Open

Migrate linear algebra code from faer-rs to nalgebra #13665

mtreinish opened this issue Jan 14, 2025 · 0 comments
Labels
priority: medium Rust This PR or issue is related to Rust code in the repository type: feature request New feature or request
Milestone

Comments

@mtreinish
Copy link
Member

mtreinish commented Jan 14, 2025

What should we add?

In qiskit 1.0 we started using the faer-rs library for linear algebra from rust. This enabled us to migrate a lot of python/numpy numeric code into Rust during the development of 1.x which greatly accelerated a lot of transpiler code in the 1.x cycle. But with the recent releases of the faer it's become clear the project isn't really in a place where we can rely on it as a stable upstream dependency for Qiskit. The development of the library is very rapid and it's hard to keep track of what is going on, and the MSRV changes are incompatible with our requirements for Qiskit.

For our use cases most of the linear algebra we do is with small matrices, especially in the transpiler, typically with 2x2 and 4x4 matrices, and nalgebra is a good fit for that because we can used fixed size stack allocated types for the arrays. This will also provide better performance in most cases for what we're currently using. It's also a more stable/established library that is better aligned with our requirements for dependencies in Qiskit.

@mtreinish mtreinish added priority: medium Rust This PR or issue is related to Rust code in the repository type: feature request New feature or request labels Jan 14, 2025
@mtreinish mtreinish added this to the 2.0.0 milestone Jan 14, 2025
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 21, 2025
As part of Qiskit#13665 this migrates the faer-rs usage in quantum_volume.rs
to just use nalgebra instead. The only faer usage there was for the qr
decomposition and everything else was done with fixed sized arrays
or Array2. This migration is fairly simple but it's a good start towards
the goal of eventually removing our faer-rs usage in Qiskit. It might
improve performance because we remove one layer of heap allocated
objects by moving from Array2 to Matrix4 for rust operations. Although
we do need an equivalent heap allocation for generating the numpy arrays
used in `UnitaryGate` so it might not really change anything
significantly.
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 23, 2025
This commit stops using faer and ndarray for the uc_gate.rs module and
switches to use nalgebra instead. The uc_gate.rs module works all in
terms of 2x2 matrices and moving to nalgebra brings the advantage of
having everything be fixed size stack allocated for the arrays used.
While this code path isn't performance critical this should improve
performance, despite the goal here being to remove a dependency on faer.

The tradeoff here is that nalgebra doesn't seem to have a general
eigensolver natively. There is functionality for this in the
nalgebra-lapack crate, but as the name implies this depends on linking
against lapack to work. The reason we originally started using faer is
because it's pure rust and doesn't complicate our build system. In this
specific case this is not a big deal because the matrices we need to
compute the eigenvectors and eigenvalues for is a 2x2 and it's trivial
to do by hand, which this commit does. This is something to keep in
mind for the future if we need this functionality more generally we
will have to make a choice between faer and linking against lapack.

Part of Qiskit#13665
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 23, 2025
This commit stops using faer and ndarray for the uc_gate.rs module and
switches to use nalgebra instead. The uc_gate.rs module works all in
terms of 2x2 matrices and moving to nalgebra brings the advantage of
having everything be fixed size stack allocated for the arrays used.
While this code path isn't performance critical this should improve
performance, despite the goal here being to remove a dependency on faer.

The tradeoff here is that nalgebra doesn't seem to have a general
eigensolver natively. There is functionality for this in the
nalgebra-lapack crate, but as the name implies this depends on linking
against lapack to work. The reason we originally started using faer is
because it's pure rust and doesn't complicate our build system. In this
specific case this is not a big deal because the matrices we need to
compute the eigenvectors and eigenvalues for is a 2x2 so this commit
includes a function to compute that directly. This lack of functionality
is something to keep in mind for the future if we need this
functionality more generally we will have to make a choice between faer
and linking against lapack.

Part of Qiskit#13665
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 23, 2025
This commit stops using faer and ndarray for the uc_gate.rs module and
switches to use nalgebra instead. The uc_gate.rs module works all in
terms of 2x2 matrices and moving to nalgebra brings the advantage of
having everything be fixed size stack allocated for the arrays used.
While this code path isn't performance critical this should improve
performance, despite the goal here being to remove a dependency on faer.

The tradeoff here is that nalgebra doesn't seem to have a general
eigensolver natively. There is functionality for this in the
nalgebra-lapack crate, but as the name implies this depends on linking
against lapack to work. The reason we originally started using faer is
because it's pure rust and doesn't complicate our build system. In this
specific case this is not a big deal because the matrices we need to
compute the eigenvectors and eigenvalues for is a 2x2 so this commit
includes a function to compute that directly. This lack of functionality
is something to keep in mind for the future if we need this
functionality more generally we will have to make a choice between faer
and linking against lapack.

Part of Qiskit#13665
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 23, 2025
This commit stops using faer and ndarray for the uc_gate.rs module and
switches to use nalgebra instead. The uc_gate.rs module works all in
terms of 2x2 matrices and moving to nalgebra brings the advantage of
having everything be fixed size stack allocated for the arrays used.
While this code path isn't performance critical this should improve
performance, despite the goal here being to remove a dependency on faer.

The tradeoff here is that nalgebra doesn't seem to have a general
eigensolver natively. There is functionality for this in the
nalgebra-lapack crate, but as the name implies this depends on linking
against lapack to work. The reason we originally started using faer is
because it's pure rust and doesn't complicate our build system. In this
specific case this is not a big deal because the matrices we need to
compute the eigenvectors and eigenvalues for is a 2x2 so this commit
includes a function to compute that directly. This lack of functionality
is something to keep in mind for the future if we need this
functionality more generally we will have to make a choice between faer
and linking against lapack.

Part of Qiskit#13665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: medium Rust This PR or issue is related to Rust code in the repository type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant