示例#1
0
 def __pow__(self, exponent: Union[float,
                                   sympy.Symbol]) -> 'PauliStringPhasor':
     pn = protocols.mul(self.exponent_neg, exponent, None)
     pp = protocols.mul(self.exponent_pos, exponent, None)
     if pn is None or pp is None:
         return NotImplemented
     return PauliStringPhasor(self.pauli_string,
                              exponent_neg=pn,
                              exponent_pos=pp)
示例#2
0
    def __imul__(self, other):
        if isinstance(other, BaseDensePauliString):
            if len(other) > len(self):
                raise ValueError(
                    "The receiving dense pauli string is smaller than "
                    "the dense pauli string being multiplied into it.\n"
                    f"self={repr(self)}\n"
                    f"other={repr(other)}")
            self_mask = self.pauli_mask[:len(other.pauli_mask)]
            self.coefficient *= _vectorized_pauli_mul_phase(
                self_mask, other.pauli_mask)
            self.coefficient *= other.coefficient
            self_mask ^= other.pauli_mask
            return self

        if isinstance(other, (sympy.Basic, numbers.Number)):
            new_coef = protocols.mul(self.coefficient, other, default=None)
            if new_coef is None:
                return NotImplemented
            self.coefficient = new_coef if isinstance(
                new_coef, sympy.Basic) else complex(new_coef)
            return self

        split = _attempt_value_to_pauli_index(other)
        if split is not None:
            p, i = split
            self.coefficient *= _vectorized_pauli_mul_phase(
                self.pauli_mask[i], p)
            self.pauli_mask[i] ^= p
            return self

        return NotImplemented
示例#3
0
 def __pow__(self, exponent: Any) -> 'ThreeQubitDiagonalGate':
     if not isinstance(exponent, (int, float, sympy.Basic)):
         return NotImplemented
     return ThreeQubitDiagonalGate([
         protocols.mul(angle, exponent, NotImplemented)
         for angle in self._diag_angles_radians
     ])
示例#4
0
    def __mul__(self, other):
        if isinstance(other, BaseDensePauliString):
            max_len = max(len(self.pauli_mask), len(other.pauli_mask))
            min_len = min(len(self.pauli_mask), len(other.pauli_mask))
            new_mask = np.zeros(max_len, dtype=np.uint8)
            new_mask[:len(self.pauli_mask)] ^= self.pauli_mask
            new_mask[:len(other.pauli_mask)] ^= other.pauli_mask
            tweak = _vectorized_pauli_mul_phase(self.pauli_mask[:min_len],
                                                other.pauli_mask[:min_len])
            return DensePauliString(pauli_mask=new_mask,
                                    coefficient=self.coefficient *
                                    other.coefficient * tweak)

        if isinstance(other, (sympy.Basic, numbers.Number)):
            new_coef = protocols.mul(self.coefficient, other, default=None)
            if new_coef is None:
                return NotImplemented
            return DensePauliString(pauli_mask=self.pauli_mask,
                                    coefficient=new_coef)

        split = _attempt_value_to_pauli_index(other)
        if split is not None:
            p, i = split
            mask = np.copy(self.pauli_mask)
            mask[i] ^= p
            return DensePauliString(
                pauli_mask=mask,
                coefficient=self.coefficient *
                _vectorized_pauli_mul_phase(self.pauli_mask[i], p),
            )

        return NotImplemented
