Skip to content

Latest commit

 

History

History
194 lines (145 loc) · 5.79 KB

File metadata and controls

194 lines (145 loc) · 5.79 KB
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 guide is no longer maintained. It is archived in GitHub for historical reference only.

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.

For information about retrieving account credentials, see [Install and set up](/docs/guides/cloud-setup).
from qiskit_ibm_runtime import QiskitRuntimeService

QiskitRuntimeService.save_account(channel="ibm_quantum_platform", token="<API_TOKEN>", overwrite=True, set_as_default=True)

Additionally, you can now name your saved credentials and load the credentials by name.

# Save different accounts for open and premium access

QiskitRuntimeService.save_account(channel="ibm_quantum_platform",
                                  token="<API_TOKEN>", # Use the 44-character API_KEY you created and saved from the IBM Quantum Platform Home dashboard
                                  instance="<CRN for premium instance>",
                                  name="premium")
QiskitRuntimeService.save_account(channel="ibm_quantum_platform",
                                  token="<API_TOKEN>", # Use the 44-character API_KEY you created and saved from the IBM Quantum Platform Home dashboard
                                  instance="<CRN for open instance>",
                                  name="open")

# Load the "open" credentials

service = QiskitRuntimeService(name="open")
```python IBMProvider.save_account(token='your_API_TOKEN') ```

Instance selection (get a provider)

Use the updated code to select an instance.

The new syntax combines the functionality from load_account() and get_provider() in one statement. Use the instance keyword to specify your Cloud Resource Name (CRN).

# To access saved credentials for the IBM quantum channel and select an instance
service = QiskitRuntimeService(channel="ibm_quantum_platform", instance="<CRN>")

Get a backend

Use the updated code to specify a backend.

# 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:

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.

# 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()
```python provider = ibm.get_provider(hub="h1", group="g1", project="p1") backend = provider.get_backend("ibm_backend") ```

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).

```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()}")

Example: Use sessions

The following code block will return an error for users on the Open Plan, because it uses sessions. Workloads on the Open Plan can run only in [job mode](/docs/guides/execution-modes#job-mode) or [batch mode](/docs/guides/execution-modes#batch-mode). ```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()