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

improve builtins shape and ndim; add math.get_deep_interface #4603

Merged
merged 9 commits into from
Oct 3, 2023

Conversation

timmysilv
Copy link
Contributor

@timmysilv timmysilv commented Sep 15, 2023

Context:
Sometimes people do things on lists like [jnp.array(1.1), jnp.array(2.2)] and autoray thinks the interface is builtins. This happens a lot with Hamiltonian.coeffs. I'm here to help autoray.

Description of the Change:

  • Add qml.math.get_deep_interface to extract out the interface from one of those (arbitrarily) deeply nested lists of lists, or tuples of tuples. Note that I assume the struct isn't ragged, because if you're asking for the interface, you're probably about to cast the whole list and that can fail if it needs to
  • Change the builtins implementation for ndim and shape to check if it's a nested type containing a different interface. Then, cast to that interface and use its implementation for ndim/shape

Benefits:
We can get the ndim/shape of ugly nested things, and the helper function might be generally helpful elsewhere

Possible Drawbacks:
None!!!

Related GitHub Issues:
Fixes #4601

@timmysilv timmysilv marked this pull request as ready for review September 18, 2023 21:30
@timmysilv timmysilv requested a review from a team September 18, 2023 21:30
@timmysilv timmysilv changed the title improve builtins shape and ndim improve builtins shape and ndim; add math.get_deep_interface Sep 18, 2023
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
# Conflicts:
#	doc/releases/changelog-dev.md
@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (057c776) 99.63% compared to head (fb5340b) 99.63%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4603   +/-   ##
=======================================
  Coverage   99.63%   99.63%           
=======================================
  Files         376      376           
  Lines       33649    33665   +16     
=======================================
+ Hits        33525    33541   +16     
  Misses        124      124           
Files Coverage Δ
pennylane/math/__init__.py 100.00% <ø> (ø)
pennylane/math/single_dispatch.py 99.47% <100.00%> (+0.01%) ⬆️
pennylane/math/utils.py 99.18% <100.00%> (+0.04%) ⬆️

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

@timmysilv timmysilv merged commit d799923 into master Oct 3, 2023
39 checks passed
@timmysilv timmysilv deleted the better-builtins-ndim-shape branch October 3, 2023 17:35
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] Possible bug in Hamiltonian observable unflattening when using jax.jit
3 participants