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

Transform to split sums into single terms #5884

Merged
merged 32 commits into from
Jul 12, 2024
Merged

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Jun 20, 2024

Context:
Some simulators can handle non-commuting observables but don't take multi-term observables.

Description of the Change:
We add a transform that splits the observables on a tape up into single-term observables. This transform relies heavily on the no_grouping functionality already implemented in split_non_commuting. However, instead of splitting the measurements across many tapes, all of the measurements are returned on a single tape.

measurements = [qml.expval(qml.X(0) + 0.2 * qml.X(1)), qml.expval(qml.X(1) + qml.Z(1))]
tape = qml.tape.QuantumScript(measurements=measurements)
tapes, processing_fn = qml.transforms.split_to_single_terms(tape)
>>> tapes[0].measurements
[expval(X(0)), expval(X(1)), expval(Z(1))]
>>> dev = qml.device("default.qubit", wires=2)
>>> results = dev.execute(tapes)
>>> results
((0.0, 0.0, 1.0),)
>>> processing_fn(results)
(0.0, 1.0)

@lillian542
Copy link
Contributor Author

[sc-60187]

@lillian542
Copy link
Contributor Author

[sc-60188]

Copy link

codecov bot commented Jun 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (dbb26f2) to head (446e9eb).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5884      +/-   ##
==========================================
- Coverage   99.67%   99.67%   -0.01%     
==========================================
  Files         425      426       +1     
  Lines       40926    40660     -266     
==========================================
- Hits        40793    40526     -267     
- Misses        133      134       +1     

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

@lillian542 lillian542 modified the milestone: v0.37 Jun 20, 2024
@lillian542 lillian542 marked this pull request as ready for review July 9, 2024 21:25
@albi3ro albi3ro self-requested a review July 10, 2024 13:30
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.

All comments from me are minor and non-blocking. No concerns from my side :)

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.

Looks good to me! Although with this much overlap between this and split_non_commuting, I wonder if it is worth it to unify the two at some point in the future.

@lillian542
Copy link
Contributor Author

with this much overlap between this and split_non_commuting, I wonder if it is worth it to unify the two at some point in the future

Then we'd have to rename it, since this doesn't split things that don't commute... perhaps we could call it sum_expand 🤣

@lillian542
Copy link
Contributor Author

@trbromley I added a note on each transform that I hope clarifies

@lillian542 lillian542 enabled auto-merge (squash) July 11, 2024 18:50
@lillian542 lillian542 merged commit 50def12 into master Jul 12, 2024
40 checks passed
@lillian542 lillian542 deleted the split-sum-transform branch July 12, 2024 16:51
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.

4 participants