def test_ctm_adjoint(): scen, control = small_scenario() state = CTMSimulator().simulate(scen, control) hx = CTMAdjoint(scen).hx(state, control) hu = CTMAdjoint(scen).hu(state, control) assert_array_equal(hx, np.tril(hx)) u = np.ones(control.flatten().shape) * 0.01 adjoint = CTMAdjoint(scen) assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u), 3)
def test_fs_controlled(): beta_op.beta = 1.0 scen, control = small_scenario() ca = CTMAdjoint(scen) for _ in range(5): c = rand(scen.N * scen.T, 1).flatten() assert_array_almost_equal(ca.grad(c), ca.grad_fd(c)) # fs = CTMSimulator().simulate(scen, control) ca = ControlledCtmAdjoint(True, scen) control = ca.construct_control(ca.no_control()) fs = ca.fs(control) # assert_array_equal(fs.density, [[.9, 1.0, .9, .1, 0, 0, 0, 0, 0, 0, 0], [0, 1.0, .9, .9, .1, 0, 0, 0, 0, 0, 0]]) assert_array_almost_equal(fs.density, [[0.9, 1.0, 0.9, 0.1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) control[1][:] += 2.0 fs = ca.fs(control) assert_array_almost_equal( fs.density, np.array([[0.9, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0]]) )
def test_simplest(): scen, control = smallest_possible() u = control.flatten() adjoint = CTMAdjoint(scen) assert_array_almost_equal(adjoint.grad(u), adjoint.grad_fd(u))