diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8cb7c4f37..603fb227f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -33,6 +33,7 @@ Fixed """"" - Fixed incorrect parsing of some API hub URLs (#77). +- Fixed noise model handling for remote simulators (#84). `0.1.1`_ - 2019-05-01 diff --git a/qiskit/providers/ibmq/utils.py b/qiskit/providers/ibmq/utils.py index 7381b8205..5f5ee4be6 100644 --- a/qiskit/providers/ibmq/utils.py +++ b/qiskit/providers/ibmq/utils.py @@ -17,6 +17,31 @@ from qiskit.qobj import QobjHeader +def _serialize_noise_model(config): + """Traverse the dictionary looking for noise_model keys and apply + a transformation so it can be serialized. + + Args: + config (dict): The dictionary to traverse + + Returns: + dict: The transformed dictionary + """ + for k, v in config.items(): + if isinstance(config[k], dict): + _serialize_noise_model(config[k]) + else: + if k == 'noise_model': + try: + config[k] = v.as_dict(serializable=True) + except AttributeError: + # if .as_dict() fails is probably because the noise_model + # has been already transformed elsewhere + pass + + return config + + def update_qobj_config(qobj, backend_options=None, noise_model=None): """Update a Qobj configuration from options and noise model. @@ -35,9 +60,12 @@ def update_qobj_config(qobj, backend_options=None, noise_model=None): for key, val in backend_options.items(): config[key] = val - # Append noise model to configuration. + # Append noise model to configuration. Overwrites backend option if noise_model: - config['noise_model'] = noise_model.as_dict(serializable=True) + config['noise_model'] = noise_model + + # Look for noise_models in the config, and try to transform them + config = _serialize_noise_model(config) # Update the Qobj configuration. qobj.config = QobjHeader.from_dict(config)