224 lines
5.9 KiB
Plaintext
224 lines
5.9 KiB
Plaintext
---
|
|
title: Migrate from qiskit-ibm-provider
|
|
description: Migrate from backend.run in qiskit-ibm-provider to using Qiskit Runtime primitives
|
|
|
|
---
|
|
|
|
# Migrate from `qiskit_ibm_provider`
|
|
|
|
This topic shows how to migrate code that implemented `IBMBackend.run()`
|
|
by using `qiskit_ibm_provider` to use `qiskit_ibm_runtime`.
|
|
|
|
## Save accounts
|
|
|
|
Use the updated code to save accounts.
|
|
|
|
<Tabs>
|
|
<TabItem value="updated" label="Runtime primitives">
|
|
The new syntax accepts credentials for
|
|
Qiskit Runtime on IBM Cloud or IBM Quantum Platform. For more
|
|
information on retrieving account credentials, see [Install and set up](/guides/setup-channel).
|
|
|
|
``` python
|
|
from qiskit_ibm_runtime import QiskitRuntimeService
|
|
|
|
# IBM Quantum channel; set to default
|
|
|
|
QiskitRuntimeService.save_account(channel="ibm_quantum", token="<IQP_TOKEN>", overwrite=True, set_as_default=True)
|
|
```
|
|
|
|
Additionally, you can now name your saved credentials and load the credentials by name.
|
|
|
|
|
|
``` python
|
|
# Save different accounts for open and premium access
|
|
|
|
QiskitRuntimeService.save_account(channel="ibm_quantum", token="<IQX_TOKEN>", instance="h1/g1/p1", name="premium")
|
|
QiskitRuntimeService.save_account(channel="ibm_quantum", token="<IQX_TOKEN>", instance="h2/g2/p2", name="open")
|
|
|
|
# Load the "open" credentials
|
|
|
|
service = QiskitRuntimeService(name="open")
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="legacy" label="backend.run">
|
|
``` python
|
|
IBMProvider.save_account(token='MY_API_TOKEN')
|
|
```
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
|
|
## Load accounts
|
|
|
|
Use the updated code to load accounts.
|
|
|
|
<Tabs>
|
|
<TabItem value="updated" label="Runtime primitives">
|
|
The `channel` input parameter is
|
|
optional. If multiple accounts have been saved in one device and no
|
|
`channel` is provided, the default is `"ibm_cloud"`.
|
|
|
|
``` python
|
|
# To access saved credentials for the IBM cloud channel
|
|
service = QiskitRuntimeService(channel="ibm_cloud")
|
|
|
|
# To access saved credentials for the IBM quantum channel
|
|
service = QiskitRuntimeService(channel="ibm_quantum")
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="legacy" label="backend.run">
|
|
``` python
|
|
provider = IBMProvider()
|
|
```
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
|
|
## Instance selection (get a provider)
|
|
|
|
Use the updated code to select a hub, group, and project.
|
|
|
|
<Tabs>
|
|
<TabItem value="updated" label="Runtime primitives">
|
|
When using the `ibm_quantum` channel,
|
|
the `hub`, `group`, and `project` are specified through the new
|
|
`instance` keyword.
|
|
|
|
``` python
|
|
# To access saved credentials for the IBM quantum channel and select an instance
|
|
service = QiskitRuntimeService(channel="ibm_quantum", instance="my_hub/my_group/my_project")
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="legacy" label="backend.run">
|
|
``` python
|
|
provider = IBMProvider(instance="my_hub/my_group/my_project")
|
|
|
|
```
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
## Get a backend
|
|
|
|
Use the updated code to specify a backend.
|
|
|
|
<Tabs>
|
|
<TabItem value="updated" label="Runtime primitives">
|
|
|
|
``` python
|
|
# You can specify the instance in service.backend() instead of initializing a new service
|
|
backend = service.backend("ibm_backend", instance="h1/g1/p1")
|
|
```
|
|
If you don't know what backend you want to use, you can instead use code such as the following:
|
|
|
|
```python
|
|
from qiskit_ibm_runtime import QiskitRuntimeService
|
|
|
|
service = QiskitRuntimeService()
|
|
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=<num_qubits>)
|
|
```
|
|
|
|
With Qiskit Runtime, you can also run in local testing mode.
|
|
|
|
```python
|
|
# Define a local backend
|
|
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
|
|
backend = FakeManilaV2()
|
|
|
|
# You could use an Aer simulator instead by using the following code:
|
|
# from qiskit_aer import AerSimulator
|
|
# backend = AerSimulator()
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="legacy" label="backend.run">
|
|
``` python
|
|
provider = ibm.get_provider(hub="h1", group="g1", project="p1")
|
|
backend = provider.get_backend("ibm_backend")
|
|
```
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
|
|
## Example: Basic execution
|
|
|
|
Change the import and run statements, instantiate the primitive, and make sure the input is modified to adhere to the backend's instruction set architecture (ISA).
|
|
|
|
<Tabs>
|
|
<TabItem value="updated" label="Qiskit Runtime">
|
|
```python
|
|
from qiskit import QuantumCircuit
|
|
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
|
|
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
|
|
|
|
service = QiskitRuntimeService()
|
|
|
|
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
|
|
|
|
circuit = QuantumCircuit(2, 2)
|
|
circuit.h(0)
|
|
circuit.cx(0, 1)
|
|
circuit.measure_all()
|
|
|
|
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
|
|
isa_circuit = pm.run(circuit)
|
|
|
|
sampler = Sampler(backend)
|
|
job = sampler.run([isa_circuit])
|
|
result = job.result()
|
|
print(f" > Counts: {result[0].data.meas.get_counts()}")
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="legacy" label="backend.run">
|
|
```python
|
|
from qiskit_ibm_provider import IBMProvider
|
|
from qiskit import QuantumCircuit
|
|
from qiskit.compiler import transpile
|
|
|
|
circuit = QuantumCircuit(2, 2)
|
|
circuit.h(0)
|
|
circuit.cx(0, 1)
|
|
circuit.measure_all()
|
|
|
|
provider = IBMProvider()
|
|
backend = provider.get_backend("ibm_qasm_simulator")
|
|
transpiled_circuit = transpile(circuit, backend=backend)
|
|
job = backend.run(transpiled_circuit)
|
|
print(f" > Counts: {job.result().get_counts()}")
|
|
```
|
|
</TabItem>
|
|
|
|
</Tabs>
|
|
|
|
|
|
## Example: Use sessions
|
|
|
|
<Tabs>
|
|
<TabItem value="Updated" label="Runtime primitives">
|
|
```python
|
|
from qiskit_ibm_runtime import Session
|
|
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
|
|
|
|
with Session(backend=backend) as session:
|
|
sampler = Sampler()
|
|
job = sampler.run([isa_circuit])
|
|
another_job = sampler.run([another_isa_circuit])
|
|
result = job.result()
|
|
another_result = another_job.result()
|
|
```
|
|
</TabItem>
|
|
|
|
<TabItem value="Legacy" label="backend.run">
|
|
```python
|
|
with backend.open_session() as session:
|
|
job = backend.run(transpiled_circuit)
|
|
another_job = backend.run(transpiled_circuit)
|
|
result = job.result()
|
|
another_result = another_job.result()
|
|
```
|
|
</TabItem>
|
|
</Tabs> |