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

Efficient Gradients with the Adjoint Method #90

Closed
wants to merge 58 commits into from

Conversation

ebutler414
Copy link
Contributor

@ebutler414 ebutler414 commented Mar 16, 2023

Efficient Gradients with the Adjoint Method

As per email correspondence, the end UI could be improved and the explanations contained in the tutorial are in a pretty raw state, but the code is hopefully in less of a raw state ;)

I have left a lot of these tasks unchecked until the final form of the end UI has been decided

Best
Eoin

  • The contribution has been discussed and agreed on in the Issue section.
  • Code contributions do its best to follow the zen of python.
  • The automated test are all positive:
    • tox -e py36 (to run pytest) the code tests.
    • tox -e style (to run pylint) the code style tests.
    • tox -e docs (to run sphinx) generate the documentation.
  • Added test for changed/added code.
  • API code contributions include input checks (defensive code).
  • API code contributions include helpful error messages.
  • The documentation has been updated:
    • docstring for all new functions/methods/classes/modules.
    • consistent style of all docstrings.
    • for new modules: /docs/pages/modules.rst has been updated.
    • for api contributions: /docs/pages/api.rst has been updated.
    • for api contributions: tutorials and examples have been updated.

basically didn't change anything except added targtet state and reversed
the counting index.
However the legs are not pointing the correct direction: TODO

Also added the lists that store the forwardprop and backprop as well as
the lines that append the nodes to the list in the correct place
Each tensor in the backprop now has its legs swapped (either through
numpy swapaxes in _get_pt_mpos_backprop in the for loop, or
alternatively is transposed in the case of the system operators.

The network is propagated upwards as described in the visual diagrams, but
because of the axes swaps, time now runs downwards in those diagrams
Created the high level functions that the user will use, and a class to contain the gradient results.

Added the loop into the backprop that combines the forwardprop and backprpop tensors. This deviates from my private version of the code where this loop was entirely seperate to the forwardprop and backprop. The reason for this change is to allow me to delete the forwardprop tensors as I generate the backprop. This reduceds the amount of memory needed for the adjoint method by 50%.
I haven't debugged it but it has all of the methods it needs for now.
Did not debug this prob doesn't work, or even compile.
Only imports PT and v barebones right now
now get index for chain rule using interp1d which makes way more sense as it removes all of the floating point error bullshit

A lot of the old method by searching sorted lists was deleted in this commit so if you're missing something and you want to look how the previous method searched through a sorted list look at the previous commit
fixed the lines that store the backpropagated tensors and delete them accordingly

Also fixed a bug where in the backpropagation the indices were one ahead of what
they should have been, because since we are propagating backwards, the propagators
we are actually interested in are the ones associated with the previous step
in the forwardprop
Forgot that i need N(MPOs)+1 dprop lists, not N.
i had a z and an x mi8xed up, gradients still aren't correct, however they're
currently considerably better

They're wrong because i'm taking the derivative across two half timesteps,
but i'm not reintroducing the second when i'm combinging the derivatives with
dprop dsys, next update.....
finds two numbers that differ by one ascending in a list, and checks if the first one is even

this is the condition necessary for adjacent operators in the algorithm
added a brif sketch of what the code should look like when i add the ability to
use two propagators. this code is currently broken, debugging....
Basically made the previous algorithm i introduced defunct as i'm not convinced it
adds any time saving that would warrent the increase in complexity and modes of
failure of the code.

Instead added the extra node necessary during the chain rule, which i previously
forgot to add.

Whole thing is still bug, one is because i haven't treated the first and last
half propagator seperately as they're a special case, but there is more bugs
that i don't know about
I felt like what i was doing was too long-winded for a single tutorial so I
decided it'd be better to split it up into a very introductory tutorial and
then a seperate one to talk about optimisation and how that would be done.

This is in addition to a seperate tutorial that will talk about the chain rule
and finite differencing / autograd
where foreward and backprop lists are both specified. this wouldn't have
affected the other case where i delete the foreward and backprop lists
and just give total derivitives (which are still bugged)
but i don't like it i'm prob gonna change some stuff
I think it's currently bugged, this will be converted into a jupyter
notebook when it's done. it's currently pretty hardcoded.
improved dprop accessability and understandability, and tested it to make sure
it works properly
deleted script that had no purpouse other than a proof of concept
Basically reverted previous change and made the gradient function return the
dynamics object so can reuse adjoint calculation for different dprop lists
Updated the frontend comments / documentation

Added the retain forward-prop and back-prop user flag to the frontend
(previously it was just in the backend)
get_hamiltonian now makes way more sense.....
it's at the start of the tutorial

As piper suggested
Deleted redundant tutorials
@piperfw piperfw changed the title Effecient Gradients with the Adjoint Method Efficient Gradients with the Adjoint Method Mar 21, 2023
added ParametrizedSystem, as well as an example of how this script
should work. Most of this was originally written by gerald
This approach will break my previous code, but retains all of it's power
and is more succient.

This should work perfectly as is with gerald's example
Plan is for it to instead inherit from a propegator based class, seperate for
gradient code, that just supplies a list of propagators instead of a hamiltonian
The PropagatorSystem should be pretty functional, the other one, is very much not
@gefux
Copy link
Member

gefux commented May 4, 2024

This PR is a previous draft of #127.

@gefux gefux closed this May 4, 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.

2 participants