def from_bitarray(msg: ndarrays_pb2.BitArray) -> np.ndarray:
    """Convert a BitArray proto to a numpy array of the same shape.

    Args:
        msg: Message to extract array from.

    Returns:
        Numpy array.
    """
    if not msg.shape:
        raise ValueError(f"Cannot convert unset/empty {type(msg)} message to a numpy array.")

    # Load into uint8 array.
    flat_bytes = np.frombuffer(msg.flat_bytes, dtype=np.uint8)
    # Now extract flattened array from bits.
    tot_bits = functools.reduce(lambda x, y: x * y, msg.shape)
    flat_bits = np.unpackbits(flat_bytes, count=tot_bits)
    return np.reshape(flat_bits, msg.shape).view(np.bool_)
Convert a BitArray proto to a numpy array of the same shape.

    Args:
        msg: Message to extract array from.

    Returns:
        Numpy array.
    
cirq-google/cirq_google/api/v2/ndarrays.py