-
Notifications
You must be signed in to change notification settings - Fork 162
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
Changes from 17 commits
d211315
1872ca0
f550d87
ccf0633
ea899ec
6b6f247
295f0fb
26dcec3
9d0038a
8d7c35a
470da63
d0ab583
61b1de6
7ebbf12
8d0de3f
1f0ff3d
25d6e45
64dba83
ada27f2
23092ff
1e9b641
8879a2d
9521be6
fc6f69d
23a16de
5ead385
fcf0372
57de435
2f1814e
e7454bd
c6631e5
caa648a
8a65ddc
19108ad
c18447b
1b25cbb
f10e366
65c9c6c
96ba7b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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`` | ||||||
===================================================================================== | ||||||
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
||||||
|
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`. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.