Example #1
0
def test_param_shift_2q(pl_op, braket_gate, angle, observable):
    """Tests that the parameter-shift rules of custom operations yield the correct derivatives."""
    op = pl_op(angle, wires=[0, 1])
    if op.grad_recipe[0]:
        shifts = op.grad_recipe[0]
    else:
        orig_shifts = qml.gradients.generate_shift_rule(
            op.parameter_frequencies[0])
        shifts = [[c, 1, s] for c, s in orig_shifts]

    summands = []
    for shift in shifts:
        shifted = pl_op.compute_matrix(angle + shift[2])
        summands.append(shift[0] * np.matmul(
            np.matmul(np.transpose(np.conj(shifted)), observable), shifted))

    from_shifts = sum(summands)

    def conj_obs_gate(angle):
        mat = pl_op.compute_matrix(angle)
        return anp.matmul(anp.matmul(anp.transpose(anp.conj(mat)), observable),
                          mat)

    direct_calculation = deriv(conj_obs_gate)(angle)

    assert np.allclose(from_shifts, direct_calculation)
Example #2
0
 def inner(ws):
     result = []
     for li in range(len(ws)):
         ll = ws[li]
         result.append([[
             deriv(lambda x: f(
                 replace_ith(
                     ws, li,
                     replace_ith(ws[li], ui, replace_ith(ws[li][ui], wi, x))
                 )))(ws[li][ui][wi]) for wi in range(len(ll[ui]))
         ] for ui in range(len(ll))])
     return result
Example #3
0
def test_van_genuchten(theta):

    params = {
        'Ks': 42,
        'alpha': 0.0123,
        'l': 0.27,
        'm': 0.3142,
        'theta_range': (0.123, 0.654)
    }

    D = fronts.D.van_genuchten(**params)

    assert np.all(D(theta, 1)[0] == D(theta))
    assert np.all(D(theta, 2)[0] == D(theta))
    assert np.all(D(theta, 1)[1] == D(theta, 2)[1])

    D_ref = functools.partial(van_genuchten_D, **params)

    assert D(theta) == pytest.approx(D_ref(theta))
    assert D(theta, 1)[1] == pytest.approx(deriv(D_ref)(theta))
    assert D(theta, 2)[2] == pytest.approx(deriv(deriv(D_ref))(theta),
                                           rel=1e-5)
Example #4
0
def mrf_ir_fisp_efficient_crb_forward_differentiation(M0, FAs, TEs, TRs,
                                                      inversion_delay, T1, T2):

    deriv_fn_T1 = deriv(mrf_ir_fisp_real,
                        get_arg_index(mrf_ir_fisp_real, 'T1'))
    deriv_fn_T2 = deriv(mrf_ir_fisp_real,
                        get_arg_index(mrf_ir_fisp_real, 'T2'))
    #deriv_fn_M0 = deriv(mrf_ir_fisp_real, get_arg_index(mrf_ir_fisp_real, 'M0'))

    m_echos = mrf_ir_fisp_real(M0, FAs, TEs, TRs, inversion_delay, T1, T2)

    fim_T1 = np.transpose(
        deriv_fn_T1(M0, FAs, TEs, TRs, inversion_delay, T1, T2))
    fim_T2 = np.transpose(
        deriv_fn_T2(M0, FAs, TEs, TRs, inversion_delay, T1, T2))
    #fim_M0 = np.transpose(deriv_fn_M0(M0, FAs, TEs, TRs, inversion_delay, T1, T2))

    # if M0 == 1.0 (which should always be done - you can just weight the W crlb differently)
    # then this is true so we can save a bit of computation, for N_TR = 1000, 10 EPG states, this lets us go from
    # 22 -> 18.5 +/- 0.845 seconds for calculation of grad (using %%timeit)
    fim_M0 = np.transpose(m_echos)

    return m_echos, fim_M0, fim_T1, fim_T2
Example #5
0
def test_deriv():
    def fun(x):
        return (x + np.sin(x**2)) * x

    assert 3.190948746871 - 1e-6 < deriv(fun)(1.3) < 3.190948746871 + 1e-6
Example #6
0
def test_no_jvp_def():
    fun = primitive(lambda x: 2. * x)
    with pytest.raises(NotImplementedError):
        deriv(fun)(1.)
 def inner(x):
     return [
         deriv(lambda xi: f(replace_ith(x, i, xi)))(x[i])
         for i in range(len(x))
     ]
Example #8
0
def test_no_jvp_def():
    fun = primitive(lambda x: 2. * x)
    deriv(fun)(1.)
Example #9
0
def test_array_creation_fwd():
    def fun(x, N):
        arr = [x for i in range(N)]
        return np.sum(np.array(arr))

    assert_linear_time(lambda N: deriv(fun)(1.0, 400 * N))
Example #10
0
def test_list_creation():
    def fun(x, N):
        return make_list(*[x for _ in range(N)])

    assert_linear_time(lambda N: deriv(fun)(0.0, 20 * N))
Example #11
0
def test_deriv():
    def fun(x): return (x + np.sin(x**2)) * x
    assert 3.190948746871 - 1e-6 < deriv(fun)(1.3) < 3.190948746871 + 1e-6
Example #12
0
def test_array_creation_fwd():
    def fun(x, N):
        arr = [x for i in range(N)]
        return np.sum(np.array(arr))

    assert_linear_time(lambda N: deriv(fun)(1.0, 400*N))
Example #13
0
def test_list_creation():
    def fun(x, N):
        return make_list(*[x for _ in range(N)])
    assert_linear_time(lambda N: deriv(fun)(0.0, 20*N))
Example #14
0
def test_no_jvp_def():
    fun = primitive(lambda x: 2. * x)
    deriv(fun)(1.)