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 TrotterProduct fail for a Hamiltonian with identical coefficients #5073

Merged
merged 18 commits into from
Feb 2, 2024

Conversation

soranjh
Copy link
Contributor

@soranjh soranjh commented Jan 17, 2024

Context:

Fixes this bug. The input hamiltonian in TrotterProduct is internally converted to a Sum. But a Hamiltonian with identical coefficients (=! 1) created internally with qml.dot(coeffs, ops) has type SProd and gives an error.

Description of the Change:
The following addition to TrotterProduct fixes the issue:

if isinstance(hamiltonian, SProd):
    hamiltonian = hamiltonian.simplify()

Benefits:

Possible Drawbacks:

Related GitHub Issues:

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.

@soranjh soranjh changed the title Add support for SProd in TrotterProduct Fix TrotterProduct fail for a Hamiltonian with identical coefficients Jan 17, 2024
@soranjh soranjh added the bug 🐛 Something isn't working label Jan 17, 2024
Copy link

codecov bot commented Jan 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (35af47f) 99.69% compared to head (d35e952) 99.68%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5073      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         394      394              
  Lines       36073    35806     -267     
==========================================
- Hits        35962    35694     -268     
- Misses        111      112       +1     

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

Copy link
Contributor

@KetpuntoG KetpuntoG left a comment

Choose a reason for hiding this comment

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

Pending passing all the GitHub tests, it looks great and It works correctly

@soranjh
Copy link
Contributor Author

soranjh commented Jan 29, 2024

[sc-54033]

Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

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

I suggest a slightly different solution to preserve the interface.

Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

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

💯

@soranjh soranjh merged commit 70e64b5 into master Feb 2, 2024
35 checks passed
@soranjh soranjh deleted the trotter_sprod_bugfix branch February 2, 2024 20:21
albi3ro added a commit that referenced this pull request Feb 12, 2024
)

[sc-55330]

See also PR #5073 and Issue #5066 . Sometimes `qml.dot` was returning a
scalar product between a single coefficient and a sum, instead of
returning a sum. `TrotterProduct` was not able to handle this sort of
nested operator.

While we should also make our code more flexible in handling nested
operators, we can also standardize the return of `qml.dot`.
Gabriel-Bottrill pushed a commit to QSAR-UBC/pennylane_qutrit_mixed that referenced this pull request Feb 14, 2024
…nnyLaneAI#5143)

[sc-55330]

See also PR PennyLaneAI#5073 and Issue PennyLaneAI#5066 . Sometimes `qml.dot` was returning a
scalar product between a single coefficient and a sum, instead of
returning a sum. `TrotterProduct` was not able to handle this sort of
nested operator.

While we should also make our code more flexible in handling nested
operators, we can also standardize the return of `qml.dot`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants