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 parameter counting in DefaultQubitLegacy adjoint method #4820

Merged
merged 6 commits into from Nov 11, 2023

Conversation

dwierichs
Copy link
Contributor

Context:
The adjoint method of DefaultQubitLegacy uses two incompatible ways to quantify tape parameters: It sets up the number of tape parameters correctly using tape.get_parameters(trainable_only=False) but checks whether an operation comes with a parameter via grad_method is not None. This lead to a bug where, for example, QubitUnitary disrupts the parameter counting.

Description of the Change:
As all operations with more than one parameter are being decomposed/raised an error upon anyways, this PR changes the way to detect parametrized operations to op.num_params==1.

Benefits:
Bug fix: Correct adjoint method derivatives even when using QubitUnitary and other operations that have grad_method=None but have parameters.

Possible Drawbacks:
N/A

Related GitHub Issues:
N/A

@dwierichs
Copy link
Contributor Author

@vincentmr This is the PR (sorry, took a moment to figure this out after all)

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Even though its currently not causing issues, mind fixing this as well in the new system:

if op.grad_method is not None:

if op.grad_method is not None:

if op.grad_method is not None:

@dwierichs
Copy link
Contributor Author

Even though its currently not causing issues, mind fixing this as well in the new system:

@albi3ro Done :)

Copy link

codecov bot commented Nov 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (677fbe0) 99.64% compared to head (5f65309) 99.64%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4820      +/-   ##
==========================================
- Coverage   99.64%   99.64%   -0.01%     
==========================================
  Files         381      381              
  Lines       34377    34117     -260     
==========================================
- Hits        34256    33995     -261     
- Misses        121      122       +1     
Files Coverage Δ
pennylane/_qubit_device.py 99.44% <100.00%> (-0.01%) ⬇️
pennylane/devices/qubit/adjoint_jacobian.py 100.00% <100.00%> (ø)

... and 42 files with indirect coverage changes

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

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

great find, and simple fix!

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

👍 Thanks for getting this together so quickly.

@dwierichs dwierichs merged commit 768bea0 into master Nov 11, 2023
33 checks passed
@dwierichs dwierichs deleted the adjoint-param-counts branch November 11, 2023 14:12
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.

None yet

3 participants