def _pauli_expansion_(self) -> value.LinearDict[str]:
        """Computes Pauli expansion of self from Pauli expansions of terms."""

        def extend_term(
            pauli_names: str, qubits: tuple[cirq.Qid, ...], all_qubits: tuple[cirq.Qid, ...]
        ) -> str:
            """Extends Pauli product on qubits to product on all_qubits."""
            assert len(pauli_names) == len(qubits)
            qubit_to_pauli_name = dict(zip(qubits, pauli_names))
            return ''.join(qubit_to_pauli_name.get(q, 'I') for q in all_qubits)

        def extend(
            expansion: value.LinearDict[str],
            qubits: tuple[cirq.Qid, ...],
            all_qubits: tuple[cirq.Qid, ...],
        ) -> value.LinearDict[str]:
            """Extends Pauli expansion on qubits to expansion on all_qubits."""
            return value.LinearDict(
                {extend_term(p, qubits, all_qubits): c for p, c in expansion.items()}
            )

        result: value.LinearDict[str] = value.LinearDict({})
        for op, coefficient in self.items():
            expansion = protocols.pauli_expansion(op)
            extended_expansion = extend(expansion, op.qubits, self.qubits)
            result += extended_expansion * coefficient
        return result
Computes Pauli expansion of self from Pauli expansions of terms.
cirq-core/cirq/ops/linear_combinations.py