Esempio n. 1
0
    def validate_args(self, qubits: Sequence[Qid]) -> None:
        """Checks if this gate can be applied to the given qubits.

        By default checks if input is of type Qid and qubit count.
        Child classes can override.

        Args:
            qubits: The collection of qubits to potentially apply the gate to.

        Throws:
            ValueError: The gate can't be applied to the qubits.
        """
        if len(qubits) != qid_shape_protocol.num_qubits(self):
            raise ValueError('Wrong number of qubits for <{!r}>. '
                             'Expected {} qubits but got <{!r}>.'.format(
                                 self, qid_shape_protocol.num_qubits(self),
                                 qubits))

        if any([not isinstance(qubit, Qid) for qubit in qubits]):
            raise ValueError('Gate was called with type different than Qid.')
Esempio n. 2
0
    def __pow__(self, power):
        if power == 1:
            return self

        if power == -1:
            # HACK: break cycle
            from cirq.devices import line_qubit

            decomposed = decompose.decompose_once_with_qubits(
                self,
                qubits=line_qubit.LineQubit.range(
                    qid_shape_protocol.num_qubits(self)),
                default=None)
            if decomposed is None:
                return NotImplemented

            inverse_decomposed = inverse.inverse(decomposed, None)
            if inverse_decomposed is None:
                return NotImplemented

            return _InverseCompositeGate(self)

        return NotImplemented
Esempio n. 3
0
 def _num_qubits_(self):
     return qid_shape_protocol.num_qubits(self._original)
Esempio n. 4
0
 def _backwards_compatibility_num_qubits(self) -> int:
     return qid_shape_protocol.num_qubits(self)