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 dimension ordering bug with sum_expand #5702

Merged
merged 10 commits into from
May 24, 2024
Merged

Fix dimension ordering bug with sum_expand #5702

merged 10 commits into from
May 24, 2024

Conversation

astralcai
Copy link
Contributor

Context:
sum_expand incorrectly order the dimensions when combining shot vectors, multiple measurements, and parameter broadcasting. Additionally, qml.math.dot raises an error when when only one of the operands is a scaler. For example:

>>> import pennylane.numpy as np
>>> arr = np.array([1,2,3])
>>> qml.math.dot(arr, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/astral.cai/Workspace/pennylane/pennylane/math/multi_dispatch.py", line 151, in wrapper
    return fn(*args, **kwargs)
  File "/Users/astral.cai/Workspace/pennylane/pennylane/math/multi_dispatch.py", line 365, in dot
    return np.tensordot(x, y, axes=[[-1], [-2]], like=like)
  File "/Users/astral.cai/Workspace/pennylane/venv/lib/python3.9/site-packages/autoray/autoray.py", line 80, in do
    return get_lib_fn(backend, fn)(*args, **kwargs)
  File "/Users/astral.cai/Workspace/pennylane/pennylane/numpy/wrapper.py", line 117, in _wrapped
    res = obj(*args, **kwargs)
  File "/Users/astral.cai/Workspace/pennylane/venv/lib/python3.9/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "/Users/astral.cai/Workspace/pennylane/venv/lib/python3.9/site-packages/numpy/core/numeric.py", line 1091, in tensordot
    if as_[axes_a[k]] != bs[axes_b[k]]:
IndexError: tuple index out of range

Description of the Change:

  • Fixes the bug with sum_expand.
  • Updates dot in to use * if either operand is a scaler.

Related GitHub Issues:
Fixes #5700
[sc-63541]

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.

Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (58668d7) to head (03a8c2b).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5702      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         416      416              
  Lines       39049    38810     -239     
==========================================
- Hits        38925    38685     -240     
- Misses        124      125       +1     

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

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks @astralcai, nice upgrade + bug fix in one! 🎉
Could I ask you to add unit tests in tests/math/test_functions for the newly supported case(s) in qml.math.dot? :) Happy to approve otherwise!

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/math/multi_dispatch.py Outdated Show resolved Hide resolved
pennylane/math/multi_dispatch.py Outdated Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
@astralcai astralcai requested a review from dwierichs May 22, 2024 21:19
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks @astralcai 🎉

@lillian542 lillian542 self-requested a review May 23, 2024 18:49
@astralcai astralcai requested a review from lillian542 May 23, 2024 19:41
Copy link
Contributor

@lillian542 lillian542 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 @astralcai 🚀

@astralcai astralcai enabled auto-merge (squash) May 23, 2024 20:31
@astralcai astralcai merged commit c5def2a into master May 24, 2024
40 checks passed
@astralcai astralcai deleted the sum-expand-bug branch May 24, 2024 15:03
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] sum_expand producing incorrect result dimensions
3 participants