You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how_to/run_session.rst
+23-24Lines changed: 23 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
Run a primitive in a session
2
2
=================================
3
3
4
-
There are several ways to set up and use sessions. The following information should not be considered mandatory steps to follow. Instead, choose the configuration that best suits your needs. To learn more about sessions, see `Introduction to sessions <../sessions.html>`__. This information assumes that you are using Qiskit Runtime `primitives <../primitives.html>`__.
4
+
There are several ways to set up and use sessions. The following information should not be considered mandatory steps to follow. Instead, choose the configuration that best suits your needs. To learn more about sessions, see `Introduction to sessions <../sessions.html>`__. This information assumes that you are using Qiskit Runtime `primitives <../primitives.html>`__.
5
5
6
6
Prerequisites
7
7
--------------
8
8
9
9
Before starting a session, you must `Set up Qiskit Runtime <https://qiskit.org/documentation/partners/qiskit_ibm_runtime/getting_started.html>`__ and initialize it as a service:
10
10
11
11
.. code-block:: python
12
-
12
+
13
13
from qiskit_ibm_runtime import QiskitRuntimeService
14
14
15
15
service = QiskitRuntimeService()
@@ -19,15 +19,15 @@ Open a session
19
19
20
20
You can open a runtime session by using the context manager `with Session(…)` or by initializing the `Session` class. When you start a session, you can specify options, such as the backend to run on. This topic describes the most commonly used options. For the full list, see the `Sessions API documentation <https://qiskit.org/documentation/partners/qiskit_ibm_runtime/stubs/qiskit_ibm_runtime.Session.html#qiskit_ibm_runtime.Session>`__.
21
21
22
-
.. important::
22
+
.. important::
23
23
Data from the first session job is cached and used by subsequent jobs. Therefore, if the first job is cancelled, subsequent session jobs will all fail.
24
24
25
25
**Session class**
26
26
27
27
A session can be created by initializing the `Session` class, which can then be passed to the desired primitives. Example:
@@ -37,7 +37,7 @@ A session can be created by initializing the `Session` class, which can then be
37
37
The context manager automatically opens a session for you. A session is started when the first primitive job in this context manager starts (not when it is queued). Primitives created in the context automatically use that session. Example:
38
38
39
39
.. code-block:: python
40
-
40
+
41
41
with Session(service=service, backend="ibmq_qasm_simulator"):
42
42
estimator = Estimator()
43
43
sampler = Sampler()
@@ -46,19 +46,19 @@ The context manager automatically opens a session for you. A session is started
46
46
Specify a backend
47
47
-----------------
48
48
49
-
When you start a session, you can specify session options, such as the backend to run on. A backend is required if you are using the IBM Quantum channel, but optional if you are using the IBM Cloud channel. Once specified, you cannot change the backend used for a session and you cannot specify multiple backends within a session. To use a different backend, open a new session.
49
+
When you start a session, you can specify session options, such as the backend to run on. A backend is required if you are using the IBM Quantum channel, but optional if you are using the IBM Cloud channel. Once specified, you cannot change the backend used for a session and you cannot specify multiple backends within a session. To use a different backend, open a new session.
50
50
51
51
There are two ways to specify a backend in a session:
52
52
53
-
**Directly specify a string with the backend name.** Example:
54
-
53
+
**Directly specify a string with the backend name.** Example:
54
+
55
55
.. code-block:: python
56
56
57
57
backend ="ibmq_qasm_simulator"
58
58
with Session(backend=backend):
59
59
...
60
60
61
-
**Pass the backend object.** Example:
61
+
**Pass the backend object.** Example:
62
62
63
63
.. code-block:: python
64
64
@@ -74,43 +74,42 @@ When a session is started, it is assigned a maximum session timeout value. After
74
74
75
75
You can configure the maximum session timeout value through the `max_time` parameter, which can be specified as seconds (int) or a string, like "2h 30m 40s". This value has to be greater than the `max_execution_time` of the job and less than the system’s `max_time`. The default value is the system’s `max_time`. See `What is the maximum execution time for a Qiskit Runtime job? <../faqs/max_execution_time.html>`__ to determine the system limit.
76
76
77
-
When setting the session length, consider how long each job within the session might take. For example, if you run five jobs within a session and each job is estimated to be five minutes long, the maximum time for the session should at least 25 min.
77
+
When setting the session length, consider how long each job within the session might take. For example, if you run five jobs within a session and each job is estimated to be five minutes long, the maximum time for the session should at least 25 min.
78
78
79
79
.. code-block:: python
80
80
81
81
with Session(service=service, backend=backend, max_time="25m"):
82
-
...
82
+
...
83
83
84
-
There is also an interactive timeout value (5 minutes), which is not configurable. If no session jobs are queued within that window, the session is temporarily deactivated. For more details about session length and timeout, see `How long a session stays active <../sessions.html#active>`__.
84
+
There is also an interactive timeout value (5 minutes), which is not configurable. If no session jobs are queued within that window, the session is temporarily deactivated. For more details about session length and timeout, see `How long a session stays active <../sessions.html#active>`__.
85
85
86
86
.. _close session:
87
-
87
+
88
88
Close a session
89
89
---------------
90
90
91
-
When jobs are all done, it is recommended that you use `session.close()` to close the session. This allows the scheduler to run the next job without waiting for the session timeout, therefore making it easier for everyone. You cannot submit jobs to a closed session.
91
+
When jobs are all done, it is recommended that you use `session.close()` to close the session. This allows the scheduler to run the next job without waiting for the session timeout, therefore making it easier for everyone. You cannot submit jobs to a closed session.
92
92
93
-
.. warning::
94
-
Close a session only after all session jobs **complete**, rather than immediately after they have all been submitted. Session jobs that are not completed will fail.
93
+
.. warning::
94
+
Close a session only after all session jobs **complete**, rather than immediately after they have all been submitted. Session jobs that are not completed will fail.
95
95
96
96
.. code-block:: python
97
97
98
98
with Session(service=service, backend=backend) as session:
99
-
...
100
-
estimator = Estimator()
101
-
job = estimator.run(...)
102
-
# Do not close here, the job might not be completed!
103
-
result = job.result()
104
-
# Reaching this line means that the job is finished.
105
-
session.close()
99
+
estimator = Estimator()
100
+
job = estimator.run(...)
101
+
# Do not close here, the job might not be completed!
102
+
result = job.result()
103
+
# job.result() is blocking, so this job is now finished and the session can be safely closed.
104
+
session.close()
106
105
107
106
Full example
108
107
------------
109
108
110
109
In this example, we start a session, run an Estimator job, and output the result:
111
110
112
111
.. code-block:: python
113
-
112
+
114
113
from qiskit.circuit.random import random_circuit
115
114
from qiskit.quantum_info import SparsePauliOp
116
115
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Estimator, Options
0 commit comments