def to_int16_array(
    array: np.ndarray, out: ndarrays_pb2.Int16Array | None = None
) -> ndarrays_pb2.Int16Array:
    """Populate a new or provided Int16Array message with given numpy array.

    Supports any int type taking <=4 bytes. If given e.g. an 8-bit or 16-bit
    integer, it will recast (sign extend) as a 16-bit integer and serialize.

    Note that this only accepts signed integers.

    Args:
        array: Array to add.
        out: Message to add to.
    """
    if out is None:
        out = ndarrays_pb2.Int16Array()
    endianness, dtype = _to_dtype(array.dtype, "i2")
    out.shape[:] = array.shape
    out.endianness = endianness
    out.flat_bytes = array.astype(dtype, copy=False).tobytes()
    return out
Populate a new or provided Int16Array message with given numpy array.

    Supports any int type taking <=4 bytes. If given e.g. an 8-bit or 16-bit
    integer, it will recast (sign extend) as a 16-bit integer and serialize.

    Note that this only accepts signed integers.

    Args:
        array: Array to add.
        out: Message to add to.
    
cirq-google/cirq_google/api/v2/ndarrays.py