Example #1
0
 def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
     x0, x1, x2, x3 = self._diag_angles_radians
     q0, q1 = qubits
     yield common_gates.ZPowGate(exponent=x2 / np.pi).on(q0)
     yield common_gates.ZPowGate(exponent=x1 / np.pi).on(q1)
     yield common_gates.CZPowGate(exponent=(x3 - (x1 + x2)) / np.pi).on(
         q0, q1)
     yield common_gates.XPowGate().on_each(q0, q1)
     yield common_gates.CZPowGate(exponent=x0 / np.pi).on(q0, q1)
     yield common_gates.XPowGate().on_each(q0, q1)
Example #2
0
    def _decompose_(self, qubits):
        a, b = qubits

        yield common_gates.CNOT(a, b)
        yield common_gates.H(a)
        yield common_gates.CNOT(b, a)
        yield common_gates.ZPowGate(exponent=self._exponent / 2,
                                    global_shift=self.global_shift).on(a)
        yield common_gates.CNOT(b, a)
        yield common_gates.ZPowGate(exponent=-self._exponent / 2,
                                    global_shift=-self.global_shift).on(a)
        yield common_gates.H(a)
        yield common_gates.CNOT(a, b)
def _strat_act_on_stabilizer_ch_form_from_single_qubit_decompose(
        val: Any, args: 'cirq.ActOnStabilizerCHFormArgs') -> bool:
    if num_qubits(val) == 1:
        if not has_unitary(val):
            return NotImplemented
        u = unitary(val)
        clifford_gate = SingleQubitCliffordGate.from_unitary(u)
        if clifford_gate is not None:
            # Gather the effective unitary applied so as to correct for the
            # global phase later.
            final_unitary = np.eye(2)
            for axis, quarter_turns in clifford_gate.decompose_rotation():
                gate = None  # type: Optional[cirq.Gate]
                if axis == pauli_gates.X:
                    gate = common_gates.XPowGate(exponent=quarter_turns / 2)
                    assert gate._act_on_(args)
                elif axis == pauli_gates.Y:
                    gate = common_gates.YPowGate(exponent=quarter_turns / 2)
                    assert gate._act_on_(args)
                else:
                    assert axis == pauli_gates.Z
                    gate = common_gates.ZPowGate(exponent=quarter_turns / 2)
                    assert gate._act_on_(args)

                final_unitary = np.matmul(unitary(gate), final_unitary)

            # Find the entry with the largest magnitude in the input unitary.
            k = max(np.ndindex(*u.shape), key=lambda t: abs(u[t]))
            # Correct the global phase that wasn't conserved in the above
            # decomposition.
            args.state.omega *= u[k] / final_unitary[k]
            return True

    return NotImplemented
Example #4
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 `ZPowGate` 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 = ZPowGate`.
        """
        if num_controls == 0:
            return self
        return controlled_gate.ControlledGate(
            controlled_gate.ControlledGate(
                common_gates.ZPowGate(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,
        )
 def _x(self, g: common_gates.XPowGate, axis: int):
     exponent = g.exponent
     if exponent % 2 != 0:
         if exponent % 0.5 != 0.0:
             raise ValueError('Y exponent must be half integer')  # coverage: ignore
         self._h(common_gates.H, axis)
         self._z(common_gates.ZPowGate(exponent=exponent), axis)
         self._h(common_gates.H, axis)
     self.state.omega *= _phase(g)
def _strat_act_on_clifford_tableau_from_single_qubit_decompose(
    val: Any, args: 'cirq.ActOnCliffordTableauArgs', qubits: Sequence['cirq.Qid']
) -> bool:
    if num_qubits(val) == 1:
        if not has_unitary(val):
            return NotImplemented
        u = unitary(val)
        clifford_gate = SingleQubitCliffordGate.from_unitary(u)
        if clifford_gate is not None:
            for axis, quarter_turns in clifford_gate.decompose_rotation():
                if axis == pauli_gates.X:
                    common_gates.XPowGate(exponent=quarter_turns / 2)._act_on_(args, qubits)
                elif axis == pauli_gates.Y:
                    common_gates.YPowGate(exponent=quarter_turns / 2)._act_on_(args, qubits)
                else:
                    assert axis == pauli_gates.Z
                    common_gates.ZPowGate(exponent=quarter_turns / 2)._act_on_(args, qubits)
            return True

    return NotImplemented
Example #7
0
 def __pow__(self: '_PauliZ',
             exponent: 'cirq.TParamVal') -> common_gates.ZPowGate:
     return common_gates.ZPowGate(
         exponent=exponent) if exponent != 1 else _PauliZ()
Example #8
0
def _phase_flip_Z() -> common_gates.ZPowGate:
    """
    Returns a cirq.Z which corresponds to a guaranteed phase flip.
    """
    return common_gates.ZPowGate()
Example #9
0
 def _decompose_(self, qubits):
     yield common_gates.ZPowGate(exponent=self.exponent)(qubits[0])
     yield common_gates.ZPowGate(exponent=self.exponent)(qubits[1])
     yield common_gates.CZPowGate(exponent=-2 * self.exponent,
                                  global_shift=-self.global_shift / 2)(
                                      qubits[0], qubits[1])
Example #10
0
 def __pow__(self: '_PauliZ',
             exponent: value.TParamVal) -> common_gates.ZPowGate:
     return common_gates.ZPowGate(exponent=exponent)
Example #11
0
 def __pow__(self: '_PauliZ',
             exponent: Union[sympy.Basic, float]) -> common_gates.ZPowGate:
     return common_gates.ZPowGate(exponent=exponent)