def update_qobj_config(qobj: Union[QasmQobj, PulseQobj], backend_options: Optional[Dict] = None, noise_model: Any = None) -> Union[QasmQobj, PulseQobj]: """Update a ``Qobj`` configuration from backend options and a noise model. Args: qobj: Description of the job. backend_options: Backend options. noise_model: Noise model. Returns: The updated ``Qobj``. """ config = qobj.config.to_dict() # Append backend options to configuration. if backend_options: for key, val in backend_options.items(): config[key] = val # Append noise model to configuration. Overwrites backend option if noise_model: 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) return qobj
def update_qobj_config( qobj: Qobj, backend_options: Optional[Dict] = None, noise_model: Any = None ) -> Qobj: """Update a Qobj configuration from options and noise model. Args: qobj (Qobj): description of job backend_options (dict): backend options noise_model (NoiseModel): noise model Returns: Qobj: qobj. """ config = qobj.config.to_dict() # Append backend options to configuration. if backend_options: for key, val in backend_options.items(): config[key] = val # Append noise model to configuration. Overwrites backend option if noise_model: 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) return qobj
def update_qobj_config(qobj, backend_options=None, noise_model=None): """Update a Qobj configuration from options and noise model. Args: qobj (Qobj): description of job backend_options (dict): backend options noise_model (NoiseModel): noise model Returns: Qobj: qobj. """ config = qobj.config.as_dict() # Append backend options to configuration. if backend_options: for key, val in backend_options.items(): config[key] = val # Append noise model to configuration. if noise_model: config['noise_model'] = noise_model.as_dict(serializable=True) # Update the Qobj configuration. qobj.config = QobjHeader.from_dict(config) return qobj
def setUp(self): experiment_result_data = ExperimentResultData.from_dict( {'counts': { '0x0': 42 }}) experiment_result_data_2 = ExperimentResultData.from_dict( {'counts': { '0x1': 42 }}) experiment_result_data_3 = ExperimentResultData.from_dict({}) header_1 = QobjHeader.from_dict({'name': 'Test1'}) header_2 = QobjHeader.from_dict({'name': 'Test2'}) header_3 = QobjHeader.from_dict({'name': 'Test3'}) self.experiment_result_dictionary_1 = { 'name': 'Test1', 'shots': 42, 'data': experiment_result_data, 'status': 'DONE', 'success': True, 'time_taken': 0.42, 'header': header_1 } self.experiment_result_dictionary_2 = { 'name': 'Test2', 'shots': 23, 'data': experiment_result_data_2, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_2 } self.experiment_result_dictionary_3 = { 'name': 'Test3', 'shots': 23, 'data': experiment_result_data_3, 'status': 'CANCELLED', 'success': False, 'time_taken': 0.12, 'header': header_3 } self.experiment_result_1 = ExperimentResult( **self.experiment_result_dictionary_1) self.experiment_result_2 = ExperimentResult( **self.experiment_result_dictionary_2) self.experiment_result_3 = ExperimentResult( **self.experiment_result_dictionary_3)
def test_qobj_headers_in_result(self): """Test that the qobj headers are passed onto the results.""" custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend in qiskit.providers.aer.Aer.backends(): with self.subTest(backend=backend): qobj = compile(self.qc1, backend) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' result = backend.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def test_qobj_headers_in_result(self): """Test that the qobj headers are passed onto the results.""" custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend in BasicAer.backends(): with self.subTest(backend=backend): new_circ = transpile(self.qc1, backend=backend) qobj = assemble(new_circ, shots=1024) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' result = backend.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def test_qobj_headers_in_result_devices(self, backend): """Test that the qobj headers are passed onto the results for devices.""" # pylint: disable=unused-argument custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} qobj = assemble(transpile(self.qc1, backend=backend), backend=backend) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' job = backend.run(qobj, validate_qobj=True) job.wait_for_final_state(wait=300, callback=self.simple_job_callback) result = job.result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def from_dict(cls, data): """Create a new ExperimentResultData object from a dictionary. Args: data (dict): A dictionary representing the Result to create. It will be in the same format as output by :meth:`to_dict`. Returns: Result: The ``Result`` object from the input dictionary. """ in_data = copy.copy(data) in_data["results"] = [ExperimentResult.from_dict(x) for x in in_data.pop("results")] if "header" in in_data: in_data["header"] = QobjHeader.from_dict(in_data.pop("header")) return cls(**in_data)
def test_qobj_headers_in_result_sims(self): """Test that the qobj headers are passed onto the results for sims.""" backends = self.provider.backends(simulator=True) custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend in backends: with self.subTest(backend=backend): circuits = transpile(self.qc1, backend=backend) qobj = assemble(circuits, backend=backend) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) qobj.experiments[0].header.some_field = 'extra info' result = backend.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def test_qobj_headers_in_result_devices(self, qe_token, qe_url): """Test that the qobj headers are passed onto the results for devices.""" IBMQ.enable_account(qe_token, qe_url) backends = IBMQ.backends(simulator=False) custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend in backends: with self.subTest(backend=backend): qobj = compile(self.qc1, backend) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' result = backend.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def test_qobj_headers_in_result_devices(self, provider, backend): """Test that the qobj headers are passed onto the results for devices.""" # pylint: disable=unused-argument backends = provider.backends(simulator=False, filters=lambda b: b.status().operational) custom_qobj_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}} for backend_ in backends: with self.subTest(backend=backend_): circuits = transpile(self.qc1, backend=backend_) qobj = assemble(circuits, backend=backend_) # Update the Qobj header. qobj.header = QobjHeader.from_dict(custom_qobj_header) # Update the Qobj.experiment header. qobj.experiments[0].header.some_field = 'extra info' result = backend_.run(qobj).result() self.assertEqual(result.header.to_dict(), custom_qobj_header) self.assertEqual(result.results[0].header.some_field, 'extra info')
def setUp(self): experiment_result_data_1 = ExperimentResultData.from_dict( {'counts': { '0x0': 42, '0x3': 58 }}) experiment_result_data_1.probabilities = {'0x0': 0.42, '0x3': 0.58} experiment_result_data_2 = ExperimentResultData.from_dict( {'counts': { '0x0': 24, '0x1': 25, '0x2': 23, '0x3': 28 }}) experiment_result_data_2.probabilities = { '0x0': 0.24, '0x1': 0.25, '0x2': 0.23, '0x3': 0.28 } experiment_result_data_3 = ExperimentResultData.from_dict( {'counts': { '0x0': 24, '0x1': 25, '0x2': 23, '0x3': 28 }}) header_1 = QobjHeader.from_dict({ 'name': 'Test1', 'memory_slots': 2, 'creg_sizes': [['c0', 2]] }) header_2 = QobjHeader.from_dict({ 'name': 'Test2', 'memory_slots': 3, 'creg_sizes': [['c0', 3]] }) header_3 = None self.experiment_result_dictionary_1 = { 'name': 'Test1', 'shots': 42, 'data': experiment_result_data_1, 'status': 'DONE', 'success': True, 'time_taken': 0.42, 'header': header_1 } self.experiment_result_dictionary_2 = { 'name': 'Test2', 'shots': 23, 'data': experiment_result_data_2, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_2 } self.experiment_result_dictionary_3 = { 'name': 'Test3', 'shots': 23, 'data': experiment_result_data_3, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_3 } self.experiment_result_1 = ExperimentResult( **self.experiment_result_dictionary_1) self.experiment_result_2 = ExperimentResult( **self.experiment_result_dictionary_2) self.experiment_result_3 = ExperimentResult( **self.experiment_result_dictionary_3)
def setUp(self): experiment_result_data_1 = ExperimentResultData.from_dict({ 'counts': { '0x0': 42, '0x3': 58 }, 'probabilities': { '0x0': 0.42, '0x3': 0.58 }, 'calibration': { 'fridge_temperature': 26.9, 'unit': 'mK' } }) experiment_result_data_2 = ExperimentResultData.from_dict({ 'counts': { '0x0': 24, '0x1': 25, '0x2': 23, '0x3': 28 }, 'probabilities': { '0x0': 0.24, '0x1': 0.25, '0x2': 0.23, '0x3': 0.28 }, 'calibration': { 'fridge_temperature': 25.0, 'unit': 'mK' } }) experiment_result_data_3 = ExperimentResultData.from_dict({ 'counts': { '0x0': 24, '0x1': 25, '0x2': 23, '0x3': 28 }, 'calibration': None }) experiment_result_data_4 = ExperimentResultData.from_dict( {'counts': { '0x0': 24, '0x1': 25, '0x2': 23, '0x3': 28 }}) header_1 = QobjHeader.from_dict({ 'name': 'Test1', 'memory_slots': 2, 'creg_sizes': [['c0', 2]] }) header_2 = QobjHeader.from_dict({ 'name': 'Test2', 'memory_slots': 3, 'creg_sizes': [['c0', 3]] }) header_3 = None header_4 = None self.experiment_result_dictionary_1 = { 'name': 'Test1', 'shots': 42, 'data': experiment_result_data_1, 'status': 'DONE', 'success': True, 'time_taken': 0.42, 'header': header_1 } self.experiment_result_dictionary_2 = { 'name': 'Test2', 'shots': 23, 'data': experiment_result_data_2, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_2 } self.experiment_result_dictionary_3 = { 'name': 'Test3', 'shots': 23, 'data': experiment_result_data_3, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_3 } self.experiment_result_dictionary_4 = { 'name': 'Test4', 'shots': 21, 'data': experiment_result_data_4, 'status': 'DONE', 'success': True, 'time_taken': 0.12, 'header': header_4 } self.experiment_result_1 = ExperimentResult( **self.experiment_result_dictionary_1) self.experiment_result_2 = ExperimentResult( **self.experiment_result_dictionary_2) self.experiment_result_3 = ExperimentResult( **self.experiment_result_dictionary_3) self.experiment_result_4 = ExperimentResult( **self.experiment_result_dictionary_4)