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

Various fixes to the differentiation module #62

Merged
merged 11 commits into from
Jan 29, 2025
Merged

Conversation

BrunoLiegiBastonLiegi
Copy link
Contributor

This implements some fixes to differentiation:

  • Enables gradient wrt input for PSR
  • Provides encoders with a differentiable property
  • Improves Jax by using functools.partial instead of class attributes to avoid leakage

Copy link

codecov bot commented Jan 27, 2025

Codecov Report

Attention: Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.85%. Comparing base (c0dbfbc) to head (5cb04ad).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
src/qiboml/models/decoding.py 87.50% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
- Coverage   87.90%   87.85%   -0.06%     
==========================================
  Files          11       11              
  Lines         488      494       +6     
==========================================
+ Hits          429      434       +5     
- Misses         59       60       +1     
Flag Coverage Δ
unittests 87.85% <97.87%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/qiboml/interfaces/pytorch.py 97.36% <100.00%> (+0.03%) ⬆️
src/qiboml/models/encoding.py 97.91% <100.00%> (+0.29%) ⬆️
src/qiboml/operations/differentiation.py 96.38% <100.00%> (-0.05%) ⬇️
src/qiboml/models/decoding.py 96.47% <87.50%> (-1.18%) ⬇️

@BrunoLiegiBastonLiegi BrunoLiegiBastonLiegi marked this pull request as ready for review January 28, 2025 09:57
Copy link
Contributor

@MatteoRobbiati MatteoRobbiati left a comment

Choose a reason for hiding this comment

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

Thanks again @BrunoLiegiBastonLiegi for this!!
I just added some comments below.
Let's discuss them quickly and - once this is done - for me we are good to go.

@@ -16,6 +16,7 @@
"qiboml-pytorch": None,
"qiboml-tensorflow": Jax,
"qiboml-jax": Jax,
"numpy": Jax,
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we supposing here a user could configure the Numpy backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes since qibo is a dependency, and numpy its native backend.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes since qibo is a dependency, and numpy its native backend.

If Qibojit installed, the default should be Qibojit, no?

@@ -32,8 +35,14 @@ def circuit(
):
return self._circuit

@property
def differentiable(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't maybe safer to set this property as False by default (1) or we assume cases like binary encoder are more exceptional while the default should in general be differentiable (2)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would say it's more common to have a differentiable encoding.

)
)
else:
gradient.append(backend.np.zeros((decoding.output_shape[-1], x.shape[-1])))
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure we can just pad the gradient vector with values or zeros here.
It is probably safe until the model structure is: encoding -> circuit -> decoding, but as soon as we will have a mix of trainable and non trainable gates we should probably loop over the gates.
I say this because I guess the position of the gradients in the list is important, while here we are basically padding it at its beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In our design right now, the encoding is never trainable and all its gates are in the beginning. If the design will change, this will be updated accordingly, but I cannot modify this now predicting how the design will change.

@MatteoRobbiati MatteoRobbiati merged commit 96f7c47 into main Jan 29, 2025
26 checks passed
@MatteoRobbiati MatteoRobbiati deleted the diff_fixes branch January 29, 2025 09:16
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.

2 participants