def __init__(self, *,  # Forces keyword args.
                 exponent: Optional[Union[sympy.Symbol, float]]=None,
                 rads: Optional[float]=None,
                 degs: Optional[float]=None,
                 duration: Optional[float]=None) -> None:
        """Initialize the gate.

        At most one of exponent, rads, degs, or duration may be specified.
        If more are specified, the result is considered ambiguous and an
        error is thrown. If no argument is given, the default value of one
        half-turn is used.

        Args:
            exponent: The exponent angle, in half-turns.
            rads: The exponent angle, in radians.
            degs: The exponent angle, in degrees.
            duration: The exponent as a duration of time.
        """

        if len([1 for e in [exponent, rads, degs, duration]
                if e is not None]) > 1:
            raise ValueError('Redundant exponent specification. '
                             'Use ONE of exponent, rads, degs, or duration.')

        if duration is not None:
            exponent = 2 * duration / np.pi
        else:
            exponent = cirq.chosen_angle_to_half_turns(
                half_turns=exponent,
                rads=rads,
                degs=degs)

        super().__init__(exponent=exponent)
Esempio n. 2
0
    def __init__(self,
                 weights: Tuple[float, float, float]=(1, 1, 1),
                 absorb_exponent: bool=True,
                 *,  # Forces keyword args.
                 exponent: Optional[Union[sympy.Symbol, float]]=None,
                 rads: Optional[float]=None,
                 degs: Optional[float]=None,
                 duration: Optional[float]=None
                 ) -> None:
        """Initialize the gate.

        At most one of exponent, rads, degs, or duration may be specified.
        If more are specified, the result is considered ambiguous and an
        error is thrown. If no argument is given, the default value of one
        half-turn is used.

        Args:
            weights: The weights of the terms in the Hamiltonian.
            absorb_exponent: Whether to absorb the given exponent into the
                weights. If true, the exponent of the returned gate is 1.
            exponent: The exponent angle, in half-turns.
            rads: The exponent angle, in radians.
            degs: The exponent angle, in degrees.
            duration: The exponent as a duration of time.
        """

        assert len(weights) == 3
        self.weights = weights

        if len([1 for e in [exponent, rads, degs, duration]
                if e is not None]) > 1:
            raise ValueError('Redundant exponent specification. '
                             'Use ONE of exponent, rads, degs, or duration.')

        if duration is not None:
            exponent = 2 * duration / np.pi
        else:
            exponent = cirq.chosen_angle_to_half_turns(
                half_turns=exponent,
                rads=rads,
                degs=degs)

        super().__init__(exponent=exponent)

        if absorb_exponent:
            self.absorb_exponent_into_weights()
    def __init__(
            self,
            *,  # Forces keyword args.
            half_turns: Optional[Union[cirq.Symbol, float]] = None,
            rads: Optional[float] = None,
            degs: Optional[float] = None) -> None:
        """Initializes the gate.

        At most one angle argument may be specified. If more are specified,
        the result is considered ambiguous and an error is thrown. If no angle
        argument is given, the default value of one half turn is used.

        Args:
            half_turns: Relative phasing of CCZ's eigenstates, in half_turns.
            rads: Relative phasing of CCZ's eigenstates, in radians.
            degs: Relative phasing of CCZ's eigenstates, in degrees.
        """
        super().__init__(exponent=cirq.chosen_angle_to_half_turns(
            half_turns=half_turns, rads=rads, degs=degs))
Esempio n. 4
0
def test_chosen_angle_to_half_turns():
    assert cirq.chosen_angle_to_half_turns() == 1
    assert cirq.chosen_angle_to_half_turns(default=0.5) == 0.5
    assert cirq.chosen_angle_to_half_turns(half_turns=0.25,
                                           default=0.75) == 0.25
    np.testing.assert_allclose(cirq.chosen_angle_to_half_turns(rads=np.pi / 2),
                               0.5,
                               atol=1e-8)
    np.testing.assert_allclose(cirq.chosen_angle_to_half_turns(rads=-np.pi /
                                                               4),
                               -0.25,
                               atol=1e-8)
    assert cirq.chosen_angle_to_half_turns(degs=90) == 0.5
    assert cirq.chosen_angle_to_half_turns(degs=1080) == 6.0
    assert cirq.chosen_angle_to_half_turns(degs=990) == 5.5

    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0, rads=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0, degs=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(degs=0, rads=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0, rads=0, degs=0)
Esempio n. 5
0
def test_chosen_angle_to_half_turns():
    assert cirq.chosen_angle_to_half_turns() == 1
    assert cirq.chosen_angle_to_half_turns(default=0.5) == 0.5
    assert cirq.chosen_angle_to_half_turns(half_turns=0.25,
                                                     default=0.75) == 0.25
    np.testing.assert_allclose(
        cirq.chosen_angle_to_half_turns(rads=np.pi/2),
        0.5,
        atol=1e-8)
    np.testing.assert_allclose(
        cirq.chosen_angle_to_half_turns(rads=-np.pi/4),
        -0.25,
        atol=1e-8)
    assert cirq.chosen_angle_to_half_turns(degs=90) == 0.5
    assert cirq.chosen_angle_to_half_turns(degs=1080) == 6.0
    assert cirq.chosen_angle_to_half_turns(degs=990) == 5.5

    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0, rads=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0, degs=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(degs=0, rads=0)
    with pytest.raises(ValueError):
        _ = cirq.chosen_angle_to_half_turns(half_turns=0,
                                                      rads=0,
                                                      degs=0)