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 probs.process_state with tf.function and batching #4590

Merged
merged 2 commits into from
Sep 12, 2023

Conversation

timmysilv
Copy link
Contributor

@timmysilv timmysilv commented Sep 12, 2023

Context:
Tensorflow Autograph forces discipline upon me that I do not want! It doesn't like when something returns None sometimes and a tuple at other times. I thought I fixed Probability.process_state in #4470 because a test failed with tf.function. Unfortunately, I put the code into a state where Autograph could no longer properly compile it, so it traced all Tensors with shape <unknown>, and that made anything shape-related afterwards fail. This includes qml.math.dot, which expval.process_state does. Though I knew this was related to the problem, only yesterday was I able to single it out. And last night I realized, I ought not try to invent a fix. I should just copy from default.qubit(.tf)! So this PR takes code structure from there.

Description of the Change:
Re-organize Probability.process_state so it allows for batching and TF-Autograph support. This adds a try-expect to ProbabilityMP._get_batch_size, but it's what DefaultQubitTF was doing before (see link above).

Benefits:
Everything works! I am unblocked in replacing DQL with DQ2 because this broke KerasLayer with model.save!

Possible Drawbacks:
The extra try-except? I also changed the prototype for ProbabilityMP.marginal_prob (I removed batch_size) but it isn't used anywhere else

@timmysilv timmysilv requested a review from a team September 12, 2023 14:56
@rmoyard rmoyard self-requested a review September 12, 2023 15:19
Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

Great bug fix 💯

@timmysilv timmysilv enabled auto-merge (squash) September 12, 2023 17:37
@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

Patch coverage: 83.33% and project coverage change: -0.01% ⚠️

Comparison is base (1f93032) 99.65% compared to head (3560e90) 99.65%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4590      +/-   ##
==========================================
- Coverage   99.65%   99.65%   -0.01%     
==========================================
  Files         376      376              
  Lines       33159    33163       +4     
==========================================
+ Hits        33046    33047       +1     
- Misses        113      116       +3     
Files Changed Coverage Δ
pennylane/measurements/probs.py 97.08% <83.33%> (-2.92%) ⬇️

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

@timmysilv timmysilv merged commit ced8f1d into master Sep 12, 2023
38 of 39 checks passed
@timmysilv timmysilv deleted the fix-probs-process-state-tf branch September 12, 2023 17:38
mudit2812 pushed a commit that referenced this pull request Sep 12, 2023
**Context:**
Tensorflow Autograph forces discipline upon me that I do not want! It
doesn't like when something returns `None` sometimes and a tuple at
other times. I thought I fixed `Probability.process_state` in #4470
because a test failed with `tf.function`. Unfortunately, I put the code
into a state where Autograph could no longer properly compile it, so it
traced all Tensors with shape `<unknown>`, and *that* made anything
shape-related afterwards fail. This includes `qml.math.dot`, which
`expval.process_state` does. Though I knew this was related to the
problem, only yesterday was I able to single it out. And last night I
realized, I ought not try to invent a fix. I should just copy from
`default.qubit(.tf)`! So this PR takes code structure from
[there](https://github.com/PennyLaneAI/pennylane/blob/1f93032a5f79ea0b9076d456b5b6c30515ef347d/pennylane/devices/default_qubit_tf.py#L189-L205).

**Description of the Change:**
Re-organize `Probability.process_state` so it allows for batching _and_
TF-Autograph support. This adds a try-expect to
`ProbabilityMP._get_batch_size`, but it's what DefaultQubitTF was doing
before (see link above).

**Benefits:**
Everything works! I am unblocked in replacing DQL with DQ2 because this
broke `KerasLayer` with `model.save`!

**Possible Drawbacks:**
The extra try-except? I also changed the prototype for
`ProbabilityMP.marginal_prob` (I removed `batch_size`) but it isn't used
anywhere else
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.

3 participants