def get_result(self, handle: ResultHandle, **kwargs: KwargTypes) -> BackendResult:
     """
     See :py:meth:`pytket.backends.Backend.get_result`.
     Supported kwargs: `timeout`, `wait`.
     """
     try:
         return Backend.get_result(self, handle)
     except CircuitNotRunError:
         timeout = kwargs.get("timeout")
         wait = kwargs.get("wait", 1.0)
         # Wait for job to finish; result will then be in the cache.
         end_time = (time.time() + timeout) if (timeout is not None) else None
         while (end_time is None) or (time.time() < end_time):
             circuit_status = self.circuit_status(handle)
             if circuit_status.status is StatusEnum.COMPLETED:
                 return cast(BackendResult, self._cache[handle]["result"])
             if circuit_status.status is StatusEnum.ERROR:
                 raise RuntimeError(circuit_status.message)
             time.sleep(cast(float, wait))
         raise RuntimeError(f"Timed out: no results after {timeout} seconds.")
Esempio n. 2
0
    def get_result(self, handle: ResultHandle,
                   **kwargs: KwargTypes) -> BackendResult:
        """
        See :py:meth:`pytket.backends.Backend.get_result`.
        Supported kwargs: `timeout`, `wait`.
        """
        try:
            return super().get_result(handle)
        except CircuitNotRunError:
            jobid = str(handle[0])

            if self._MACHINE_DEBUG or jobid.startswith(_DEBUG_HANDLE_PREFIX):
                debug_handle_info = jobid[len(_DEBUG_HANDLE_PREFIX):]
                n_qubits, shots = literal_eval(debug_handle_info)
                return _convert_result({"c": (["0" * n_qubits] * shots)})
            # TODO exception handling when jobid not found on backend
            timeout = kwargs.get("timeout")
            if timeout is not None:
                timeout = int(timeout)
            wait = kwargs.get("wait")
            if wait is not None:
                wait = int(wait)

            job_retrieve = self._retrieve_job(jobid, timeout, wait)
            circ_status = _parse_status(job_retrieve)
            if circ_status.status not in (StatusEnum.COMPLETED,
                                          StatusEnum.CANCELLED):
                raise RuntimeError(
                    f"Cannot retrieve results, job status is {circ_status.message}"
                )
            try:
                res = job_retrieve["results"]
            except KeyError:
                raise RuntimeError("Results missing.")

            backres = _convert_result(res)
            self._update_cache_result(handle, backres)
            return backres
Esempio n. 3
0
 def process_circuits(
     self,
     circuits: Iterable[Circuit],
     n_shots: Optional[int] = None,
     valid_check: bool = True,
     **kwargs: KwargTypes,
 ) -> List[ResultHandle]:
     """
     See :py:meth:`pytket.backends.Backend.process_circuits`.
     Supported kwargs: `seed`.
     """
     if n_shots is None or n_shots < 1:
         raise ValueError(
             "Parameter n_shots is required for this backend for ForestBackend"
         )
     if valid_check:
         self._check_all_circuits(circuits)
     handle_list = []
     for circuit in circuits:
         p, bits = tk_to_pyquil(circuit, return_used_bits=True)
         p.wrap_in_numshots_loop(n_shots)
         ex = self._qc.compiler.native_quil_to_executable(p)
         qam = copy(self._qc.qam)
         qam.load(ex)
         qam.random_seed = kwargs.get("seed")  # type: ignore
         qam.run()
         handle = ResultHandle(uuid4().int)
         measures = circuit.n_gates_of_type(OpType.Measure)
         if measures == 0:
             self._cache[handle] = {
                 "qam": qam,
                 "c_bits": sorted(bits),
                 "result": self.empty_result(circuit, n_shots=n_shots),
             }
         else:
             self._cache[handle] = {"qam": qam, "c_bits": sorted(bits)}
         handle_list.append(handle)
     return handle_list