Пример #1
0
    def to_cirq_result(
        self,
        params: Optional[study.ParamResolver] = None,
    ) -> study.Result:
        """Returns a `cirq.Result` for these results.

        `cirq.Result` contains a less dense representation of results than that returned by
        the IonQ API.  Typically these results are also ordered by when they were run, though
        that contract is implicit.  Because the IonQ API does not retain that ordering information,
        the order of these `cirq.Result` objects should *not* be interpetted as representing the
        order in which the circuit was repeated.

        Args:
            params: The `cirq.ParamResolver` used to generate these results.

        Returns:
            The `cirq.Result` for these results.

        Raises:
            ValueError: If the circuit used to produce this result had no measurement gates
                (and hence no measurement keys).
        """
        if len(self.measurement_dict()) == 0:
            raise ValueError(
                'Can convert to cirq results only if the circuit had measurement gates '
                'with measurement keys.')
        measurements = {}
        for key, targets in self.measurement_dict().items():
            qpu_results = list(self.counts(key).elements())
            measurements[key] = np.array(
                list(
                    digits.big_endian_int_to_bits(x, bit_count=len(targets))
                    for x in qpu_results))
        return study.Result(params=params or study.ParamResolver({}),
                            measurements=measurements)
Пример #2
0
def _pretty_str_dict(value: dict, bit_count: int) -> str:
    """Pretty prints a dict, converting int dict values to bit strings."""
    strs = []
    for k, v in value.items():
        bits = ''.join(
            str(b)
            for b in digits.big_endian_int_to_bits(k, bit_count=bit_count))
        strs.append(f'{bits}: {v}')
    return '\n'.join(strs)
Пример #3
0
def _little_endian_to_big(value: int, bit_count: int) -> int:
    return digits.big_endian_bits_to_int(
        digits.big_endian_int_to_bits(value, bit_count=bit_count)[::-1]
    )