Description
What is the expected feature or enhancement?
Primitives can be defined without using a session, but later used within the context of a session. In this instance, the primitive should use the session provided instead of a default program.
For example:
estimator = Estimator()
sampler = Sampler()
with Session(service) as session:
estimator.run()
sampler.run()
Both primitives should use the session given by the context manager.
If it's possible to open two sessions at once e.g. for Quantum Federated Learning needing multiple backends, then it shouldn't overwrite sessions that are provided by a user.
Context: I'm training a hybrid QML model. If I build the model with standard primitives, I end up binding a session to a particular instance of a model. While this is fine for inference, if I'm training I need multiple sessions due to runtime session limits. This means that I have to regenerate my estimators every time and partially rebuild my model. Given the default session behaviour already, I think this is a fairly natural extension and should just require adding a check to the BasePrimitive._run_primitive()
method.
Acceptance criteria
The below code should use Session 1 for the first run and Session 2 for the second run.
estimator = Estimator()
sampler = Sampler()
with Session(service) as session_1:
estimator.run()
sampler.run()
with Session(service) as session_2:
estimator.run()
sampler.run()