Ejemplo n.º 1
0
    def controlled(
        self,
        num_controls: int = None,
        control_values: Optional[Sequence[Union[int, Collection[int]]]] = None,
        control_qid_shape: Optional[Tuple[int, ...]] = None,
    ) -> raw_types.Gate:
        """Returns a controlled `XPowGate` with two additional controls.

        The `controlled` method of the `Gate` class, of which this class is a
        child, returns a `ControlledGate` with `sub_gate = self`. This method
        overrides this behavior to return a `ControlledGate` with
        `sub_gate = XPowGate`.
        """
        if num_controls == 0:
            return self
        return controlled_gate.ControlledGate(
            controlled_gate.ControlledGate(
                common_gates.XPowGate(exponent=self._exponent,
                                      global_shift=self._global_shift),
                num_controls=2,
            ),
            num_controls=num_controls,
            control_values=control_values,
            control_qid_shape=control_qid_shape,
        )
Ejemplo n.º 2
0
 def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> np.ndarray:
     if protocols.is_parameterized(self):
         return NotImplemented
     p = 1j**(2 * self._exponent * self._global_shift)
     if p != 1:
         args.target_tensor *= p
     return protocols.apply_unitary(controlled_gate.ControlledGate(
         controlled_gate.ControlledGate(pauli_gates.X**self.exponent)),
                                    protocols.ApplyUnitaryArgs(
                                        args.target_tensor,
                                        args.available_buffer, args.axes),
                                    default=NotImplemented)
Ejemplo n.º 3
0
 def _apply_unitary_to_tensor_(self,
                               target_tensor: np.ndarray,
                               available_buffer: np.ndarray,
                               axes: Sequence[int],
                               ) -> np.ndarray:
     return protocols.apply_unitary_to_tensor(
         controlled_gate.ControlledGate(
             controlled_gate.ControlledGate(
                 common_gates.X**self.exponent)),
         target_tensor,
         available_buffer,
         axes,
         default=NotImplemented)
Ejemplo n.º 4
0
 def gate(self) -> Optional['cirq.ControlledGate']:
     if self.sub_operation.gate is None:
         return None
     return controlled_gate.ControlledGate(
         self.sub_operation.gate,
         control_values=self.control_values,
         control_qid_shape=[q.dimension for q in self.controls])
Ejemplo n.º 5
0
 def _apply_unitary_(self,
                     args: 'protocols.ApplyUnitaryArgs') -> np.ndarray:
     return protocols.apply_unitary(
         controlled_gate.ControlledGate(swap_gates.SWAP),
         protocols.ApplyUnitaryArgs(args.target_tensor,
                                    args.available_buffer, args.axes),
         default=NotImplemented)
Ejemplo n.º 6
0
 def _apply_unitary_to_tensor_(
     self,
     target_tensor: np.ndarray,
     available_buffer: np.ndarray,
     axes: Sequence[int],
 ) -> np.ndarray:
     if protocols.is_parameterized(self):
         return NotImplemented
     p = 1j**(2 * self._exponent * self._global_shift)
     if p != 1:
         target_tensor *= p
     return protocols.apply_unitary_to_tensor(
         controlled_gate.ControlledGate(
             controlled_gate.ControlledGate(common_gates.X**self.exponent)),
         target_tensor,
         available_buffer,
         axes,
         default=NotImplemented)
Ejemplo n.º 7
0
    def controlled(
        self,
        num_controls: int = None,
        control_values: Optional[Sequence[Union[int, Collection[int]]]] = None,
        control_qid_shape: Optional[Tuple[int, ...]] = None,
    ) -> raw_types.Gate:
        """Returns a controlled `SWAP` with one additional control.

        The `controlled` method of the `Gate` class, of which this class is a
        child, returns a `ControlledGate` with `sub_gate = self`. This method
        overrides this behavior to return a `ControlledGate` with
        `sub_gate = SWAP`.
        """
        if num_controls == 0:
            return self
        return controlled_gate.ControlledGate(
            controlled_gate.ControlledGate(swap_gates.SWAP, num_controls=1),
            num_controls=num_controls,
            control_values=control_values,
            control_qid_shape=control_qid_shape,
        )