Migrate from qiskit_ibm_provider
This documentation is relevant to the new IBM Quantum® Platform. If you need the previous version, return to the IBM Quantum Platform Classic documentation.
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 on retrieving account credentials, see Install and set up.
from qiskit_ibm_runtime import QiskitRuntimeService
QiskitRuntimeService.save_account(channel="ibm_cloud", 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_cloud", token="<API_TOKEN>", instance="<CRN for premium instance>", name="premium")
QiskitRuntimeService.save_account(channel="ibm_cloud", token="<API_TOKEN>", instance="<CRN for open instance>", name="open")
# Load the "open" credentials
service = QiskitRuntimeService(name="open")
IBMProvider.save_account(token='MY_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_cloud", 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()
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).
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()}")
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 or batch mode.
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()
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()