def expectation_from_density_matrix(
        self, state: np.ndarray, qid_map: Mapping[raw_types.Qid, int]
    ) -> float:
        """Expectation of the sum of projections from a density matrix.

        Projects the density matrix onto the sum of projectors and computes the expectation of the
        measurements.

        Args:
            state: An array representing a valid  density matrix.
            qid_map: A map from all qubits used in this ProjectorSum to the indices of the qubits
                that `state_vector` is defined over.

        Returns:
            The expectation value of the input state.
        """
        return sum(
            coeff
            * _projector_string_from_projector_dict(vec).expectation_from_density_matrix(
                state, qid_map
            )
            for vec, coeff in self._linear_dict.items()
        )
Expectation of the sum of projections from a density matrix.

        Projects the density matrix onto the sum of projectors and computes the expectation of the
        measurements.

        Args:
            state: An array representing a valid  density matrix.
            qid_map: A map from all qubits used in this ProjectorSum to the indices of the qubits
                that `state_vector` is defined over.

        Returns:
            The expectation value of the input state.
        
cirq-core/cirq/ops/linear_combinations.py