Exemple #1
0
def parse_error(res):
    """
    Every server error should contain a "status" field with a human readable explanation of what went wrong as well as
    a "error_type" field indicating the kind of error that can be mapped to a Python type.

    There's a fallback error UnknownError for other types of exceptions (network issues, api gateway problems, etc.)
    """
    try:
        body = res.json()
    except JSONDecodeError:
        raise UnknownApiError(res.text)

    if 'error_type' not in body:
        raise UnknownApiError(str(body))

    error_type = body['error_type']
    status = body['status']

    if re.search(
            r"[0-9]+ qubits were requested, but the QVM is limited to [0-9]+ qubits.",
            status):
        return TooManyQubitsError(status)

    error_cls = error_mapping.get(error_type, UnknownApiError)
    return error_cls(status)
Exemple #2
0
    def result(self):
        """
        The result of the job if available
        throws ValueError is result is not available yet
        throws ApiError if server returned an error indicating program execution was not successful
        or if the job was cancelled
        """
        if not self.is_done():
            raise ValueError(
                "Cannot get a result for a program that isn't completed.")

        if self._raw['status'] == 'CANCELLED':
            raise CancellationError(self._raw['result'])
        elif self._raw['status'] == 'ERROR':
            if self._machine == 'QVM':
                raise QVMError(self._raw['result'])
            elif self._machine == 'QPU':
                raise QPUError(self._raw['result'])
            elif self._machine == 'QUILC':
                raise QUILCError(self._raw['result'])
            else:
                raise UnknownApiError(self._raw['result'])

        if self._raw['program']['type'] == 'wavefunction':
            return Wavefunction.from_bit_packed_string(
                base64.b64decode(self._raw['result']),
                self._raw['program']['addresses'])
        elif self._raw['program']['type'] in [
                'multishot', 'multishot-measure', 'expectation'
        ]:
            return np.asarray(self._raw['result'])
        else:
            return self._raw['result']
Exemple #3
0
 def time_in_queue(self):
     """
     For how long was the job in the Forest queue?
     :return: Time in queue, seconds
     :rtype: Optional[float]
     """
     if not self.is_done():
         raise ValueError(
             "Cannot get time in queue for a program that isn't completed.")
     try:
         time_in_queue = float(self._raw['time_in_queue'].split()[0])
     except (ValueError, KeyError, IndexError):
         raise UnknownApiError(str(self._raw))
     return time_in_queue
Exemple #4
0
 def running_time(self):
     """
     For how long was the job running?
     :return: Running time, seconds
     :rtype: Optional[float]
     """
     if not self.is_done():
         raise ValueError(
             "Cannot get running time for a program that isn't completed.")
     try:
         running_time = float(self._raw['running_time'].split()[0])
     except (ValueError, KeyError, IndexError):
         raise UnknownApiError(str(self._raw))
     return running_time