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

Update condition for swapping iterators in PauliWord._matmul #5301

Merged
merged 17 commits into from
Apr 23, 2024

Conversation

mudit2812
Copy link
Contributor

Context:
We found that the wire order in Sum/Prod.terms was being swapped for the first and second term in terms that were Prods. This was due to the use of the pauli_rep. While building the pauli rep, we use PauliWord._matmul which swaps the order of self and other if len(other) >= len(self) so that we iterate through the larger PauliWord.

Example:

H = qml.prod(X(0), X(1), X(2))
Sum_coeffs, Sum_ops = H.terms()
>>> Sum_ops[0].wires, H.wires
(<Wires = [1, 0, 2]>, <Wires = [0, 1, 2]>)

Description of the Change:
Changed the condition for swapping from len(other) >= len(self) to len(other) > len(self).

Benefits:
More consistent Sum/Prod.pauli_rep, and Sum/Prod.terms() with the original operands. The above example now behaves as follows:

H = qml.prod(X(0), X(1), X(2))
Sum_coeffs, Sum_ops = H.terms()
>>> Sum_ops[0].wires, H.wires
(<Wires = [0, 1, 2]>, <Wires = [0, 1, 2]>)

Possible Drawbacks:

Related GitHub Issues:

Copy link
Contributor

github-actions bot commented Mar 4, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

thanks @mudit2812

Perhaps add a test ensuring that behavior

H = qml.prod(X(0), X(1), X(2))
Sum_coeffs, Sum_ops = H.terms()
assert Sum_ops[0].wires == H.wires

or update one of the existing terms() tests to also check the wires

Also changelog/bugfixes

@mudit2812
Copy link
Contributor Author

[sc-57962]

@mudit2812
Copy link
Contributor Author

Also changelog/bugfixes

I wouldn't call this a bug fix, as the repr wasn't incorrect, and it wasn't impacting any behaviour. I'll add tests and re-request review

Copy link

codecov bot commented Mar 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.68%. Comparing base (a8bba50) to head (288724c).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5301      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         410      410              
  Lines       38230    37939     -291     
==========================================
- Hits        38113    37821     -292     
- Misses        117      118       +1     

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

Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

Love it, thanks so much @mudit2812 ❤️

@trbromley trbromley requested a review from astralcai April 19, 2024 12:23
Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this! I remember being bugged by this when we were working on the ham-tests branch. 🎸

@trbromley trbromley added this to the v0.36 milestone Apr 19, 2024
@mlxd
Copy link
Member

mlxd commented Apr 23, 2024

Hey folks, since we have sufficient approvals can this be merged, or is there more work coming? If more work, is there a test or two we can add to demonstrate the changes?

@mudit2812 mudit2812 enabled auto-merge (squash) April 23, 2024 17:59
@mudit2812
Copy link
Contributor Author

Hey folks, since we have sufficient approvals can this be merged, or is there more work coming? If more work, is there a test or two we can add to demonstrate the changes?

No other work @mlxd . I was just blocked by reviews on the related PR in lightning. I wanted to have that one approved before merging this so that we can push the appropriate fix to lightning ASAP after this gets merged.

@mudit2812 mudit2812 merged commit 1476cd2 into master Apr 23, 2024
38 checks passed
@mudit2812 mudit2812 deleted the pauli-swap-order branch April 23, 2024 19:06
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.

5 participants