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

Handle recursion errors in CompositeOp methods #6375

Merged
merged 12 commits into from
Oct 11, 2024
Merged

Conversation

astralcai
Copy link
Contributor

@astralcai astralcai commented Oct 9, 2024

By default, qml.sum and qml.prod set lazy=True, which keeps its operands nested. Given the recursive nature of such structures, if there are too many levels of nesting, a RecursionError would occur when accessing many of the properties and methods. Catching these errors and re-raising a more sensible error message suggesting that the user could either set lazy=False or use the + and @ operators instead, which sets lazy=False.

Update
Extending the behaviour to SProd

Fixes #5948
[sc-67745]

@astralcai astralcai marked this pull request as ready for review October 10, 2024 13:58
@astralcai astralcai requested a review from albi3ro October 10, 2024 14:01
Copy link

codecov bot commented Oct 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (dbb64fb) to head (1b523f2).
Report is 324 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6375      +/-   ##
==========================================
- Coverage   99.71%   99.39%   -0.32%     
==========================================
  Files         447      447              
  Lines       42365    42422      +57     
==========================================
- Hits        42244    42167      -77     
- Misses        121      255     +134     

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

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.

👍

I'm tempted to try and create a class decorator so we don't have to decorate every method and property, but I have a feeling that is too much black magic...

@astralcai
Copy link
Contributor Author

👍

I'm tempted to try and create a class decorator so we don't have to decorate every method and property, but I have a feeling that is too much black magic...

That was actually the first thing I tried, overriding the __getattribute__ of the class, but it was too much trouble getting it to work.

@astralcai astralcai merged commit 2078ff1 into master Oct 11, 2024
38 checks passed
@astralcai astralcai deleted the composite-op-recursion branch October 11, 2024 17:11
austingmhuang pushed a commit that referenced this pull request Oct 23, 2024
By default, `qml.sum` and `qml.prod` set `lazy=True`, which keeps its
operands nested. Given the recursive nature of such structures, if there
are too many levels of nesting, a `RecursionError` would occur when
accessing many of the properties and methods. Catching these errors and
re-raising a more sensible error message suggesting that the user could
either set `lazy=False` or use the `+` and `@` operators instead, which
sets `lazy=False`.

**Update**
Extending the behaviour to `SProd`

Fixes #5948
[sc-67745]
mudit2812 pushed a commit that referenced this pull request Nov 11, 2024
By default, `qml.sum` and `qml.prod` set `lazy=True`, which keeps its
operands nested. Given the recursive nature of such structures, if there
are too many levels of nesting, a `RecursionError` would occur when
accessing many of the properties and methods. Catching these errors and
re-raising a more sensible error message suggesting that the user could
either set `lazy=False` or use the `+` and `@` operators instead, which
sets `lazy=False`.

**Update**
Extending the behaviour to `SProd`

Fixes #5948
[sc-67745]
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] Recursion error when accessing operator built using qml.prod
3 participants