condensedAI / KwantRL

0 stars 1 forks source link

compare staircasiness metric #2

Open Ulfgard opened 3 years ago

Ulfgard commented 3 years ago

We still might need to put some work into finding a good metric and do a proper comparison.

From Teams:

  1. assuming all measured points have the same distance, [Ulfgards] metric should be something like:
    def deriv_metric(staircase):
        res = 0
        for i in range(len(staircase) - 1):
            res += np.sqrt(np.abs(staircase[i+1] - staircase[i]))
        res /= np.sqrt(np.max(staircase) - np.min(staircase))
        return res
​
  1. The histogram metric is very difficult to optimize, because the only information you have is when points are moved in different bins. Thus small changes that keep points in a bin will leave the metric unaffected. This means that the objective function is just a set of plateaus with no gradient information in-between (or the gradient is basically the penalty function) and you can't use it to fine-tune
Ulfgard commented 3 years ago

In the meanrtime, i have run the comparisons using the following code in run.py:

def func_to_minimize(x):
    global fevals
    # new_point ensures points are valid within bounds and constraints.
    x_projected,penalty=new_point(x,bounds=bounds)

    result=[]
    for avg_gates in common_voltages:
        QPC.set_all_pixels(x_projected+avg_gates)
        result.append(QPC.transmission())

    def deriv_metric(staircase):
        res = 0
        for i in range(len(staircase) - 1):
            res += np.sqrt(np.abs(staircase[i+1] - staircase[i]))
        res /= np.sqrt(np.max(staircase) - np.min(staircase)) 
        return res

    metric = deriv_metric(result)
    # metric = stairs.histogram(result)
    return pfactor*penalty+metric
Ulfgard commented 3 years ago

comparison. old result: test100 (1)

new result: test100

everthemore commented 3 years ago

That looks good to me, seems smoother with flatter plateaus indeed. Just a quick point: we want the plateaus to be at integer values (though that does not guarantee we are seeing QPC physics).

Ulfgard commented 3 years ago

Is it physically possible to have the plateaus on non-integer values? Or is this an issue in the simulation? We could try to inverse x/y axis and put more weight near the integer positions to make this happen.

Ulfgard commented 3 years ago

i have tried putting more emphasis on the dervatives around y=1,2,3... it changed it a little bit, but not so much. I did it by adding weight terms depending on the y-values:

def weighted_deriv_metric(staircase):
        res = 0
        sumw = 0
        for i in range(len(staircase) - 1):
            y= (staircase[i+1] + staircase[i])/2
            wi = 1+np.cos(2*np.pi*y) 
            sumw +=wi
            res += wi* np.sqrt(np.abs(staircase[i+1] - staircase[i]))
        res /= np.sqrt(np.max(staircase) - np.min(staircase)) * sumw
        return res

test100

Ulfgard commented 3 years ago

Finally, i tried some approach which is similar to what you tried. I am mapping the y-values to the 0-1 interval by subtracrting the next integer (rounding down) from it. then the optimal stair should be the solution that maximizes the variance of the mapped values (producing either value s0 or 1 which would be the values of the nearest plateaus on both sides)

    def var_metric(staircase):
        data=np.fmod(staircase,1)
        var=np.var(data)
        return 1/var

result is pretty similar to what you have gotten: test100