def circuit_status(self, handle: ResultHandle) -> CircuitStatus: if handle in self._cache and "result" in self._cache[handle]: return CircuitStatus(StatusEnum.COMPLETED) if handle in self._cache: qamstatus = self._cache[handle]["qam"].status tkstat = _STATUS_MAP.get(qamstatus, StatusEnum.ERROR) return CircuitStatus(tkstat, qamstatus) raise CircuitNotRunError(handle)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) jobid = handle[0] message = "" measure_permutations = json.loads(handle[1]) # type: ignore if self._MACHINE_DEBUG: n_qubits, n_shots = literal_eval( jobid[len(_DEBUG_HANDLE_PREFIX):]) # type: ignore empty_ar = OutcomeArray.from_ints([0] * n_shots, n_qubits, big_endian=True) self._cache[handle].update( {"result": BackendResult(shots=empty_ar)}) statenum = StatusEnum.COMPLETED else: data = put(self._url, data={ "id": jobid }, headers=self._header).json() status = data["status"] if "ERROR" in data: message = data["ERROR"] statenum = _STATUS_MAP.get(status, StatusEnum.ERROR) if statenum is StatusEnum.COMPLETED: shots = OutcomeArray.from_ints(data["samples"], data["no_qubits"], big_endian=True) shots = shots.choose_indices(measure_permutations) self._cache[handle].update( {"result": BackendResult(shots=shots)}) return CircuitStatus(statenum, message)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: """ Return a CircuitStatus reporting the status of the circuit execution corresponding to the ResultHandle """ if handle in self._cache: return CircuitStatus(StatusEnum.COMPLETED) raise CircuitNotRunError(handle)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) jobid = str(handle[0]) n_shots = cast(int, handle[1]) if self._MACHINE_DEBUG: n_qubits: int = literal_eval(jobid[len(_DEBUG_HANDLE_PREFIX):]) zero_counts: Counter = Counter() zero_array = OutcomeArray.from_ints( ints=[0], width=n_qubits, big_endian=False, ) zero_counts[zero_array] = n_shots if handle in self._cache: self._cache[handle].update( {"result": BackendResult(counts=zero_counts)}) else: self._cache[handle] = { "result": BackendResult(counts=zero_counts) } statenum = StatusEnum.COMPLETED else: measure_permutations = json.loads(str(handle[2])) url = self._url + str(jobid) resp = get(url, headers=self._header).json() status = resp["status"] statenum = _STATUS_MAP.get(status) # type: ignore if statenum is StatusEnum.COMPLETED: ionq_counts = resp["data"]["histogram"] tket_counts: Counter = Counter() # reverse engineer counts. Imprecise, due to rounding. max_counts = 0 max_array = None for outcome_key, prob in ionq_counts.items(): array = OutcomeArray.from_ints( ints=[int(outcome_key)], width=int(resp["qubits"]), big_endian=False, ) array = array.choose_indices(measure_permutations) array_counts = round(n_shots * float(prob)) tket_counts[array] = array_counts if array_counts > max_counts: max_counts = array_counts max_array = array # account for rounding error sum_counts = sum(tket_counts.values()) diff = n_shots - sum_counts tket_counts[max_array] += diff if handle in self._cache: self._cache[handle].update( {"result": BackendResult(counts=tket_counts)}) else: self._cache[handle] = { "result": BackendResult(counts=tket_counts) } return CircuitStatus(statenum)
def _parse_status(response: Dict) -> CircuitStatus: h_status = response["status"] msgdict = { k: response.get(k, None) for k in ( "name", "submit-date", "result-date", "queue-position", "cost", "error", ) } message = str(msgdict) return CircuitStatus(_STATUS_MAP[h_status], message)
def test_aerstate_result_handle() -> None: c = circuit_gen() b1 = AerStateBackend() h1 = b1.process_circuits([c])[0] state = b1.get_result(h1).get_state() status = b1.circuit_status(h1) assert status == CircuitStatus(StatusEnum.COMPLETED, "job has successfully run") assert np.allclose(state, [np.sqrt(0.5), 0, 0, math.sqrt(0.5)], atol=1e-10) b2 = AerUnitaryBackend() unitary = b2.get_unitary(c) assert np.allclose( unitary, np.sqrt(0.5) * np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 1, 0, -1], [1, 0, -1, 0]]), )
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) jobid = str(handle[0]) if self._api_handler is None or jobid.startswith(_DEBUG_HANDLE_PREFIX): return CircuitStatus(StatusEnum.COMPLETED) # TODO check queue position and add to message try: response = self._api_handler.retrieve_job_status( jobid, use_websocket=True) except HQSAPIError: self.relogin() response = self._api_handler.retrieve_job_status( jobid, use_websocket=True) if response is None: raise RuntimeError( f"Unable to retrieve circuit status for handle {handle}") circ_status = _parse_status(response) if circ_status.status is StatusEnum.COMPLETED: if "results" in response: self._update_cache_result(handle, _convert_result(response["results"])) return circ_status
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: if self._device_type == _DeviceType.LOCAL: return CircuitStatus(StatusEnum.COMPLETED) task_id, want_state = handle task = AwsQuantumTask(task_id) state = task.state() if state == "FAILED": result = task.result() return CircuitStatus(StatusEnum.ERROR, result.task_metadata.failureReason) elif state == "CANCELLED": return CircuitStatus(StatusEnum.CANCELLED) elif state == "COMPLETED": self._cache[handle].update(_get_result(task, want_state)) return CircuitStatus(StatusEnum.COMPLETED) elif state == "QUEUED" or state == "CREATED": return CircuitStatus(StatusEnum.QUEUED) elif state == "RUNNING": return CircuitStatus(StatusEnum.RUNNING) else: return CircuitStatus(StatusEnum.ERROR, f"Unrecognized state '{state}'")
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) ibmstatus = self._retrieve_job(cast(str, handle[0])).status() return CircuitStatus(_STATUS_MAP[ibmstatus], ibmstatus.value)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) return CircuitStatus(StatusEnum.COMPLETED)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: if handle in self._cache: return CircuitStatus(StatusEnum.COMPLETED) raise CircuitNotRunError(handle)
def circuit_status(self, handle: ResultHandle) -> CircuitStatus: self._check_handle_type(handle) job: "AerJob" = self._cache[handle]["job"] ibmstatus = job.status() return CircuitStatus(_STATUS_MAP[ibmstatus], ibmstatus.value)