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

[Feature] Pyqtorch - First Order Adjoint Differentiation #155

Merged
merged 81 commits into from
Nov 17, 2023

Conversation

dominikandreasseitz
Copy link
Collaborator

@dominikandreasseitz dominikandreasseitz commented Oct 30, 2023

Fixes #97

This MR adds a "adjoint" differentiation mode to qadence which is a implementation of Algorithm 1 of https://arxiv.org/pdf/2009.02823.pdf.

Limitations:

  1. The adjoint method is only available in the pyqtorch backend
  2. Parametric observables are not supported
  3. Multiple observables are not supported
  4. Higher order derivatives are not natively supported by the adjoint method but torch.autograd can be used on the gradients returned by the adjoint method.
  5. Only expectation values can be differentiated, not wave functions

Additional changes:

  • Removal of gradient checkpointing (raising a UserWarning if requested)
  • Utility methods for the pyqtorch backends to convert/validate qadence/ pyqtorch states
  • Many new tests comparing gradients between ad, adjoint and gpsr
  • Rework of _set_backend_config
  • Added section for adjoint differentiation to examples/quantum_model.py

Future Todos:

  • Optimize HamEvo derivatives for time_evo and generators (currently done via finite differencing)

@dominikandreasseitz dominikandreasseitz added feature New feature or request performance labels Oct 30, 2023
@dominikandreasseitz dominikandreasseitz self-assigned this Oct 30, 2023
@dominikandreasseitz dominikandreasseitz marked this pull request as draft October 30, 2023 16:08
@dominikandreasseitz dominikandreasseitz changed the title [Feature] Adjoint Differentation [Feature] Pyqtorch - Adjoint Differentation Nov 1, 2023
@nmheim
Copy link
Collaborator

nmheim commented Nov 1, 2023

Are we moving adjoint diff from pyqtorch to qadence?

@dominikandreasseitz
Copy link
Collaborator Author

@nmheim, cant reply to your comment directly. we need our own qadence adjoint because we need to handle custom backward passes for nested circuits, and scaleblocks

@nmheim
Copy link
Collaborator

nmheim commented Nov 14, 2023

  • are we preventing user-level parameters somehow? I remember us talking about concerns about correctness in that case?

what are "user-facing" parameters to you? expression level? if yes, then yes, we need gate-level parameters.

just wondering how the gate-level params are enforced, is that done automatically when choosing the diff_mode?

Copy link
Collaborator

@madagra madagra left a comment

Choose a reason for hiding this comment

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

First part of the review. Overall, it looks great to me but there is one very important point missing here: documentation. I would at least add a small example in the getting started tutorial + a script in the examples/ folder.

qadence/backend.py Show resolved Hide resolved
qadence/backends/adjoint.py Show resolved Hide resolved
qadence/backends/adjoint.py Show resolved Hide resolved
qadence/backends/adjoint.py Show resolved Hide resolved
qadence/backends/adjoint.py Show resolved Hide resolved
@dominikandreasseitz
Copy link
Collaborator Author

  • are we preventing user-level parameters somehow? I remember us talking about concerns about correctness in that case?

what are "user-facing" parameters to you? expression level? if yes, then yes, we need gate-level parameters.

just wondering how the gate-level params are enforced, is that done automatically when choosing the diff_mode?

@nmheim here https://github.com/pasqal-io/qadence/blob/ds/adjoint/qadence/extensions.py#L97

Copy link
Collaborator

@madagra madagra left a comment

Choose a reason for hiding this comment

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

Thanks, @dominikandreasseitz. I am pre-approving this but I would add proper documentation before merging.

qadence/backends/braket/backend.py Show resolved Hide resolved
qadence/backends/braket/backend.py Show resolved Hide resolved
qadence/backends/pyqtorch/backend.py Outdated Show resolved Hide resolved
qadence/backends/pyqtorch/convert_ops.py Show resolved Hide resolved
qadence/backends/pyqtorch/convert_ops.py Show resolved Hide resolved
qadence/backends/utils.py Outdated Show resolved Hide resolved
qadence/extensions.py Outdated Show resolved Hide resolved
tests/backends/test_adjoint.py Show resolved Hide resolved
tests/backends/test_adjoint.py Show resolved Hide resolved
tests/backends/test_adjoint.py Show resolved Hide resolved
@RolandMacDoland RolandMacDoland self-requested a review November 17, 2023 10:58
Copy link
Collaborator

@RolandMacDoland RolandMacDoland left a comment

Choose a reason for hiding this comment

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

Very nice @dominikandreasseitz. It is much clearer for me after our discussion. Very minor comment for the sake of discussion. Otherwise LGTM.

qadence/backends/adjoint.py Outdated Show resolved Hide resolved
@dominikandreasseitz dominikandreasseitz merged commit e78a711 into main Nov 17, 2023
8 checks passed
@dominikandreasseitz dominikandreasseitz deleted the ds/adjoint branch November 17, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement adjoint differentiation mode
4 participants