def density_matrix(
    state: np.ndarray,
    qid_shape: tuple[int, ...] | None = None,
    *,  # Force keyword arguments
    copy: bool = False,
    validate: bool = True,
    dtype: DTypeLike | None = None,
    atol: float = 1e-7,
) -> QuantumState:
    """Create a QuantumState object from a density matrix.

    Args:
        state: The density matrix.
        qid_shape: The qid shape.
        copy: Whether to copy the density matrix.
        validate: Whether to check if the given data and qid shape
            represent a valid quantum state with the given dtype.
        dtype: The expected data type.
        atol: Absolute numerical tolerance to use for validation.

    Raises:
        ValueError: Invalid density matrix.
    """
    if state.ndim != 2 or state.shape[0] != state.shape[1]:
        raise ValueError(f'A density matrix must be a square matrix. Got shape {state.shape}.')
    dim, _ = state.shape
    if qid_shape is None:
        qid_shape = _infer_qid_shape_from_dimension(dim)
    return QuantumState(
        data=state.copy() if copy else state,
        qid_shape=qid_shape,
        dtype=dtype,
        validate=validate,
        atol=atol,
    )
Create a QuantumState object from a density matrix.

    Args:
        state: The density matrix.
        qid_shape: The qid shape.
        copy: Whether to copy the density matrix.
        validate: Whether to check if the given data and qid shape
            represent a valid quantum state with the given dtype.
        dtype: The expected data type.
        atol: Absolute numerical tolerance to use for validation.

    Raises:
        ValueError: Invalid density matrix.
    
cirq-core/cirq/qis/states.py