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

Add resources() to qml.TrotterProduct class #5680

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Jaybsoni
Copy link
Contributor

Context:
qml.TrotterProduct() now has error tracking functionality, but ins't compatible with resource tracking. This bug fix adds native resource tracking functionality to allow for compatibility with both pipelines.

image

Description of the Change:
Inherit from ResourcesOperation class as well and directly implement the resource method.

Benefits:
Allows for tracking resources without decomposing the template.

Possible Drawbacks:
If we change the target gate-set or want to decompose further, then this method is no longer accurate.

Related GitHub Issues:
Replicating most of this PR post release.

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.

@Jaybsoni Jaybsoni requested a review from trbromley May 10, 2024 21:19
@Jaybsoni
Copy link
Contributor Author

[sc-62523]

Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (0dcba44) to head (d22d046).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5680      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         415      415              
  Lines       38886    38610     -276     
==========================================
- Hits        38762    38485     -277     
- Misses        124      125       +1     

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

pennylane/templates/subroutines/trotter.py Outdated Show resolved Hide resolved
num_wires = len(self.wires)
num_gates = len(decomp)

unique_operation_decomp = (copy.deepcopy(op) for op in decomp)
Copy link
Contributor

Choose a reason for hiding this comment

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

I did ask it before, but once again, why do we call this unique_operation? It simply makes a generator. 🤔

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I mean by the name unique_operation_decomp is that this is a decomposition where each operation has been deep_copied so each one is unique (seperate hash) as opposed to the previous decomp where we were using multiple copies of the same operator.

We need "unique" deep-copies in order to properly count resources. Due to some legacy code in the circuit graph module.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that the deep copying changes the hash -
image.
Anyway, since it is legacy code, it doesn't matter much!

Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
Copy link
Contributor

@soranjh soranjh left a comment

Choose a reason for hiding this comment

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

Thanks @Jaybsoni, please note my minor comments before merging.

qml.TrotterProduct(hamiltonian, time, n=5, order=2)
return qml.expval(qml.Z(0))

tracked_resources = qml.specs(circ)()["resources"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we test with qml.Tracker too?

Comment on lines +115 to +117
* `qml.TrotterProduct` is now compatible with resource tracking by inheriting from `ResourcesOperation`.
[(#5680)](https://github.com/PennyLaneAI/pennylane/pull/5680)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why it is under Breaking changes? Is there a bug report for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh nice catch, let me move it to improvements

Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

Thanks @Jaybsoni!


assert expected_resources == tracked_resources


Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be curious to have a test that calculates resources and error at the same time.

Copy link
Contributor

@obliviateandsurrender obliviateandsurrender left a comment

Choose a reason for hiding this comment

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

Thanks @Jaybsoni, looks good to me! Optionally, you might want to add an integration test with qml.Tracker too.

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.

None yet

4 participants