def state_vector() -> ResultType: """Registers this function into the circuit class. Returns: ResultType: state vector as a requested result type Examples: >>> circ = Circuit().state_vector() """ return ResultType.StateVector()
def _get_result(completed_task: Union[AwsQuantumTask, LocalQuantumTask], want_state: bool) -> Dict[str, BackendResult]: result = completed_task.result() kwargs = {} if want_state: kwargs["state"] = result.get_value_by_result_type( ResultType.StateVector()) else: kwargs["shots"] = OutcomeArray.from_readouts(result.measurements) return {"result": BackendResult(**kwargs)}
def process_circuits( self, circuits: Iterable[Circuit], n_shots: Optional[int] = None, valid_check: bool = True, **kwargs: KwargTypes, ) -> List[ResultHandle]: """ Supported `kwargs`: none """ if not self.supports_shots and not self.supports_state: raise RuntimeError("Backend does not support shots or state") if n_shots is None: n_shots = 0 want_state = n_shots == 0 if (not want_state) and (n_shots < self._sample_min_shots or n_shots > self._sample_max_shots): raise ValueError( "For sampling, n_shots must be between " f"{self._sample_min_shots} and {self._sample_max_shots}. " "For statevector simulation, omit this parameter.") if valid_check: self._check_all_circuits(circuits, nomeasure_warn=False) handles = [] for circ in circuits: bkcirc = self._to_bkcirc(circ) if want_state: bkcirc.add_result_type(ResultType.StateVector()) if not bkcirc.instructions and len(circ.bits) == 0: task = None else: task = self._run(bkcirc, n_shots=n_shots) if self._device_type == _DeviceType.LOCAL: # Results are available now. Put them in the cache. if task is not None: assert task.state() == "COMPLETED" results = _get_result(task, want_state) else: results = { "result": self.empty_result(circ, n_shots=n_shots) } else: # Task is asynchronous. Must wait for results. results = {} if task is not None: handle = ResultHandle(task.id, want_state) else: handle = ResultHandle(str(uuid4()), False) self._cache[handle] = results handles.append(handle) return handles