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

Add serverless guide with function template example: hamiltonian simu… #2300

Merged
merged 23 commits into from
Dec 18, 2024

Conversation

frankharkins
Copy link
Member

Continuation of #2276

…lation (#2276)

The very rough outline is intact, including the code, but I still need
to write/revise some prose between the cells. Right now this example
uses the `mergedeep` package to merge the user's estimator options with
the function's default. If we need to remove this dependency, then I
will modify the program so that if a user passes estimator options, they
completely replace the function's default.

Closes #2242.

---------

Co-authored-by: Jen Glick <[email protected]>
Co-authored-by: Rebecca Dimock <[email protected]>
Co-authored-by: Rebecca Dimock <[email protected]>
@qiskit-bot
Copy link
Contributor

One or more of the following people are relevant to this code:

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@garrison
Copy link
Member

This will close #2242 when it is merged.

@garrison
Copy link
Member

I opened #2302 since I don't have permission to push to this repository.

garrison and others added 2 commits November 12, 2024 21:36
I am not able to push directly to the branch in #2300, so I opened this
instead.
@abbycross
Copy link
Collaborator

I recommend we rename the file something more human-readable, like /serverless-function-template

@pandasa123
Copy link
Collaborator

@frankharkins, @garrison, @jenglick I'd recommend adding two things:

  1. Handling SIGTERM to clean up gracefully, if a user decides to cancel their workload, Example below:
import traceback
import signal
from qiskit_serverless import save_result

class FunctionTemplate:

    def signal_handler(self, signum, frame):
        """Handle termination signals."""
        signame = signal.Signals(signum).name
        logger.info("Received %s signal, shutting down...", signame)
        for job in self._jobs:
            try:
                job.cancel()
            except Exception as exc:
                # The cancel will give a RuntimeInvalidStateError if the job has already
                # finished, but it's more efficient to always cancel than to check for status first.
                if not isinstance(exc, RuntimeInvalidStateError):
                    logger.error("Unable to cancel Qiskit Runtime job %s", job.job_id())
        self._session.close()


class Runner:
    def run(self, arguments: dict) -> dict:
        try:
            func = FunctionTemplate(**arguments)
            signal.signal(signal.SIGTERM, func.signal_handler)
            return func.run()
        except Exception:
            save_result(traceback.format_exc())
            raise
  1. In job.result(), adding a metadata property to output that neatly lists arguments passed in (akin to Runtime)
# job.result()

{
     'twoqubit_depth': ...,
     'metadata': {
          'hamiltonian': ...,
          'backend_name': ...,
          ... 
     }
}

@garrison
Copy link
Member

2. In job.result(), adding a metadata property to output that neatly lists arguments passed in (akin to Runtime)

This is a great idea.

  1. Handling SIGTERM to clean up gracefully

@pandasa123, It's not entirely clear to me how to integrate this code snippet with the function. How are the two classes to be used?

But also: I wonder if it might be better for serverless to expose an abstraction that does this, rather than people needing to copy and paste a snippet of code like this.

…#2504)

I originally wanted to be able to validate that this is the same as the
code displayed above. I had planned to do this using a cell with a
`%%writefile` directive, like is above in this file. Given this, I could
have asserted that the two files are equivalent. Unfortunately I don't
think it is possible to use Jupyter directives like this within an HTML
block in a notebook.
Follow-up to #2504.

---------

Co-authored-by: Eric Arellano <[email protected]>
@beckykd beckykd self-requested a review December 18, 2024 21:37
@beckykd beckykd added this pull request to the merge queue Dec 18, 2024
Merged via the queue into main with commit bbbc6a9 Dec 18, 2024
5 checks passed
@beckykd beckykd deleted the FH/preview-fct-template branch December 18, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add guide in serverless section with function template example
8 participants