Skip to content

Commit

Permalink
misc session fixes (Qiskit#252)
Browse files Browse the repository at this point in the history
closes Qiskit#247

---------

Co-authored-by: Jessie Yu <[email protected]>
  • Loading branch information
beckykd and jyu00 authored Oct 30, 2023
1 parent b327bfa commit e78ff21
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
30 changes: 24 additions & 6 deletions docs/run/run-jobs-in-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ backend to run on. This topic describes the most commonly used options.
For the full list, see the [Sessions API documentation](../api/qiskit-ibm-runtime/qiskit_ibm_runtime.Session#session).

<Admonition type="note" title="Important">
Data from the first session job is cached and used by subsequent jobs. Therefore, if the first job is canceled, subsequent session jobs will all fail.
If the first session job is canceled, subsequent session jobs will all fail.
</Admonition>


Expand Down Expand Up @@ -70,8 +70,8 @@ There are two ways to specify a backend in a session:
**Directly specify a string with the backend name.** Example:

``` python
backend = "ibmq_qasm_simulator"
with Session(backend=backend):
service = QiskitRuntimeService()
with Session(service=service, backend="ibmq_qasm_simulator"):
...
```

Expand Down Expand Up @@ -121,13 +121,31 @@ closed session.

This behavior exists in `qiskit-ibm-runtime` 0.13 or later releases only. Previously, `session.close()` **canceled** the session.

Example: The session is automatically closed when the context manager is exited.

``` python
with Session(service=service, backend=backend) as session:
estimator = Estimator()
job = estimator.run(...)
job1 = estimator.run(...)
job2 = estimator.run(...)

# The session is no longer accepting jobs but the submitted job will run to completion
result = job.result()
# The session is no longer accepting jobs but the submitted job will run to completion.
result = job1.result()
result2 = job2.result()
```

Example: The session is manually closed because we didn't use a context manager.

``` python
session = Session(backend=backend)
estimator = Estimator(session=session)
job1 = estimator.run(...)
job2 = estimator.run(...)
print(f"Result1: {job1.result()}")
print(f"Result2: {job2.result()}")

# Manually close the session. Running and queued jobs will run to completion.
session.close()
```

<span id="cancel"></span>
Expand Down
42 changes: 38 additions & 4 deletions docs/run/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ There are several benefits to using sessions:

<Admonition type="note" title="Notes">
- The queuing time does not decrease for the first job submitted within a session. Therefore, a session does not provide any benefits if you only need to run a single job.
- Since data from the first session job is cached and used by subsequent jobs, if the first job is canceled, subsequent session jobs will all fail.
- If the first session job is canceled, subsequent session jobs will all fail.
</Admonition>


Expand Down Expand Up @@ -142,7 +142,7 @@ Sessions can be used for iterative or batch execution.

Any session job submitted within the interactive timeout, also known as interactive time to live (ITTL), is processed immediately. This allows some time for variational algorithms, such as VQE, to perform classical post-processing.

- The quantum device is locked to the session user unless the ITTL is reached.
- When a session is active, its jobs get priority until ITTL or max timeout is reached.
- Post-processing could be done anywhere, such as a personal computer, cloud service, or an HPC environment.

![Iterative session execution diagram.](/images/run/iterative.png 'Figure 2: Iterative session execution')
Expand All @@ -151,21 +151,55 @@ Any session job submitted within the interactive timeout, also known as interact
There might be a limit imposed on the ITTL value depending on whether your hub is Premium, Open, and so on.
</Admonition>

This is an example of running an iterative workload that uses the classical SciPy optimizer to minimize a cost function. In this model, SciPy uses the output of the cost function to calculate its next input.

```python
def cost_func(params, ansatz, hamiltonian, estimator):
# Return estimate of energy from estimator

energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]
return energy

x0 = 2 * np.pi * np.random.random(num_params)

session = Session(backend=backend)

estimator = Estimator(session=session, options={"shots": int(1e4)})
res = minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator), method="cobyla")

# Close the session because we didn't use a context manager.
session.close()
```

### Batch

Ideal for running experiments closely together to avoid device drifts,
that is, to maintain device characterization.

- Suitable for batching many jobs together.
- Jobs that fit within the maximum session time run back-to-back on
hardware.
- The classical computation, such as compilation, of the jobs is run in parallel. This means running multiple jobs in a batch would be significantly faster than running them serially.

<Admonition type="note">
When batching, jobs are not guaranteed to run in the order they are submitted.
</Admonition>

![Batch session execution diagram.](/images/run/batch.png 'Figure 3: Batch session execution')

The following example shows how you can divide up a long list of circuits into multiple jobs and run them as a batch to take advantage of the parallel processing.

```python
backend = service.backend("ibm_sherbrooke")

with Session(backend=backend):
estimator = Estimator()
start_idx = 0
jobs = []
while start_idx < len(circuits):
end_idx = start_idx + backend.max_circuits
jobs.append(estimator.run(circuits[start_idx:end_idx], obs[start_idx:end_idx], params[start_idx:end_idx]))
start_idx = end_idx
```

## Sessions and reservations

IBM Quantum Premium users can access both reservations and sessions on
Expand Down

0 comments on commit e78ff21

Please sign in to comment.