def test_parameters_are_properly_initialized_for_each_layer( self, min_layer, max_layer, n_layers_per_iteration, ansatz): def parameters_initializer(number_of_params, old_params): return np.random.uniform(-np.pi, np.pi, number_of_params) parameters_initializer = mock.Mock(wraps=parameters_initializer) optimizer = LayerwiseAnsatzOptimizer( ansatz=ansatz, inner_optimizer=ScipyOptimizer("L-BFGS-B"), min_layer=min_layer, max_layer=max_layer, n_layers_per_iteration=n_layers_per_iteration, parameters_initializer=parameters_initializer, ) _ = optimizer.minimize(cost_function_factory, initial_params=np.ones(min_layer)) assert (parameters_initializer.call_count == (max_layer - min_layer) // n_layers_per_iteration) for ((args, _kwrgs), i) in zip( parameters_initializer.call_args_list, range(min_layer + n_layers_per_iteration, max_layer, n_layers_per_iteration), ): number_of_params, old_params = args assert number_of_params == i
def test_if_satisfies_contracts(self, contract, ansatz, initial_params): optimizer = LayerwiseAnsatzOptimizer( ansatz=ansatz, inner_optimizer=ScipyOptimizer("L-BFGS-B"), min_layer=1, max_layer=3, ) assert contract(optimizer, cost_function_factory, initial_params)
def test_fails_for_invalid_min_max_layer(self, min_layer, max_layer, ansatz): with pytest.raises(AssertionError): LayerwiseAnsatzOptimizer( ansatz=ansatz, inner_optimizer=ScipyOptimizer("L-BFGS-B"), min_layer=min_layer, max_layer=max_layer, )
def test_SLSQP_with_inequality_constraints(self, rosenbrock_function): # Given cost_function = FunctionWithGradient( rosenbrock_function, finite_differences_gradient(rosenbrock_function)) constraints = {"type": "ineq", "fun": lambda x: x[0] + x[1] - 3} optimizer = ScipyOptimizer(method="SLSQP") initial_params = np.array([0, 0]) # When results_without_constraints = optimizer.minimize( cost_function, initial_params=initial_params) optimizer.constraints = constraints results_with_constraints = optimizer.minimize( cost_function, initial_params=initial_params) # Then assert results_without_constraints.opt_value == pytest.approx( results_with_constraints.opt_value, abs=1e-1) assert results_with_constraints.opt_params.sum() >= 3
def test_SLSQP_with_equality_constraints(self, sum_x_squared, rosenbrock_function): # Given cost_function = FunctionWithGradient( rosenbrock_function, finite_differences_gradient(rosenbrock_function)) constraint_cost_function = sum_x_squared constraints = ({"type": "eq", "fun": constraint_cost_function}, ) optimizer = ScipyOptimizer(method="SLSQP", constraints=constraints) initial_params = np.array([1, 1]) target_params = np.array([0, 0]) target_value = 1 # When results = optimizer.minimize(cost_function, initial_params=initial_params) # Then assert results.opt_value == pytest.approx(target_value, abs=1e-3) assert results.opt_params == pytest.approx(target_params, abs=1e-3)
def test_dimension_of_solution_increases(self, max_layer, ansatz): min_layer = 1 optimizer = LayerwiseAnsatzOptimizer( ansatz=ansatz, inner_optimizer=ScipyOptimizer("L-BFGS-B"), min_layer=min_layer, max_layer=max_layer, ) opt_results = optimizer.minimize(cost_function_factory, initial_params=np.ones(min_layer), keep_history=True) assert len(opt_results.opt_params) == max_layer
def test_ansatz_is_not_modified_outside_of_minimize( self, ansatz, initial_params): initial_number_of_layers = ansatz.number_of_layers optimizer = LayerwiseAnsatzOptimizer( ansatz=ansatz, inner_optimizer=ScipyOptimizer("L-BFGS-B"), min_layer=1, max_layer=3, ) _ = optimizer.minimize(cost_function_factory, initial_params=initial_params) assert ansatz.number_of_layers == initial_number_of_layers
def optimizer(request): return ScipyOptimizer(**request.param)