def result(self, timeout: Optional[float] = None, wait: float = 0.5) -> Result: """ Args: timeout: Timeout in seconds. wait: Wait time between queries to the quantum-inspire platform. Returns: Result object containing results from the experiments. Raises: JobTimeoutError: If timeout is reached. QisKitBackendError: If an error occurs during simulation. """ start_time = time.time() while self.status() != JobStatus.DONE: elapsed_time = time.time() - start_time if timeout is not None and elapsed_time > timeout: raise JobTimeoutError( 'Failed getting result: timeout reached.') time.sleep(wait) experiment_results = self._backend.get_experiment_results(self) return Result(backend_name=self._backend.backend_name, backend_version=quantum_inspire_version, job_id=self.job_id(), qobj_id=self.job_id(), success=True, results=experiment_results)
def _wait_for_job(self, timeout=60, wait=5): """Wait until all online ran circuits of a qobj are 'COMPLETED'. Args: timeout (float or None): seconds to wait for job. If None, wait indefinitely. wait (float): seconds between queries Returns: dict: A dict with the contents of the API request. Raises: JobTimeoutError: if the job does not return results before a specified timeout. JobError: if something wrong happened in some of the server API calls """ start_time = time.time() while self.status() not in JOB_FINAL_STATES: elapsed_time = time.time() - start_time if timeout is not None and elapsed_time >= timeout: raise JobTimeoutError( 'Timeout while waiting for the job: {}'.format( self._job_id)) logger.info('status = %s (%d seconds)', self._status, elapsed_time) time.sleep(wait) if self._cancelled: raise JobError( 'Job result impossible to retrieve. The job was cancelled.') return self._api.get_job(self._job_id)
def _wait_for_result(self, timeout=None, wait=5): start_time = time.time() result = None header = { "Ocp-Apim-Subscription-Key": self._backend._provider.access_token, "SDK": "qiskit" } while True: elapsed = time.time() - start_time if timeout and elapsed >= timeout: raise JobTimeoutError('Timed out waiting for result') result = requests.put(self._backend.url, data={ 'id': self._job_id, 'access_token': self._backend._provider.access_token }, headers=header).json() if result['status'] == 'finished': break if result['status'] == 'error': raise JobError('API returned error:\n' + str(result)) time.sleep(wait) return result
def _result(self, result_function: Callable[[Job], List[ExperimentResult]], timeout: Optional[float] = None, wait: float = 0.5) -> QIResult: """ Return the result for the experiments. :param result_function: backend function for fetching the requested results. :param timeout: Timeout in seconds. :param wait: Wait time between queries to the Quantum Inspire platform. :return: Result object containing results from the experiments. :raises JobTimeoutError: If timeout is reached. :raises QiskitBackendError: If an error occurs during simulation. """ start_time = time.time() while self.status() not in JOB_FINAL_STATES: elapsed_time = time.time() - start_time if timeout is not None and elapsed_time > timeout: raise JobTimeoutError('Failed getting result: timeout reached.') time.sleep(wait) experiment_results = result_function(self) total_time_taken = sum(getattr(experiment_result, "time_taken", 0.0) for experiment_result in experiment_results) return QIResult(backend_name=self._backend.backend_name, backend_version=quantum_inspire_version, job_id=self.job_id(), qobj_id=self.job_id(), success=True, results=experiment_results, status=self.status(), time_taken=total_time_taken)
def _wait_for_submission(self, timeout=60): """Waits for the request to return a job ID""" if self._job_id is None: if self._future is None: raise JobError("You have to submit before asking for status or results!") try: submit_info = self._future.result(timeout=timeout) if self._future_captured_exception is not None: raise self._future_captured_exception except TimeoutError as ex: raise JobTimeoutError( "Timeout waiting for the job being submitted: {}".format(ex) ) if 'error' in submit_info: self._status = JobStatus.ERROR self._api_error_msg = str(submit_info['error']) raise JobError(str(submit_info['error']))
def _wait_for_final_status_websocket(self, timeout=None): """Wait until the job progress to a final state using websockets. Args: timeout (float or None): seconds to wait for job. If None, wait indefinitely. Raises: JobTimeoutError: if the job does not return results before a specified timeout. """ websocket_client = self._api.websocket_client() try: status_response = asyncio.get_event_loop().run_until_complete( websocket_client.get_job_status(self._job_id, timeout=timeout)) self._update_status(status_response) except WebsocketTimeoutError: raise JobTimeoutError('Timeout while waiting for job {}'.format( self._job_id))
def _wait_for_final_status(self, timeout=None, wait=5): """Wait until the job progress to a final state. Args: timeout (float or None): seconds to wait for job. If None, wait indefinitely. wait (float): seconds between queries. Raises: JobTimeoutError: if the job does not return results before a specified timeout. """ start_time = time.time() while self.status() not in JOB_FINAL_STATES: elapsed_time = time.time() - start_time if timeout is not None and elapsed_time >= timeout: raise JobTimeoutError( 'Timeout while waiting for job {}'.format(self._job_id)) logger.info('status = %s (%d seconds)', self._status, elapsed_time) time.sleep(wait)
def _wait_for_final_status_websocket(self, timeout=None): """Wait until the job progress to a final state using websockets. Args: timeout (float or None): seconds to wait for job. If None, wait indefinitely. Raises: JobTimeoutError: if the job does not return results before a specified timeout. """ # Avoid the websocket invocation if already in a final state. if self._status in JOB_FINAL_STATES: return try: status_response = self._api.job_final_status_websocket( self._job_id, timeout=timeout) self._update_status(status_response) except WebsocketTimeoutError: raise JobTimeoutError('Timeout while waiting for job {}'.format( self._job_id))
def _wait_for_completion( self, timeout: Optional[float] = None, wait: float = 5, required_status: Tuple[JobStatus] = JOB_FINAL_STATES) -> bool: """Wait until the job progress to a final state such as DONE or ERROR. Args: timeout (float or None): seconds to wait for job. If None, wait indefinitely. wait (float): seconds between queries. required_status (tuple[JobStatus]): the final job status required. Returns: bool: True if the final job status matches one of the required states. Raises: JobTimeoutError: if the job does not return results before a specified timeout. """ if self._status in JOB_FINAL_STATES: return self._status in required_status with api_to_job_error(): try: status_response = self._api.job_final_status(self.job_id(), timeout=timeout, wait=wait) except UserTimeoutExceededError: raise JobTimeoutError( 'Timeout while waiting for job {}'.format(self._job_id)) self._update_status_position(ApiJobStatus(status_response['status']), status_response.get('infoQueue', None)) # Get all job attributes if the job is done. if self._status in JOB_FINAL_STATES: self.refresh() return self._status in required_status