def xor_polygons_phidl(A, B, hash_geom=True):
    """ Given two devices A and B, performs a layer-by-layer XOR diff between
    A and B, and returns polygons representing the differences between A and B.
    """
    from phidl import Device
    import gdspy
    if hash_geom and A.hash_geometry() == B.hash_geometry():
        return Device()
    D = Device()
    A_polys = A.get_polygons(by_spec=True)
    B_polys = B.get_polygons(by_spec=True)
    A_layers = A_polys.keys()
    B_layers = B_polys.keys()
    all_layers = set()
    all_layers.update(A_layers)
    all_layers.update(B_layers)
    for layer in all_layers:
        if layer in A_layers and layer in B_layers:
            p = gdspy.fast_boolean(A_polys[layer], B_polys[layer],
                operation='xor', precision=0.001, max_points=4000, layer=
                layer[0], datatype=layer[1])
        elif layer in A_layers:
            p = A_polys[layer]
        elif layer in B_layers:
            p = B_polys[layer]
        if p is not None:
            D.add_polygon(p, layer=layer)
    return D
