Fix density matrix overlap dimension#3909
Merged
1tnguyen merged 6 commits intoNVIDIA:mainfrom Feb 19, 2026
Merged
Conversation
The density matrix branch of CuDensityMatState::overlap() incorrectly used `dimension` (total element count, N^2) as the matrix side length, creating an N^2 x N^2 Eigen matrix and copying N^4 elements instead of N^2. This caused excessive memory allocation or crashes for any density matrix overlap computation. Use sqrt(dimension) as the matrix side length, consistent with dump(), getTensor(), and operator() in the same file. Add regression tests for density matrix overlap covering self-overlap, orthogonal states, and partial overlap. Signed-off-by: huaweil <huaweil@nvidia.com>
The move constructor and move assignment operator of CuDensityMatState did not transfer the singleStateDimension field. Add it for completeness so that all member state is properly moved, preventing potential issues if a batched state object is ever moved directly rather than through a unique_ptr. This is a defensive change with no impact on current usage, as batched states with non-zero singleStateDimension are always heap-allocated and managed via unique_ptr. Signed-off-by: huaweil <huaweil@nvidia.com>
1a6a397 to
4b6a6ca
Compare
Collaborator
Command Bot: Processing... |
Remove redundant local `size` alias and pass `dimension` directly to cudaMemcpy. Signed-off-by: huaweil <huaweil@nvidia.com>
b9aabdc to
8836ce8
Compare
Collaborator
Author
Command Bot: Processing... |
Collaborator
Command Bot: Processing... |
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
CuDensityMatState::overlap()where the density matrix branch incorrectly useddimension(total element count, N²) as the Eigen matrix side length, creating an N² × N² matrix and attempting to copy N⁴ elements. This would cause excessive memory allocation or crashes. The fix usessqrt(dimension)as the matrix side length, consistent withdump(),getTensor(), andoperator()in the same file.singleStateDimensionin the move constructor and move assignment operator for completeness. This is a defensive change with no impact on current usage, as batched states with non-zerosingleStateDimensionare always heap-allocated and managed viaunique_ptr.Details
In
CuDensityMatState, thedimensionmember stores the total number of elements: N for state vectors, N² for density matrices. The overlap method's density matrix branch directly useddimensionas both the row and column count of the Eigen matrix: