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

Fix qml.center with linear combinations #6049

Merged
merged 20 commits into from
Aug 6, 2024
Merged

Fix qml.center with linear combinations #6049

merged 20 commits into from
Aug 6, 2024

Conversation

dwierichs
Copy link
Contributor

@dwierichs dwierichs commented Jul 29, 2024

Context:
Linear combinations were not taken into account in qml.center, making it miss center elements for some inputs.

Description of the Change:
Change the algorithm that computes the center.
Kudos to @therooler for the prototype.

Benefits:
Correct code.

Possible Drawbacks:
Slower performance for inputs that would have been fine before anyways.
This has been fixed by reverting to the previous implementation if PauliWord instances are passed only.

Related GitHub Issues:
Fixes #6048
[sc-70014]

@dwierichs dwierichs requested review from Qottmann and trbromley and removed request for trbromley July 29, 2024 12:58
tests/pauli/dla/test_center.py Outdated Show resolved Hide resolved
tests/pauli/dla/test_center.py Outdated Show resolved Hide resolved
tests/pauli/dla/test_center.py Show resolved Hide resolved
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.

Looks good 👍

Some minor things/questions. Could you add a bit more description of the involved math, since this is not super trivial and not explained in the doc string, would be good to at least have some explanation in the code as comment. That being said, is it really necessary to compute three kernels here?

pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Show resolved Hide resolved
tests/pauli/dla/test_center.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jul 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.66%. Comparing base (eafd446) to head (1cf8da1).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6049      +/-   ##
==========================================
- Coverage   99.66%   99.66%   -0.01%     
==========================================
  Files         430      430              
  Lines       41762    41489     -273     
==========================================
- Hits        41624    41349     -275     
- Misses        138      140       +2     

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

Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
@dwierichs
Copy link
Contributor Author

@Qottmann

Could you add a bit more description of the involved math, since this is not super trivial and not explained in the doc string, would be good to at least have some explanation in the code as comment.

Yep, good point! Will do!

That being said, is it really necessary to compute three kernels here?

I'm not sure, maybe we can do Gram-Schmidt instead 🤔 I'll give it another attempt!

@dwierichs
Copy link
Contributor Author

@Qottmann Found a (probably) easier way for the kernel intersections. Added derivation for that new technique to docstring and put the intersection code into a helper function with dev docstring.

@dwierichs dwierichs requested a review from Qottmann July 30, 2024 07:55
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.

Very nice now! Just some minor things but overall looks good 👍

pennylane/pauli/dla/center.py Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
@dwierichs dwierichs requested a review from Qottmann August 1, 2024 09:41
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 for this great upgrade @dwierichs 💪

@Qottmann
Copy link
Contributor

Qottmann commented Aug 1, 2024

Is this a bug or am I once again not fully getting the definition of the center 🫠

gg = [
    Z(0) @ Z(1),
    Y(0) @ Y(1),
    Z(0) @ Y(1),
    Y(0) @ Z(1)
]
c = qml.center(gg) 
print(c)
# [Z(0) @ Z(1), Y(0) @ Y(1), Z(0) @ Y(1), Y(0) @ Z(1)]


gg = [g_i.pauli_rep for g_i in gg]
for g1 in gg:
    for g2 in gg:
        com = g1.commutator(g2)
        com.simplify()
        assert len(com) == 0 # fails with e.g. third output being -2j * X(1)

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Thanks @dwierichs ! Your documentation was really helpful in understanding what the function does. Nevertheless, I was still a bit confused so I've mostly just left questions for my own understanding in this review. Other than that, it looks good, and I'm ready to approve once my questions are resolved 🚀 😄

pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Outdated Show resolved Hide resolved
pennylane/pauli/dla/center.py Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Thanks @dwierichs !

pennylane/pauli/dla/center.py Show resolved Hide resolved
@dwierichs dwierichs enabled auto-merge (squash) August 6, 2024 10:02
@dwierichs dwierichs merged commit 0ffd5d0 into master Aug 6, 2024
39 checks passed
@dwierichs dwierichs deleted the fix-center branch August 6, 2024 10:40
Shiro-Raven pushed a commit that referenced this pull request Aug 7, 2024
**Context:**
Linear combinations were not taken into account in `qml.center`, making
it miss center elements for some inputs.

**Description of the Change:**
Change the algorithm that computes the center.
Kudos to @therooler for the prototype.

**Benefits:**
Correct code.

**Possible Drawbacks:**
~~Slower performance for inputs that would have been fine before
anyways.~~
This has been fixed by reverting to the previous implementation if
`PauliWord` instances are passed only.

**Related GitHub Issues:**
Fixes #6048 
[sc-70014]

---------

Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
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.

[BUG] qml.center does not work for all linear combinations of Paulis
5 participants