Exemplo n.º 1
0
def assert_has_consistent_apply_unitary_for_various_exponents(
        val: Any,
        *,
        exponents=(0, 1, -1, 0.5, 0.25, -0.5, 0.1, sympy.Symbol('s')),
        qubit_count: Optional[int] = None) -> None:
    """Tests whether a value's _apply_unitary_ is correct.

    Contrasts the effects of the value's `_apply_unitary_` with the
    matrix returned by the value's `_unitary_` method. Attempts this after
    attempting to raise the value to several exponents.

    Args:
        val: The value under test. Should have a `__pow__` method.
        exponents: The exponents to try. Defaults to a variety of special and
            arbitrary angles, as well as a parameterized angle (a symbol). If
            the value's `__pow__` returns `NotImplemented` for any of these,
            they are skipped.
        qubit_count: A minimum qubit count for the test system. This argument
            isn't needed if the gate has a unitary matrix or implements
            `cirq.SingleQubitGate`/`cirq.TwoQubitGate`/`cirq.ThreeQubitGate`; it
            will be inferred.
    """
    for exponent in exponents:
        gate = protocols.pow(val, exponent, default=None)
        if gate is not None:
            assert_has_consistent_apply_unitary(
                gate,
                qubit_count=qubit_count)
Exemplo n.º 2
0
 def __pow__(self, exponent: Any) -> 'ControlledOperation':
     new_sub_op = protocols.pow(self.sub_operation, exponent,
                                NotImplemented)
     if new_sub_op is NotImplemented:
         return NotImplemented
     return ControlledOperation(self.controls, new_sub_op,
                                self.control_values)
Exemplo n.º 3
0
def assert_implements_consistent_protocols(
    val: Any,
    *,
    exponents: Sequence[Any] = (0, 1, -1, 0.25, -0.5, 0.1, sympy.Symbol('s')),
    qubit_count: Optional[int] = None,
    ignoring_global_phase: bool = False,
    setup_code: str = 'import cirq\nimport numpy as np\nimport sympy',
    global_vals: Optional[Dict[str, Any]] = None,
    local_vals: Optional[Dict[str, Any]] = None,
) -> None:
    """Checks that a value is internally consistent and has a good __repr__."""
    global_vals = global_vals or {}
    local_vals = local_vals or {}

    _assert_meets_standards_helper(
        val,
        ignoring_global_phase=ignoring_global_phase,
        setup_code=setup_code,
        global_vals=global_vals,
        local_vals=local_vals,
    )

    for exponent in exponents:
        p = protocols.pow(val, exponent, None)
        if p is not None:
            _assert_meets_standards_helper(
                val**exponent,
                ignoring_global_phase=ignoring_global_phase,
                setup_code=setup_code,
                global_vals=global_vals,
                local_vals=local_vals,
            )
Exemplo n.º 4
0
 def __pow__(self, exponent: Any) -> 'ControlledGate':
     new_sub_gate = protocols.pow(self.sub_gate,
                                  exponent,
                                  NotImplemented)
     if new_sub_gate is NotImplemented:
         return NotImplemented
     return ControlledGate(new_sub_gate, self.control_qubits)
Exemplo n.º 5
0
 def __pow__(self, exponent: Any) -> 'ControlledGate':
     new_sub_gate = protocols.pow(self.sub_gate, exponent, NotImplemented)
     if new_sub_gate is NotImplemented:
         return NotImplemented
     return ControlledGate(new_sub_gate,
                           self.num_controls(),
                           control_values=self.control_values,
                           control_qid_shape=self.control_qid_shape)
Exemplo n.º 6
0
 def __pow__(self, power):
     if power == 1:
         return self
     new_ops = []
     for op in self.operations:
         new_op = protocols.pow(op, power, default=None)
         if new_op is None:
             return NotImplemented
         new_ops.append(new_op)
     return Moment(new_ops)
Exemplo n.º 7
0
def assert_implements_consistent_protocols(
        val: Any,
        *,
        exponents: Sequence[Any] = (0, 1, -1, 0.5, 0.25, -0.5, 0.1,
                                    value.Symbol('s')),
        qubit_count: Optional[int] = None,
        setup_code: str = 'import cirq\nimport numpy as np') -> None:
    """Checks that a value is internally consistent and has a good __repr__."""

    _assert_meets_standards_helper(val, qubit_count, setup_code)

    for exponent in exponents:
        p = protocols.pow(val, exponent, None)
        if p is not None:
            _assert_meets_standards_helper(val**exponent, qubit_count,
                                           setup_code)
Exemplo n.º 8
0
    def __pow__(self, exponent: Any) -> 'ParallelGate':
        """Raises underlying gate to a power, applying same number of copies.

        For extrapolatable gate G this means the following two are equivalent:

            (G ** 1.5) x k  or  (G x k) ** 1.5

        Args:
            exponent: The amount to scale the gate's effect by.

        Returns:
            ParallelGate with same num_copies with the scaled underlying gate.
        """
        new_gate = protocols.pow(self.sub_gate, exponent, NotImplemented)
        if new_gate is NotImplemented:
            return NotImplemented
        return self.with_gate(new_gate)
Exemplo n.º 9
0
    def __pow__(self, exponent: Any) -> 'cirq.Operation':
        """Raise gate to a power, then reapply to the same qubits.

        Only works if the gate implements cirq.ExtrapolatableEffect.
        For extrapolatable gate G this means the following two are equivalent:

            (G ** 1.5)(qubit)  or  G(qubit) ** 1.5

        Args:
            exponent: The amount to scale the gate's effect by.

        Returns:
            A new operation on the same qubits with the scaled gate.
        """
        new_gate = protocols.pow(self.gate, exponent, NotImplemented)
        if new_gate is NotImplemented:
            return NotImplemented
        return self.with_gate(new_gate)
Exemplo n.º 10
0
def assert_has_consistent_apply_unitary_for_various_exponents(
    val: Any, *,
    exponents=(0, 1, -1, 0.5, 0.25, -0.5, 0.1, sympy.Symbol('s'))) -> None:
    """Tests whether a value's _apply_unitary_ is correct.

    Contrasts the effects of the value's `_apply_unitary_` with the
    matrix returned by the value's `_unitary_` method. Attempts this after
    attempting to raise the value to several exponents.

    Args:
        val: The value under test. Should have a `__pow__` method.
        exponents: The exponents to try. Defaults to a variety of special and
            arbitrary angles, as well as a parameterized angle (a symbol). If
            the value's `__pow__` returns `NotImplemented` for any of these,
            they are skipped.
    """
    for exponent in exponents:
        gate = protocols.pow(val, exponent, default=None)
        if gate is not None:
            assert_has_consistent_apply_unitary(gate)