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

[Terms1] Add Prod.terms() method #5132

Merged
merged 43 commits into from
Feb 8, 2024
Merged

[Terms1] Add Prod.terms() method #5132

merged 43 commits into from
Feb 8, 2024

Conversation

Qottmann
Copy link
Contributor

@Qottmann Qottmann commented Jan 31, 2024

The overall goal is to have Sum.terms() give meaningful results. This is necessary to achieve feature parity with qml.Hamiltonian to be able to soon switch to new opmath be the default. We realized that a necessity for Sum.terms() to work properly we also need a Prod.terms() method that is compatible with that.

In particular with this PR you can do:

>>> qml.operation.enable_new_opmath()
>>> op = X(0) @ (0.5 * X(1) + X(2))
>>> op.terms()
([0.5, 1.0],
 [PauliX(wires=[1]) @ PauliX(wires=[0]),
  PauliX(wires=[2]) @ PauliX(wires=[0])])

You can think of it like this: When the operator has a pauli_rep its just taking all values as coeffs and all keys as operators. This PR additionally handles all cases where no pauli rep is available. In essence, it is doing the same steps as simplify modulo some small differences (like not discerning cases of just one single term as that still yields [factor], [op]).

  • Basic functionality
  • Tests with Paulis
  • Tests without Paulis
  • Docs
  • changelog

Copy link
Contributor

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.

@Qottmann
Copy link
Contributor Author

[sc-55894]

@Qottmann Qottmann mentioned this pull request Jan 31, 2024
6 tasks
@Qottmann Qottmann changed the title Adding Prod.terms() method [WIP] Adding Prod.terms() method Jan 31, 2024
Copy link

codecov bot commented Feb 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d5e3cca) 99.69% compared to head (e862671) 99.68%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5132      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         394      394              
  Lines       36118    35860     -258     
==========================================
- Hits        36007    35748     -259     
- Misses        111      112       +1     

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

Base automatically changed from conversion-timings to master February 2, 2024 20:40
@Qottmann Qottmann changed the title Adding Prod.terms() method [Terms1] Add Prod.terms() method Feb 5, 2024
@Qottmann Qottmann requested review from albi3ro and mudit2812 February 5, 2024 10:48
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.

Looks good! I left a couple of comments, but mostly looking pretty approve-ready

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.

Looks good to me! Thanks Korbinian

@Qottmann Qottmann requested review from trbromley and albi3ro and removed request for albi3ro February 6, 2024 19:28
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Thanks! Much appreciated to have this change :)

@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0428922) 99.68% compared to head (e722932) 99.67%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5132      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         394      394              
  Lines       36298    36040     -258     
==========================================
- Hits        36183    35924     -259     
- Misses        115      116       +1     

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

@trbromley
Copy link
Contributor

Are there similar performance questions here as in #5133?

@Qottmann
Copy link
Contributor Author

Qottmann commented Feb 7, 2024

Are there similar performance questions here as in #5133?

Yep same same

@albi3ro albi3ro merged commit 69531f8 into master Feb 8, 2024
@albi3ro albi3ro deleted the prodterms branch February 8, 2024 18:38
albi3ro added a commit that referenced this pull request Feb 9, 2024
Builds on top of #5132 and
adds a meaningul `Sum.terms()` method that returns a tuple of
coefficients and ops to be at feature parity with `qml.Hamiltonian`

- [x] Basic functionality
- [x] Tests with Paulis
- [x] Tests without Paulis
- [x] Docs
- [x] changelog
- [x] handle empty pauli words

---------

Co-authored-by: albi3ro <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Qottmann added a commit that referenced this pull request Feb 22, 2024
…5164)

Following #5132 and
#5133, the overarching goal
is to achieve feature parity with `qml.Hamiltonian`, which on the other
hand is part of the overarching goal of activating new opmath by
default.

While for `qml.Hamiltonian` it made sense to access `coeffs` and `ops`
directly, in `Sum` and `Prod` it requires some processing so it makes
more sense (and costs less) to directly call the `terms()` method to
obtain both.


[sc-56284]
[sc-53963]

---------

Co-authored-by: albi3ro <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <christina@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.

5 participants