Esempio n. 1
0
    def __init__(self, *,  # Forces keyword args.
                 axis_half_turns: Optional[Union[value.Symbol, float]] = None,
                 axis_rads: Optional[float] = None,
                 axis_degs: Optional[float] = None,
                 half_turns: Optional[Union[value.Symbol, float]] = None,
                 rads: Optional[float] = None,
                 degs: Optional[float] = None) -> None:
        """Initializes the gate.

        At most one rotation angle argument may be specified. At most one axis
        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.

        The axis angle determines the rotation axis in the XY plane, with 0
        being positive-ward along X and 90 degrees being positive-ward along Y.

        Args:
            axis_half_turns: The axis angle in the XY plane, in half_turns.
            axis_rads: The axis angle in the XY plane, in radians.
            axis_degs: The axis angle in the XY plane, in degrees.
            half_turns: The amount to rotate, in half_turns.
            rads: The amount to rotate, in radians.
            degs: The amount to rotate, in degrees.
        """
        self.half_turns = value.chosen_angle_to_canonical_half_turns(
            half_turns=half_turns,
            rads=rads,
            degs=degs)
        self.axis_half_turns = value.chosen_angle_to_canonical_half_turns(
            half_turns=axis_half_turns,
            rads=axis_rads,
            degs=axis_degs,
            default=0.0)

        if (not isinstance(self.half_turns, value.Symbol) and
                not isinstance(self.axis_half_turns, value.Symbol) and
                not 0 <= self.axis_half_turns < 1):
            # Canonicalize to negative rotation around positive axis.
            self.half_turns = value.canonicalize_half_turns(-self.half_turns)
            self.axis_half_turns = value.canonicalize_half_turns(
                self.axis_half_turns + 1)
Esempio n. 2
0
    def __new__(cls,
                *,
                phase_exponent: Union[float, sympy.Symbol],
                exponent: Union[float, sympy.Symbol] = 1.0,
                global_shift: float = 0.0):
        """Substitutes a raw X or raw Y if possible.

        Args:
            phase_exponent: The exponent on the Z gates conjugating the X gate.
            exponent: The exponent on the X gate conjugated by Zs.
            global_shift: How much to shift the operation's eigenvalues at
                exponent=1.
        """
        p = value.canonicalize_half_turns(phase_exponent)
        if p == 0:
            return cirq.ops.common_gates.XPowGate(exponent=exponent,
                                                  global_shift=global_shift)
        if p == 0.5:
            return cirq.ops.common_gates.YPowGate(exponent=exponent,
                                                  global_shift=global_shift)
        return super().__new__(cls)
Esempio n. 3
0
    def _qasm_(self, args: 'protocols.QasmArgs',
               qubits: Tuple[raw_types.Qid, ...]) -> Optional[str]:
        if cirq.is_parameterized(self):
            return None

        args.validate_version('2.0')

        e = cast(float, value.canonicalize_half_turns(self._exponent))
        p = cast(float, self.phase_exponent)
        epsilon = 10**-args.precision

        if abs(e + 0.5) <= epsilon:
            return args.format('u2({0:half_turns}, {1:half_turns}) {2};\n',
                               p + 0.5, -p - 0.5, qubits[0])

        if abs(e - 0.5) <= epsilon:
            return args.format('u2({0:half_turns}, {1:half_turns}) {2};\n',
                               p - 0.5, -p + 0.5, qubits[0])

        return args.format(
            'u3({0:half_turns}, {1:half_turns}, {2:half_turns}) {3};\n', -e,
            p + 0.5, -p - 0.5, qubits[0])
Esempio n. 4
0
 def exponent_relative(self) -> Union[int, float, sympy.Basic]:
     return value.canonicalize_half_turns(self.exponent_neg -
                                          self.exponent_pos)
Esempio n. 5
0
 def exponent_relative(self) -> Union[int, float, sympy.Expr]:
     """The relative exponent between negative and positive exponents."""
     return value.canonicalize_half_turns(self.exponent_neg -
                                          self.exponent_pos)
Esempio n. 6
0
 def exponent_neg(self, exponent_neg):
     """Sets the negative exponent."""
     # coverage: ignore
     self.gate._exponent_neg = value.canonicalize_half_turns(exponent_neg)
Esempio n. 7
0
 def exponent_pos(self, exponent_pos):
     """Sets the positive exponent."""
     # coverage: ignore
     self.gate._exponent_pos = value.canonicalize_half_turns(exponent_pos)