def _matrix_to_pauli_string_phasors(self, mat: np.ndarray, qubit: ops.Qid) -> ops.OP_TREE: rotations = optimizers.single_qubit_matrix_to_pauli_rotations( mat, self.atol) out_ops = [] # type: List[ops.Operation] for pauli, half_turns in rotations: if (self.keep_clifford and linalg.all_near_zero_mod(half_turns, 0.5)): cliff_gate = ops.SingleQubitCliffordGate.from_quarter_turns( pauli, round(half_turns * 2)) if out_ops and not isinstance(out_ops[-1], ops.PauliStringPhasor): op = cast(ops.GateOperation, out_ops[-1]) gate = cast(ops.SingleQubitCliffordGate, op.gate) out_ops[-1] = gate.merged_with(cliff_gate)(qubit) else: out_ops.append( cliff_gate(qubit)) else: pauli_string = ops.PauliString.from_single(qubit, pauli) out_ops.append( ops.PauliStringPhasor(pauli_string, exponent_neg=round(half_turns, 10))) return out_ops
def _matrix_to_clifford_op(self, mat: np.ndarray, qubit: 'cirq.Qid') -> Optional[ops.Operation]: rotations = optimizers.single_qubit_matrix_to_pauli_rotations(mat, self.atol) clifford_gate = ops.SingleQubitCliffordGate.I for pauli, half_turns in rotations: if linalg.all_near_zero_mod(half_turns, 0.5): clifford_gate = clifford_gate.merged_with( self._rotation_to_clifford_gate(pauli, half_turns) ) else: return None return clifford_gate(qubit)