Exemplo n.º 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)
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
Exemplo n.º 3
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 `XPowGate` 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 = XPowGate`.
        """
        if num_controls == 0:
            return self
        return controlled_gate.ControlledGate(
            controlled_gate.ControlledGate(
                common_gates.XPowGate(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 _h(self, g: common_gates.HPowGate, axis: int):
     exponent = g.exponent
     if exponent % 2 == 0:
         return
     if exponent % 1 != 0:
         raise ValueError('H exponent must be integer')  # coverage: ignore
     self._y(common_gates.YPowGate(exponent=0.5), axis)
     self._x(common_gates.XPowGate(), axis)
Exemplo n.º 5
0
 def _value_equality_values_(self):
     if self.phase_exponent == 0:
         return common_gates.XPowGate(
             exponent=self._exponent,
             global_shift=self._global_shift)._value_equality_values_()
     if self.phase_exponent == 0.5:
         return common_gates.YPowGate(
             exponent=self._exponent,
             global_shift=self._global_shift)._value_equality_values_()
     return self.phase_exponent, self._canonical_exponent, self._global_shift
Exemplo n.º 6
0
    def _act_on_(self, sim_state: 'cirq.SimulationStateBase',
                 qubits: Sequence['cirq.Qid']):
        if len(qubits) != 1:
            return NotImplemented

        from cirq.sim import simulation_state

        if (isinstance(sim_state, simulation_state.SimulationState)
                and not sim_state.can_represent_mixed_states):
            result = sim_state._perform_measurement(qubits)[0]
            gate = common_gates.XPowGate(
                dimension=self.dimension)**(self.dimension - result)
            protocols.act_on(gate, sim_state, qubits)
            return True

        return NotImplemented
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
Exemplo n.º 8
0
 def __pow__(self: '_PauliX',
             exponent: 'cirq.TParamVal') -> common_gates.XPowGate:
     return common_gates.XPowGate(
         exponent=exponent) if exponent != 1 else _PauliX()
Exemplo n.º 9
0
def _bit_flip_X() -> common_gates.XPowGate:
    """
    Returns a cirq.X with guarantees a bit flip.
    """
    return common_gates.XPowGate()
Exemplo n.º 10
0
 def __pow__(self: '_PauliX',
             exponent: value.TParamVal) -> common_gates.XPowGate:
     return common_gates.XPowGate(exponent=exponent)
Exemplo n.º 11
0
 def _decompose_(self, qubits: Tuple['cirq.Qid', ...]) -> 'cirq.OP_TREE':
     yield common_gates.XPowGate(exponent=0.5).on_each(*qubits)
     yield ZZPowGate(exponent=self.exponent, global_shift=self.global_shift)(*qubits)
     yield common_gates.XPowGate(exponent=-0.5).on_each(*qubits)
Exemplo n.º 12
0
 def __pow__(self: '_PauliX',
             exponent: Union[sympy.Basic, float]) -> common_gates.XPowGate:
     return common_gates.XPowGate(exponent=exponent)