def pack_bits(bits: np.ndarray) -> bytes:
    """Pack bits given as a numpy array of bools into bytes."""
    # Pad length to multiple of 8 if needed.
    pad = -len(bits) % 8
    if pad:
        bits = np.pad(bits, (0, pad), 'constant')

    # Pack in little-endian bit order.
    bits = bits.reshape((-1, 8))[:, ::-1]
    byte_arr = np.packbits(bits, axis=1).reshape(-1)

    return byte_arr.tobytes()
Pack bits given as a numpy array of bools into bytes.
cirq-google/cirq_google/api/v2/results.py