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

Use qml.math.cast after qml.math.convert_like in qml.gradients to copy dtype #2120

Merged
merged 12 commits into from
Jan 25, 2022

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented Jan 22, 2022

Context:
qml.math.cast_like attempts to copy the dtype of a tensor whereas qml.math.convert_like only uses the type of the tensors during conversion

The qml.gradients module uses qml.math.convert_like. There may, however, be cases where the dtype would need to be copied, otherwise unexpected behaviour arises.

Description of the Change:

Benefits:

Possible Drawbacks:

Related GitHub Issues:
#2105

@github-actions
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.

@antalszava antalszava linked an issue Jan 24, 2022 that may be closed by this pull request
1 task
@antalszava antalszava changed the title Use qml.math.cast_like instead of qml.math.convert_like in qml.gradients Use qml.math.cast after qml.math.convert_like in qml.gradients to copy dtype Jan 24, 2022
@antalszava antalszava marked this pull request as ready for review January 24, 2022 21:24
@codecov
Copy link

codecov bot commented Jan 24, 2022

Codecov Report

Merging #2120 (a6e5250) into master (6d165f9) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2120   +/-   ##
=======================================
  Coverage   99.19%   99.19%           
=======================================
  Files         228      228           
  Lines       17476    17485    +9     
=======================================
+ Hits        17335    17344    +9     
  Misses        141      141           
Impacted Files Coverage Δ
pennylane/gradients/param_shift_hessian.py 100.00% <100.00%> (ø)
pennylane/gradients/vjp.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6d165f9...a6e5250. Read the comment docs.

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.

Looks good ! Only suggestion would be that the test added seems to be more like a workflow test, it might be good to have one or two tests which test the specific lines of code added directly (maybe a direct assertion comparing the dtype of result)

@antalszava antalszava merged commit f55d94b into master Jan 25, 2022
@antalszava antalszava deleted the compute_vjp_use_cast_like branch January 25, 2022 04:11
@@ -46,6 +46,7 @@ def compute_vjp(dy, jac, num=None):

if not isinstance(dy_row, np.ndarray):
jac = math.convert_like(jac, dy_row)
jac = math.cast(jac, dy_row.dtype)
Copy link
Member

Choose a reason for hiding this comment

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

very minor @antalszava, but I'm curious why cast_like wasn't used here, I thought it would be safer that using dy_row.dtype?

Copy link
Contributor Author

@antalszava antalszava Jan 25, 2022

Choose a reason for hiding this comment

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

qml.math.cast_like assumes that the two tensors are already of the same type if I'm not mistaken. I ran into issues using it with Torch, see this commit: 8d84766

@antalszava
Copy link
Contributor Author

[sc-13632]

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] Error for user-defined device dtype with the TF interface
3 participants