Skip to content

QNN Loss for ODEs#278

Merged
David-Kreplin merged 31 commits intosQUlearn:developfrom
RobertoFlorez:QNN_loss_ode_dev
Sep 30, 2024
Merged

QNN Loss for ODEs#278
David-Kreplin merged 31 commits intosQUlearn:developfrom
RobertoFlorez:QNN_loss_ode_dev

Conversation

@RobertoFlorez
Copy link
Contributor

Hi!

This is the implementation for the loss function for an ODE ODELoss (as done in https://arxiv.org/pdf/2011.10395). It should accept linear and non-linear 1st order and 2nd**[See footnote] order differential equations that depend on one variable.

I also created an example (ode_example) of how to use the interface and included a parameterized feature map (HEE_rzrxrz) that was used in the original paper to manipulate the function space.

The implementation interface accepts two type of inputs:

  1. A symbolic sympy expression for the homogenous differential equation:
  • If a sympy expression is given, then, the symbols_involved_in_ODE must be provided.
  1. A callable function:
  • If a callable function which implements the homogenous differential equation is given, then, a callable function for the gradient (ODE_functional_gradient) of the homogenous differential equation calculation must be provided.

About the Implementation:

This pull is a new class from LossBase that implements:

$$\mathcal{L}(\vec{\theta}) [ \ddot f, \dot f, f, x] = \sum_{j}^N \left(F_j[ \ddot f_{\vec{\theta}}, \dot f_{\vec{\theta}}, f_{\vec{\theta}}, x]\right)^2 + \eta(f_{\vec{\theta}}(x_0) -u_0)^2 + \eta(\dot f_{\vec{\theta}}(x_0)- \dot u_0)^2$$

With corresponding gradient:
image

where $F$, is the homogenous differential equation that can be given by the user. Also, it includes two ways to incorporate the initial value problem information based on the paper: floating and pinned

About the correctness:

I have benchmarked the gradient of the loss by comparing the finite-difference gradient with the one implemented using squlearnˈs parameter-shift rule (i.e previous equation):

Numerical Gradient:
image

Squlearn Gradient:
image

Also, I have solved some ODEs sucessfully,

image


Also, to use sympy I created three functions that are outside of the ODELoss class: numpyfy_sympy_loss , numerical_gradient_of_symbolic_equation and numpyfy_sympy_loss. I do not know if the current location of this function is the best one or if they should be methods, I am happy to hear your thoughts.

Feel free to give me feedback 😃 and thank you very much for your help! 🚀


*** 2nd order differential equations are implemented by differentiating twice the QNN trial function f(x), this implies that third order derivatives with regards to the parameters have to be calculated (i.e dfdxdxdp). As you may imagine this requires a lot of circuit evaluations (I believe for N points, around N^3 circuits must be evaluated). I do not think this is the proper way to solve second order ODE, one should translate a 2nd order to two coupled first orders.

I have not implemented coupled 1rst ODE solvers and this would be a possible next step, which I may do next. I could create an issue or something similar.

@RobertoFlorez RobertoFlorez changed the title QNN Loss for ODE QNN Loss for ODEs Jul 2, 2024
@David-Kreplin
Copy link
Collaborator

@rupof , is this branch ready for review or you are still working on it?
You can click "Ready for review" on the bottom of the page, if you are reaedy.

@RobertoFlorez RobertoFlorez marked this pull request as ready for review July 5, 2024 07:42
@RobertoFlorez
Copy link
Contributor Author

@David-Kreplin it is ready for review, I just changed it now. Thanks!

Copy link
Collaborator

@David-Kreplin David-Kreplin left a comment

Choose a reason for hiding this comment

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

Here are some comments on the code

Copy link
Contributor Author

@RobertoFlorez RobertoFlorez left a comment

Choose a reason for hiding this comment

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

Hi! I made the changes requested:

  • Order of the ODE is now checked through len(symbols_involved_in_ODE) and included a check at the initialization. Also checks if order is higher than 2.
  • Changed the name of initial_vec to initial_values for IVP information
  • Moved helper functions inside the class
  • Instead of HEE_rxrzrx, I created kyriienko_nonlinear_encoding which implements the feature maps used in the original paper (this implementation is slighly based on the chebyshev_tower.py).

Thanks for the feedback! Anything else that you think should be improved, I am happy to help :)

Copy link
Collaborator

@David-Kreplin David-Kreplin left a comment

Choose a reason for hiding this comment

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

Great work! I think we are almost there.
However, due to the recent updates in Squlearn, the example is currently broken. Simply adding the reshape command does not resolve the issue.

There are a few minor changes that I’ve also noted in the code:

  • The LaTeX rendering of the gradient in the ODE is broken.
  • An example of how to initialize the ODE loss would greatly improve the documentation.
  • I had difficulty understanding how to order the symbols_involved_in_ODE; perhaps you could provide a bit more detail on this.

@RobertoFlorez
Copy link
Contributor Author

RobertoFlorez commented Sep 27, 2024

Thank you very much David!

I did the following things:

  • Fixed broken tutorial ode_example
  • Fixed broken latex in loss.py
  • Included a new example in loss.py for ODELoss and slightly improved documentation. Also, added a new example in qnnr.py for fully solving a ODE

If you think that the documentation for the sympy symbols needs to be improved further let me know. Thanks!

Edit: because the example is taking so much time one check is throwing an error, I am searching for an alternative

Copy link
Collaborator

@David-Kreplin David-Kreplin left a comment

Choose a reason for hiding this comment

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

I think that's it, thank you very much!

@David-Kreplin David-Kreplin merged commit 241bdfd into sQUlearn:develop Sep 30, 2024
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