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

Migration guide for IBMBackend.run #1214

Merged
merged 39 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d211315
First version of migration guide for backend.run()
merav-aharoni Nov 9, 2023
1872ca0
Typoes and spacing
merav-aharoni Nov 9, 2023
f550d87
Added examples of Sessions
merav-aharoni Nov 9, 2023
ccf0633
Spacing
merav-aharoni Nov 9, 2023
ea899ec
Spacing
merav-aharoni Nov 9, 2023
6b6f247
Spacing
merav-aharoni Nov 9, 2023
295f0fb
removed unnecessary quotes
merav-aharoni Nov 9, 2023
26dcec3
added missing quote
merav-aharoni Nov 9, 2023
9d0038a
Fixed code blocks
merav-aharoni Nov 13, 2023
8d7c35a
Updates in migration guide
merav-aharoni Nov 13, 2023
470da63
Release note
merav-aharoni Nov 13, 2023
d0ab583
Merge branch 'main' into migrate_backend_run
merav-aharoni Nov 13, 2023
61b1de6
Fixed error
merav-aharoni Nov 13, 2023
7ebbf12
Merge branch 'main' of github.com:merav-aharoni/qiskit-ibm-runtime in…
merav-aharoni Nov 13, 2023
8d0de3f
Merge branch 'migrate_backend_run' of github.com:merav-aharoni/qiskit…
merav-aharoni Nov 13, 2023
1f0ff3d
Added new file to toc
merav-aharoni Nov 13, 2023
25d6e45
Fixed spelling
merav-aharoni Nov 13, 2023
64dba83
Fixed spelling
merav-aharoni Nov 13, 2023
ada27f2
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
23092ff
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
1e9b641
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
8879a2d
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
9521be6
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
fc6f69d
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
23a16de
Update docs/migrate/backend_run_migration_guide.rst
merav-aharoni Nov 14, 2023
5ead385
Added explanations following review
merav-aharoni Nov 14, 2023
fcf0372
Added comments in the code
merav-aharoni Nov 14, 2023
57de435
More consistency in names, such as IBMBackend
merav-aharoni Nov 14, 2023
2f1814e
Merge branch 'main' into migrate_backend_run
merav-aharoni Nov 14, 2023
e7454bd
minor wording changes
kt474 Nov 14, 2023
c6631e5
Editted text
merav-aharoni Nov 15, 2023
caa648a
Merge with main
merav-aharoni Nov 15, 2023
8a65ddc
Update docs/migrate/backend_run_migration_guide.rst
kt474 Nov 15, 2023
19108ad
Update docs/migrate/backend_run_migration_guide.rst
kt474 Nov 15, 2023
c18447b
Update docs/migrate/backend_run_migration_guide.rst
kt474 Nov 15, 2023
1b25cbb
Update docs/migrate/backend_run_migration_guide.rst
kt474 Nov 15, 2023
f10e366
Update docs/migrate/backend_run_migration_guide.rst
kt474 Nov 15, 2023
65c9c6c
address remaining comments
kt474 Nov 15, 2023
96ba7b3
Merge branch 'main' into migrate_backend_run
kt474 Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Next steps
Use Sampler to design an algorithm <migrate/migrate-sampler>
Update parameter values while running <migrate/migrate-update-parm>
Algorithm tuning options (shots, transpilation, error mitigation) <migrate/migrate-tuning>

Migrate backend.run() from qiskit_ibm_provider to qiskit_ibm_runtime <migrate/backend_run_migration_guide>

.. toctree::
:maxdepth: 1
Expand Down
89 changes: 89 additions & 0 deletions docs/migrate/backend_run_migration_guide.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
Migration guide: Migrate ``backend.run()`` from ``qiskit_ibm_provider`` to ``qiskit_ibm_runtime``
=====================================================================================

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a short introduction here? It should explain qiskit_ibm_provider vs qiskit_ibm_runtime and why you need to migrate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Please have a look.

This guide describes how to migrate code that implemented ``backend.run()``
using Qiskit IBM Provider (the ``qiskit_ibm_provider`` package) to use
Qiskit IBM Runtime (``qiskit_ibm_runtime`` package).
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved
We demonstrate the migration with code examples.

**Example 1: Straightforward execution of backend.run()**

.. code-block:: python

from qiskit import *
from qiskit.compiler import transpile, assemble
circuit = QuantumCircuit(2, 2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()

In the Provider, the code is:
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
backend = provider.get_backend("ibmq_qasm_simulator")
transpiled_circuit = transpile(circuit, backend=backend)
job = backend.run(transpiled_circuit)
print(job.result())

In Runtime, the code will be:
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.backend("ibmq_qasm_simulator")
transpiled_circuit = transpile(circuit, backend=backend)
job = backend.run(transpiled_circuit)
print(job.result())

**Example 2: Execution of backend.run() within a session:**

This section of code is identical in Provider and in Runtime.

.. code-block:: python

with backend.open_session() as session:
job1 = backend.run(transpiled_circuit)
job2 = backend.run(transpiled_circuit)
print(job1.session_id)
print(job2.session_id)
backend.cancel_session()

The Session for ``Primitives`` (``Sampler`` and ``Estimator``) is currently different than
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved
the Session for ``IBMBackend``. Therefore, we cannot run a primitive and a backend
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the Session for ``IBMBackend``. Therefore, we cannot run a primitive and a backend
in ``IBMBackend``. Therefore, we cannot run a primitive and a backend

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand "...we cannot run a primitive and a backend using a single session". What does it mean to run a backend?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should be something like "we cannot run a primitive and use backend.run() within a single session,."

using a single Session.
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved

**Example 3: Primitive Session containing backend.run:**
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved

In this example, ``sampler`` is run within session, but ``backend`` is run independently
of ``session``.
kt474 marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

from qiskit_ibm_runtime import Session, Sampler
with Session(backend=backend) as session:
sampler = Sampler(session=session)
job1 = sampler.run(transpiled_circuit)
job2 = backend.run(transpiled_circuit) # runs outside the session
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what makes job2 run outside the session. Is it just 'kicked out' of the session because it uses backend.run? Why would it have a session_id (from the print command) if it runs outside the session?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an explanation and comments in the code. Please check if they are helpful.

print(job1.session_id)
print(job2.session_id)

**Example 4: Backend Session containing Sampler:**
merav-aharoni marked this conversation as resolved.
Show resolved Hide resolved

In this example, ``backend`` is run within a session, but ``sampler`` is run independently
of ``session``.
kt474 marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

with backend.open_session() as session:
sampler = Sampler(backend=backend)
job1 = sampler.run(transpiled_circuit) # runs outside the session
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I don't understand why this runs outside the session. Is it because it was instantiated with backend.open_session(), so that session just won't accept jobs that don't use backend.run?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

job2 = backend.run(transpiled_circuit)
session_id = session.session_id
print(job1.session_id)
print(job2.session_id)


4 changes: 4 additions & 0 deletions releasenotes/notes/migrate_backend_run-06c4b0579f28061a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
other:
- |
Added migration code for running ``backend.run`` in `qiskit_ibm_runtime` instead of in `qiskit_ibm_provider`.
Loading