Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Eager toggle decoration completion #155

Merged
merged 8 commits into from
Jan 31, 2022

Conversation

zaqqwerty
Copy link
Contributor

Add tf.function tracing tests.

Added the tracing to non-initialization tests of the various classes. Resolves #136.

@zaqqwerty zaqqwerty changed the title 136 add toggle decorator Eager toggle decoration completion Jan 31, 2022
@zaqqwerty zaqqwerty requested a review from farice January 31, 2022 06:52
Copy link
Contributor

@farice farice left a comment

Choose a reason for hiding this comment

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

Amazing!

@@ -158,11 +159,17 @@ def test_expectation(self):
all_ops = [x_ops, y_ops, z_ops]

# Check with reduce True (this is the default)
# TODO(#71): Decoration yields an error seemingly coming from TFQ:
# LookupError: gradient registry has no entry for: TfqAdjointGradient
Copy link
Contributor

Choose a reason for hiding this comment

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

in case it's helpful: I think this happens when one attempts to differentiate a TFQ gradient. I've seen this for Hessians

Copy link
Contributor Author

@zaqqwerty zaqqwerty Jan 31, 2022

Choose a reason for hiding this comment

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

Here there is just the Jacobian, so there should be no second derivatives, which is why this behavior is surprising. The documentation on custom_gradient says:

It should be noted tf.GradientTape is still watching the forward pass of a tf.custom_gradient, and will use the ops it watches. As a consequence, calling tf.function while the tape is still watching leads to a gradient graph being built. If an op is used in tf.function without registered gradient, a LookupError will be raised.

So it seems to be caused by some combination of tf.function decorating and custom_gradient calls in TFQ. But still, the confusing thing is that it does not happen for regular first derivatives, just jacobians.

@zaqqwerty zaqqwerty merged commit 771c9db into google:main Jan 31, 2022
farice pushed a commit that referenced this pull request Feb 1, 2022
farice added a commit that referenced this pull request Feb 2, 2022
* Enable tracing of QuantumCircuit __add__ (#131)

* Weighted average utility (#134)

* Add new style QHBMs (#125)

* Replace custom code with TF op (#141)

* Enable EBM seed (#143)

* Add modular Hamiltonian expectation (#135)

* Energy expectation (#150)

* Update TF, TFP, and TFQ versions (#152)

* try updating

* update python versions

* Tests passing

* increase tolerance for flaky test

* update setup.py

* Utility function tests (#153)

* Eager toggle decoration completion (#155)

* circuits test passing

* expectation test failing

* parameter change

* more tests

* start refactor

* add parameters to circuits

* test passing

* format

* remove lint

* revert superfluous changes

* Restart CI

* Update hamiltonian_infer_test.py

* address review comments

* restart CI

Co-authored-by: Faris Sbahi <farissbahi@gmail.com>
zaqqwerty added a commit that referenced this pull request Feb 3, 2022
* Enable tracing of QuantumCircuit __add__ (#131)

* Weighted average utility (#134)

* Add new style QHBMs (#125)

* Replace custom code with TF op (#141)

* Enable EBM seed (#143)

* Add modular Hamiltonian expectation (#135)

* start function

* function written

* start test fixture

* initial test completed

* test passing

* remove lint

* do not restrict to layers

* typo

* format

* Initial gradient function written

* Energy expectation (#150)

* Update TF, TFP, and TFQ versions (#152)

* try updating

* update python versions

* Tests passing

* increase tolerance for flaky test

* update setup.py

* tests passing again

* rempve extra layers

* generalize derivative code

* Enable tracing of QuantumCircuit __add__ (#131)

* Weighted average utility (#134)

* Add new style QHBMs (#125)

* Replace custom code with TF op (#141)

* Enable EBM seed (#143)

* Add modular Hamiltonian expectation (#135)

* Energy expectation (#150)

* Update TF, TFP, and TFQ versions (#152)

* try updating

* update python versions

* Tests passing

* increase tolerance for flaky test

* update setup.py

* Utility function tests (#153)

* Eager toggle decoration completion (#155)

* basic test passing again

* initial analytic expectation test

* add broken utils test

* weighted average now uses einsum

* tests passing

* tests passing

* format

* remove lint

* update args to tensors

* format

* add tf function togfgle to test

* remove lint

* extend linter timeout window

* remove tape argument

* check gradients are gapped away from zero

* change to atol

* change to atol

* tweak tolerances

* remove superfluous argument to inner function

* format

* update docstring

* add gradient graph test

* add todo

* format

* change docstring

* remove lint

Co-authored-by: Faris Sbahi <farissbahi@gmail.com>
farice added a commit that referenced this pull request Feb 15, 2022
* Enable tracing of QuantumCircuit __add__ (#131)

* Weighted average utility (#134)

* Add new style QHBMs (#125)

* Replace custom code with TF op (#141)

* Enable EBM seed (#143)

* Add modular Hamiltonian expectation (#135)

* Energy expectation (#150)

* Update TF, TFP, and TFQ versions (#152)

* try updating

* update python versions

* Tests passing

* increase tolerance for flaky test

* update setup.py

* Utility function tests (#153)

* Eager toggle decoration completion (#155)

* generalize QuantumInference expectation

* removed reduced tests

* inferece change

* revert

* genralize expectation

* circuit infer tests passing

* update infer

* checkout energy files

* format

* circuit tests passing

* updated h infer test passing

* circuit tests passing

* inference tests passing

* format

* remove lint

* vqt tests passing

* format

* remove lint

* start updating circuit infer

* infere

* update infere

* energy tests passing

* update infer

* move arg

* hamuiltonian inference tests passing

* format

* circuit infer test

* remove lint

* update vqt tets

* format

* tolerance update

* lint

Co-authored-by: Faris Sbahi <farissbahi@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test tracing and graph mode for inference functions
2 participants