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

Allow using set_working_tape as a function decorator #177

Merged
merged 8 commits into from
Nov 22, 2024

Conversation

JHopeCollins
Copy link
Contributor

@JHopeCollins JHopeCollins commented Nov 19, 2024

Description

This PR uses the ContextDecorator class allow set_working_tape and stop_annotating to be used as function decorators.

The no_annotations decorator is now implemented as an instance of stop_annotating. Because no_annotations contexts could be nested, we need to keep track of the original annotation states at entry to each context separately. This is done by pushing/popping from a stack in __enter__ and __exit__.

set_working_tape decorator

The two functions below are equivalent:

@set_working_tape()
def decorated_function(*args, **kwargs):
    # do something here
    return ReducedFunctional(functional, control)

def context_function(*args, **kwargs):
    with set_working_tape():
        # do something here
        return ReducedFunctional(functional, control)

stop_annotating decorator

The two functions below are equivalent:

@stop_annotating()
def decorated_function(*args, **kwargs):
    # do something here

@no_annotations
def decorated_function(*args, **kwargs):
    # do something here

@JHopeCollins JHopeCollins self-assigned this Nov 19, 2024
  - remove `scoped_working_tape` instance, just use `set_working_tape()`
  - remove `eager` kwarg from `set_working_tape`, it avoids unnecessary Tape instantiations but is a minute optimisation at the expense of cleaner interface.
JHopeCollins and others added 2 commits November 22, 2024 13:05
…blank line.

Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
Co-authored-by: Connor Ward <c.ward20@imperial.ac.uk>
@JHopeCollins JHopeCollins merged commit 5f46e16 into master Nov 22, 2024
1 of 2 checks passed
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