示例#5
0
 def __pow__(self, exponent: Union[float, sympy.Symbol]) -> 'PhasedXPowGate':
     new_exponent = protocols.mul(self._exponent, exponent, NotImplemented)
     if new_exponent is NotImplemented:
         return NotImplemented
     return PhasedXPowGate(phase_exponent=self._phase_exponent,
                           exponent=new_exponent,
                           global_shift=self._global_shift)
    def repeat(
            self,
            repetitions: Optional[IntParam] = None,
            repetition_ids: Optional[Sequence[str]] = None
    ) -> 'CircuitOperation':
        """Returns a copy of this operation repeated 'repetitions' times.
         Each repetition instance will be identified by a single repetition_id.

        Args:
            repetitions: Number of times this operation should repeat. This
                is multiplied with any pre-existing repetitions. If unset, it
                defaults to the length of `repetition_ids`.
            repetition_ids: List of IDs, one for each repetition. If unset,
                defaults to `default_repetition_ids(repetitions)`.

        Returns:
            A copy of this operation repeated `repetitions` times with the
            appropriate `repetition_ids`. The output `repetition_ids` are the
            cartesian product of input `repetition_ids` with the base
            operation's `repetition_ids`. If the base operation has unset
            `repetition_ids` (indicates {-1, 0, 1} `repetitions` with no custom
            IDs), the input `repetition_ids` are directly used.

        Raises:
            TypeError: `repetitions` is not an integer value.
            ValueError: Unexpected length of `repetition_ids`.
            ValueError: Both `repetitions` and `repetition_ids` are None.
        """
        if repetitions is None:
            if repetition_ids is None:
                raise ValueError(
                    'At least one of repetitions and repetition_ids must be set'
                )
            repetitions = len(repetition_ids)

        if isinstance(repetitions, INT_CLASSES):
            if repetitions == 1 and repetition_ids is None:
                # As CircuitOperation is immutable, this can safely return the original.
                return self

            expected_repetition_id_length = abs(repetitions)

            if repetition_ids is None:
                if self.use_repetition_ids:
                    repetition_ids = default_repetition_ids(
                        expected_repetition_id_length)
            elif len(repetition_ids) != expected_repetition_id_length:
                raise ValueError(
                    f'Expected repetition_ids={repetition_ids} length to be '
                    f'{expected_repetition_id_length}')

        # If either self.repetition_ids or repetitions is None, it returns the other unchanged.
        repetition_ids = _full_join_string_lists(repetition_ids,
                                                 self.repetition_ids)

        # The eventual number of repetitions of the returned CircuitOperation.
        final_repetitions = protocols.mul(self.repetitions, repetitions)
        return self.replace(repetitions=final_repetitions,
                            repetition_ids=repetition_ids)
示例#7
0
 def __pow__(self, exponent: Any) -> 'DiagonalGate':
     if not isinstance(exponent, (int, float, sympy.Basic)):
         return NotImplemented
     angles = []
     for angle in self._diag_angles_radians:
         mul_angle = protocols.mul(angle, exponent, NotImplemented)
         angles.append(mul_angle)
     return DiagonalGate(angles)
示例#8
0
 def _decompose_(self) -> ops.OP_TREE:
     if len(self.pauli_string) <= 0:
         return
     if self.pauli_string.coefficient not in [-1, +1]:
         raise NotImplementedError("TODO: arbitrary coefficients.")
     qubits = self.qubits
     any_qubit = qubits[0]
     to_z_ops = ops.freeze_op_tree(self.pauli_string.to_z_basis_ops())
     xor_decomp = tuple(xor_nonlocal_decompose(qubits, any_qubit))
     yield to_z_ops
     yield xor_decomp
     sign = self.pauli_string.coefficient.real
     yield ops.Z(any_qubit)**protocols.mul(self.half_turns, sign)
     yield protocols.inverse(xor_decomp)
     yield protocols.inverse(to_z_ops)
示例#9
0
 def __pow__(self, exponent: Union[float,
                                   sympy.Symbol]) -> 'PauliStringPhasor':
     new_exponent = protocols.mul(self.half_turns, exponent, NotImplemented)
     if new_exponent is NotImplemented:
         return NotImplemented
     return self._with_half_turns(new_exponent)
示例#10
0
文件: eigen_gate.py 项目: yy8yy/Cirq
 def __pow__(self: TSelf, exponent: Union[float,
                                          sympy.Symbol]) -> 'EigenGate':
     new_exponent = protocols.mul(self._exponent, exponent, NotImplemented)
     if new_exponent is NotImplemented:
         return NotImplemented
     return self._with_exponent(exponent=new_exponent)
示例#11
0
 def __pow__(self, exponent: Any) -> 'ExpWGate':
     new_exponent = protocols.mul(self.exponent, exponent, NotImplemented)
     if new_exponent is NotImplemented:
         return NotImplemented
     return ExpWGate(exponent=new_exponent,
                     phase_exponent=self.phase_exponent)
示例#12
0
文件: xmon_gates.py 项目: xcgfth/Cirq
 def __pow__(self, exponent: Any) -> 'ExpWGate':
     new_exponent = protocols.mul(self.half_turns, exponent, NotImplemented)
     if new_exponent is NotImplemented:
         return NotImplemented
     return ExpWGate(half_turns=new_exponent,
                     axis_half_turns=self.axis_half_turns)