Skip to content

Fail to recover Experiment from the config if the experiment was initialized with an IBM runtime backend #1508

@IvanaGyro

Description

@IvanaGyro

Informations

  • Qiskit Experiments version: 0.8.0
  • Python version: Python 3.12.0b4
  • Operating system: Windows 11

What is the current behavior?

An expectation was raised:

  File "site-packages\qiskit_experiments\framework\json.py", line 338, in _deserialize_object
    return cls(**settings)
           ^^^^^^^^^^^^^^^
TypeError: IBMBackend.__init__() missing 3 required positional arguments: 'configuration', 'service', and 'api_client'

The reason is that the ABC class StoreInitArgs stores all keyword arguments to an argument __init_kwargs__, which will be accessed by BaseExperiment.config() and BaseExperiment.from_config(). However, the instance of IBMBackend cannot be encoded and decoded correctly by ExperimentEncoder and ExperimentDecoder.

Steps to reproduce the problem

Below is the minimal example code for reproducing the problem.

import json

from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_experiments.framework import ExperimentDecoder, ExperimentEncoder
from qiskit_experiments.library import T2Ramsey

service = QiskitRuntimeService(
    instance='ibm-q/open/main',
    channel='ibm_quantum',
    token='MY_TOKEN'
)

experiment = T2Ramsey(
    physical_qubits=(0,),
    delays=[0., 1.0e-6],
    backend=service.backend('ibm_sherbrooke'),
    osc_freq=0.0)

serialized_exp = json.dumps(experiment.config(), cls=ExperimentEncoder)
T2Ramsey.from_config(json.loads(serialized_exp, cls=ExperimentDecoder))

What is the expected behavior?

BaseExperiment should be able to recover the instance when the instance was initialized with an IBM runtime backend instance.

Suggested solutions

Below is not a suggested solution but a workaround: manually assigning backend instance after init/recover the experiment.

import json

from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_experiments.framework import ExperimentDecoder, ExperimentEncoder
from qiskit_experiments.library import T2Ramsey

service = QiskitRuntimeService(
    instance='ibm-q/open/main',
    channel='ibm_quantum',
    token='MY_TOKEN'
)

experiment = T2Ramsey(
    physical_qubits=(0,),
    delays=[0., 1.0e-6],
    osc_freq=0.0)

experiment.backend = service.backend('ibm_sherbrooke')

serialized_exp = json.dumps(experiment.config(), cls=ExperimentEncoder)
recovered_exp = T2Ramsey.from_config(json.loads(serialized_exp, cls=ExperimentDecoder))
recovered_exp.backend = service.backend('ibm_sherbrooke')

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions