def _result_from_job_response(self, job_response): if self._is_device: _reorder_bits(job_response) experiment_results = [] for circuit_result in job_response['qasms']: this_result = { 'data': circuit_result['data'], 'name': circuit_result.get('name'), 'compiled_circuit_qasm': circuit_result.get('qasm'), 'status': circuit_result['status'], 'success': circuit_result['status'] == 'DONE', 'shots': job_response['shots'] } if 'metadata' in circuit_result: this_result['metadata'] = circuit_result['metadata'] experiment_results.append(this_result) return result_from_old_style_dict( { 'id': self._job_id, 'status': job_response['status'], 'used_credits': job_response.get('usedCredits'), 'result': experiment_results, 'backend_name': self.backend().name(), 'success': job_response['status'] == 'DONE' }, [ circuit_result['name'] for circuit_result in job_response['qasms'] ])
def _run_job(self, job_id, qobj): """Run circuits in qobj""" self._validate(qobj) result_list = [] self._shots = qobj.config.shots self._qobj_config = qobj.config start = time.time() for circuit in qobj.experiments: result_list.append(self.run_circuit(circuit)) end = time.time() result = { 'backend': self._configuration['name'], 'id': qobj.qobj_id, 'job_id': job_id, 'result': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start) } copy_qasm_from_qobj_into_result(qobj, result) return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def _run_job(self, job_id, qobj): """Run qobj Args: qobj (Qobj): Qobj structure job_id (str): a id for the job Returns: Result: Result is a class including the information to be returned to users. Specifically, result_list in the return looks is important and it like this: [ {'data': {'unitary': array([[sqrt(2)/2, sqrt(2)/2, 0, 0], [0, 0, sqrt(2)/2, -sqrt(2)/2], [0, 0, sqrt(2)/2, sqrt(2)/2], [sqrt(2)/2, -sqrt(2)/2, 0, 0]], dtype=object)}, 'status': 'DONE'} ] """ result_list = [] start = time.time() for circuit in qobj.experiments: result_list.append(self.run_circuit(circuit)) end = time.time() result = {'backend': self.name, 'id': qobj.qobj_id, 'job_id': job_id, 'result': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start)} return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def _run_job(self, qobj): """Run circuits in q_job""" result_list = [] self._validate(qobj) qobj_old_format = qobj_to_dict(qobj, version='0.0.1') s = JKUSimulatorWrapper(self._configuration['exe'], silent=self.silent) #self._shots = qobj['config']['shots'] s.shots = qobj_old_format['config']['shots'] start = time.time() for circuit in qobj_old_format['circuits']: result_list.append(s.run_on_qobj_circuit(circuit)) end = time.time() job_id = str(uuid.uuid4()) result = { 'backend': self._configuration['name'], 'id': qobj_old_format['id'], 'job_id': job_id, 'result': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start) } return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def _run_job(self, job_id, qobj): """Run qobj. This is a blocking call. Args: job_id (str): unique id for the job. qobj (Qobj): job description Returns: Result: Result object """ result_list = [] start = time.time() for circuit in qobj.experiments: result_list.append(self.run_circuit(circuit)) end = time.time() result = { 'backend': self._configuration['name'], 'id': qobj.qobj_id, 'job_id': job_id, 'result': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start) } copy_qasm_from_qobj_into_result(qobj, result) return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def _run_job(self, qobj): self._validate(qobj) result = run(qobj, self._configuration['exe']) copy_qasm_from_qobj_into_result(qobj, result) return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def _run_job(self, job_id, qobj): self._validate(qobj) result = run(qobj, self._configuration.exe) result['job_id'] = job_id copy_qasm_from_qobj_into_result(qobj, result) return result_from_old_style_dict(result)
def _run_job(self, qobj): self._validate() # set backend to Clifford simulator if 'config' in qobj: qobj['config']['simulator'] = 'clifford' else: qobj['config'] = {'simulator': 'clifford'} result = run(qobj, self._configuration['exe']) return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])
def result(self, timeout=None, wait=5): """Return the result from the job. Args: timeout (int): number of seconds to wait for job wait (int): time between queries to IBM Q server Returns: Result: Result object Raises: JobError: exception raised during job initialization """ self._wait_for_submission() try: job_data = self._wait_for_job(timeout=timeout, wait=wait) except ApiError as api_err: raise JobError(str(api_err)) if self._is_device and self.status() == JobStatus.DONE: _reorder_bits(job_data) # Build the Result. job_result_list = [] for circuit_result in job_data['qasms']: this_result = { 'data': circuit_result['data'], 'name': circuit_result.get('name'), 'compiled_circuit_qasm': circuit_result.get('qasm'), 'status': circuit_result['status'], 'success': circuit_result['status'] == 'DONE', 'shots': job_data['shots'] } if 'metadata' in circuit_result: this_result['metadata'] = circuit_result['metadata'] job_result_list.append(this_result) return result_from_old_style_dict( { 'id': self._id, 'status': job_data['status'], 'used_credits': job_data.get('usedCredits'), 'result': job_result_list, 'backend_name': self.backend_name(), 'success': job_data['status'] == 'DONE' }, [circuit_result['name'] for circuit_result in job_data['qasms']])
def load_result_from_file(filename): """Load a results dictionary file (.json) to a Result object. Note: The json file may not load properly if it was saved with a previous version of the SDK. Args: filename (str): filename of the dictionary Returns: tuple(Result, dict): The new Results object if the metadata exists it will get returned Raises: QISKitError: if the file does not exist or does not have the proper dictionary structure. """ if not os.path.exists(filename): raise QISKitError('File %s does not exist' % filename) with open(filename, 'r') as load_file: master_dict = json.load(load_file) try: qresult_dict = master_dict['result'] convert_json_to_qobj(qresult_dict) metadata = master_dict['metadata'] except KeyError: raise QISKitError('File %s does not have the proper dictionary ' 'structure') # TODO: To keep backwards compatibility with previous saved versions, # the method adapts the recovered JSON to match the new format. Since # the save function takes a Result, not all the fields required by # the new Qobj are saved so they are marked with 'TODO'. qresult_dict['id'] = qresult_dict.get('id', 'TODO') for experiment in qresult_dict['result']: is_done = experiment['status'] == 'DONE' experiment['success'] = experiment.get('success', is_done) experiment['shots'] = experiment.get('shots', 'TODO') qresult = result_from_old_style_dict( qresult_dict, [circuit_data['name'] for circuit_data in qresult_dict['result']]) return qresult, metadata
def _run_job(self, job_id, qobj): if isinstance(qobj, Qobj): qobj_dict = qobj.as_dict() else: qobj_dict = qobj self._validate() # set backend to Clifford simulator if 'config' in qobj_dict: qobj_dict['config']['simulator'] = 'clifford' else: qobj_dict['config'] = {'simulator': 'clifford'} qobj = Qobj.from_dict(qobj_dict) result = run(qobj, self._configuration.exe) result['job_id'] = job_id return result_from_old_style_dict(result)
def _result_from_job_response(self, job_response): if self._is_device: _reorder_bits(job_response) experiment_results = [] for circuit_result in job_response['qasms']: this_result = { 'data': circuit_result['data'], 'compiled_circuit_qasm': circuit_result.get('qasm'), 'status': circuit_result['status'], 'success': circuit_result['status'] == 'DONE', 'shots': job_response['shots'] } if 'metadata' in circuit_result: this_result['metadata'] = circuit_result['metadata'] if 'header' in circuit_result['metadata'].get( 'compiled_circuit', {}): this_result['header'] = \ circuit_result['metadata']['compiled_circuit']['header'] else: this_result['header'] = {} experiment_results.append(this_result) ret = { 'id': self._job_id, 'status': job_response['status'], 'used_credits': job_response.get('usedCredits'), 'result': experiment_results, 'backend_name': self.backend().name(), 'success': job_response['status'] == 'COMPLETED', } # Append header: from the response; from the payload; or none. header = job_response.get('header', self._qobj_payload.get('header', {})) if header: ret['header'] = header return result_from_old_style_dict(ret)
def _run_job(self, job_id, qobj): """Run circuits in qobj and return the result Args: qobj (Qobj): Qobj structure job_id (str): A job id Returns: Result: Result is a class including the information to be returned to users. Specifically, result_list in the return contains the essential information, which looks like this:: [{'data': { 'statevector': array([sqrt(2)/2, 0, 0, sqrt(2)/2], dtype=object), }, 'status': 'DONE' }] """ self._validate(qobj) result_list = [] start = time.time() for circuit in qobj.experiments: result_list.append(self.run_circuit(circuit)) end = time.time() result = { 'backend': self.name, 'id': qobj.qobj_id, 'job_id': job_id, 'result': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start) } return result_from_old_style_dict( result, [circuit.header.name for circuit in qobj.experiments])