コード例 #1
0
def _convert_result(output: Dict[str, float], n_shots: int) -> BackendResult:
    counts = Counter({
        OutcomeArray.from_readouts([json.loads(state)]): int(prob * n_shots)
        for state, prob in output.items()
    })

    return BackendResult(counts=counts)
コード例 #2
0
    def process_circuits(
        self,
        circuits: Iterable[Circuit],
        n_shots: Optional[int] = None,
        valid_check: bool = True,
        **kwargs: KwargTypes,
    ) -> List[ResultHandle]:
        """
        Submit circuits to the backend for running. The results will be stored
        in the backend's result cache to be retrieved by the corresponding
        get_<data> method.

        Use keyword arguments to specify parameters to be used in submitting circuits
        See specific Backend derived class for available parameters, from the following
        list:

        * `seed`: RNG seed for simulators

        :param circuits: Circuits to process on the backend.
        :type circuits: Iterable[Circuit]
        :param n_shots: Number of shots to run per circuit. None is to be used
            for state/unitary simulators. Defaults to None.
        :type n_shots: Optional[int], optional
        :param valid_check: Explicitly check that all circuits satisfy all required
            predicates to run on the backend. Defaults to True
        :type valid_check: bool, optional
        :return: Handles to results for each input circuit, as an interable in
            the same order as the circuits.
        :rtype: List[ResultHandle]
        """

        circuit_list = list(circuits)
        if valid_check:
            self._check_all_circuits(circuit_list)

        handle_list = []
        for circuit in circuit_list:
            handle = ResultHandle(str(uuid4()))
            mycirc, measure_map = tk_to_mymeasures(circuit)
            qubit_list, bit_list = zip(*measure_map.items())
            qubit_shots = sample_mycircuit(mycirc, set(qubit_list), n_shots,
                                           kwargs.get("seed"))
            # Pad shot table with 0 columns for unused bits
            all_shots = np.zeros((n_shots, len(circuit.bits)), dtype=int)
            all_shots[:, :len(qubit_list)] = qubit_shots
            res_bits = [
                measure_map[q] for q in sorted(qubit_list, reverse=True)
            ]
            for b in circuit.bits:
                if b not in bit_list:
                    res_bits.append(b)
            res = BackendResult(c_bits=res_bits,
                                shots=OutcomeArray.from_readouts(all_shots))
            self._cache[handle] = {"result": res}
            handle_list.append(handle)
        return handle_list
コード例 #3
0
ファイル: braket.py プロジェクト: ayazskhan/pytket-extensions
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)}
コード例 #4
0
 def _calculate_results(self,
                        qscall: "QSharpCallable",
                        n_shots: Optional[int] = None) -> BackendResult:
     if n_shots:
         shots_ar = np.array([qscall.simulate() for _ in range(n_shots)],
                             dtype=np.uint8)
         shots = OutcomeArray.from_readouts(shots_ar)  # type: ignore
         # ^ type ignore as array is ok for Sequence[Sequence[int]]
         # outputs should correspond to default register,
         # as mapped by FlattenRegisters()
         return BackendResult(shots=shots)
     raise ValueError("Parameter n_shots is required for this backend")
コード例 #5
0
def qiskit_result_to_backendresult(res: Result) -> Iterator[BackendResult]:
    for result in res.results:
        header = result.header
        width = header.memory_slots

        c_bits = (
            _gen_uids(header.creg_sizes, Bit) if hasattr(header, "creg_sizes") else None
        )
        q_bits = (
            _gen_uids(header.qreg_sizes, Qubit)
            if hasattr(header, "qreg_sizes")
            else None
        )
        shots, counts, state, unitary = (None,) * 4
        datadict = result.data.to_dict()
        if len(datadict) == 0 and result.shots > 0:
            n_bits = len(c_bits) if c_bits else 0
            shots = OutcomeArray.from_readouts(
                np.zeros((result.shots, n_bits), dtype=np.uint8)  #  type: ignore
            )
        else:
            if "memory" in datadict:
                memory = datadict["memory"]
                shots = _hex_to_outar(memory, width)
            elif "counts" in datadict:
                qis_counts = datadict["counts"]
                counts = Counter(
                    dict(
                        (_hex_to_outar([hexst], width), count)
                        for hexst, count in qis_counts.items()
                    )
                )

            if "statevector" in datadict:
                state = datadict["statevector"]

            if "unitary" in datadict:
                unitary = datadict["unitary"]

        yield BackendResult(
            c_bits=c_bits,
            q_bits=q_bits,
            shots=shots,
            counts=counts,
            state=state,
            unitary=unitary,
        )
コード例 #6
0
def _convert_result(resultdict: Dict[str, List[str]]) -> BackendResult:
    array_dict = {
        creg: np.array([list(a) for a in reslist]).astype(np.uint8)
        for creg, reslist in resultdict.items()
    }
    reversed_creg_names = sorted(array_dict.keys(), reverse=True)
    c_bits = [
        Bit(name, ind) for name in reversed_creg_names
        for ind in range(array_dict[name].shape[-1] - 1, -1, -1)
    ]

    stacked_array = np.hstack(
        [array_dict[name] for name in reversed_creg_names])
    return BackendResult(
        c_bits=c_bits,
        shots=OutcomeArray.from_readouts(
            cast(Sequence[Sequence[int]], stacked_array)),
    )
コード例 #7
0
    def get_result(self, handle: ResultHandle,
                   **kwargs: KwargTypes) -> BackendResult:
        """
        See :py:meth:`pytket.backends.Backend.get_result`.
        Supported kwargs: none.
        """
        try:
            return super().get_result(handle)
        except CircuitNotRunError:
            if handle not in self._cache:
                raise CircuitNotRunError(handle)

            qam = self._cache[handle]["qam"]
            shots = qam.wait().read_memory(region_name="ro")
            shots = OutcomeArray.from_readouts(shots)
            res = BackendResult(shots=shots,
                                c_bits=self._cache[handle]["c_bits"])
            self._cache[handle].update({"result": res})
            return res
コード例 #8
0
ファイル: cirq.py プロジェクト: RimaHajjar/pytket-extensions
 def run_circuit(self, circuit: Circuit,
                 **kwargs: KwargTypes) -> BackendResult:
     cirq_circ = tk_to_cirq(circuit)
     bit_to_qubit_map = {b: q for q, b in circuit.qubit_to_bit_map.items()}
     if not cirq_circ.has_measurements():  # type: ignore
         n_shots = cast(int, kwargs.get("n_shots"))
         return self.empty_result(circuit, n_shots=n_shots)
     else:
         run = self._simulator.run(cirq_circ,
                                   repetitions=cast(int,
                                                    kwargs.get("n_shots")))
         run_dict = run.data.to_dict()
         c_bits = [
             bit for key in run_dict.keys()
             for bit in bit_to_qubit_map.keys() if str(bit) == key
         ]
         individual_readouts = [
             list(readout.values()) for readout in run_dict.values()
         ]
         shots = OutcomeArray.from_readouts(
             [list(r) for r in zip(*individual_readouts)])
         return BackendResult(shots=shots, c_bits=c_bits)