fix: Correct conjugate transpose in apply_unitary_density_bmm#316
Open
yurekami wants to merge 1 commit into
Open
fix: Correct conjugate transpose in apply_unitary_density_bmm#316yurekami wants to merge 1 commit into
yurekami wants to merge 1 commit into
Conversation
Fixes mit-han-lab#254 The apply_unitary_density_bmm function was computing only the conjugate of the matrix (U*) instead of the conjugate transpose (U†) when applying ρ → U ρ U†. For the Hermitian adjoint U†, we need both: - Element-wise complex conjugation - Matrix transposition Before: matdag = torch.conj(mat) # Only conjugate After: matdag = torch.conj(mat.permute(...)) # Conjugate + transpose This fix corrects the density matrix evolution for all gates. Files fixed: - torchquantum/functional/gate_wrapper.py (line 301) - torchquantum/density/density_func.py (line 210) The einsum implementation was already correct; this aligns the bmm implementation with it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.
Summary
Fixes #254
This PR fixes a critical bug in the
apply_unitary_density_bmmfunction where the density matrix evolution ρ → U ρ U† was computed incorrectly.The Bug
The code was computing only the conjugate of the matrix instead of the conjugate transpose (Hermitian adjoint):
For U† (Hermitian adjoint), we need both:
Example
As reported in the issue, applying Y gate to |00⟩⟨00| was giving wrong results:
Files Changed
torchquantum/functional/gate_wrapper.py- line 301torchquantum/density/density_func.py- line 210Note
The einsum implementation (
apply_unitary_density_einsum) was already correct. This fix aligns the bmm implementation with it.🤖 Generated with Claude Code