Skip to content

Commit

Permalink
Update broken threading code in Run-jobs-in-session (Qiskit#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
abbycross authored Jun 17, 2024
1 parent 249eb0a commit 1791639
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions docs/run/run-jobs-in-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,29 @@ with Session(backend=backend):
## Run two VQE algorithms in a session by using threading

You can get more out of a session by running multiple workloads simultaneously. The following example shows how you can run two VQE algorithms, each using a different classical optimizer, simultaneously inside a single session. Job tags are also used to differentiate jobs from each workload.

```python
from concurrent.futures import ThreadPoolExecutor
from qiskit_ibm_runtime import Session, EstimatorV2 as Estimator
from qiskit_ibm_runtime import EstimatorV2 as Estimator

def minimize_thread(estimator, method):
minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator), method=method)
return minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator), method=method)

with Session(backend=backend), ThreadPoolExecutor() as executor:
# Add tags to differentiate jobs from different workloads.
estimator1.options.environment.job_tags = "cobyla"
estimator1.options.environment.job_tags = "nelder-mead"
estimator1 = Estimator()
estimator2 = Estimator()

# Use different tags to differentiate the jobs.
estimator1.options.environment.job_tags = ["cobyla"]
estimator2.options.environment.job_tags = ["nelder-mead"]

# Submit the two workloads.
cobyla_future = executor.submit(minimize_thread, estimator1, "cobyla")
nelder_mead_future = executor.submit(minimize_thread, estimator2, "nelder-mead")

cobyla_result = executor.submit(minimize_thread, estimator1, "cobyla").result()
nelder_mead_result = executor.submit(minimize_thread, estimator2, "nelder-mead").result()
# Get workload results.
cobyla_result = cobyla_future.result()
nelder_mead_result = nelder_mead_future.result()
```

<span id="session-status"></span>
Expand Down

0 comments on commit 1791639

Please sign in to comment.