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.
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.
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.
# 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")
IBMProvider.save_account(token='MY_API_TOKEN')
Load accounts
Use the updated code to load accounts.
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"
.
# 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")
provider = IBMProvider()
Instance selection (get a provider)
Use the updated code to select a hub, group, and project.
When using the ibm_quantum
channel,
the hub
, group
, and project
are specified through the new
instance
keyword.
# 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")
provider = IBMProvider(instance="my_hub/my_group/my_project")
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
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()