def return_fidelities(chain_size, couplings_0, consts): """ Final fidelity calculator. Should build the chain, calculate the fidelity over the chain and find the optimisation. Works only for chains of size > 3 """ initial_state = create_initial_final_states(chain_size, True) final_state = create_initial_final_states(chain_size, False) final_state = final_state.transpose() Spin_Operator_List = Spin_List_Creator(chain_size) res = minimize_pso(symmetric_chain_cost_function, couplings_0, args=(chain_size, Spin_Operator_List, initial_state, final_state), constraints=consts, options={ 'stable_iter': 20, 'max_velocity': 1, 'verbose': True }) print( f"Best final fidelity: {1 - symmetric_chain_cost_function(res.x, chain_size, Spin_Operator_List, initial_state, final_state, XY_HAM = False)}" ) print(f"Final Couplings: {res.x}")
def test_unconstrained(self): """Test against the Rosenbrock function.""" x0 = np.random.uniform(0, 2, (1000, 5)) sol = np.array([1., 1., 1., 1., 1.]) res = minimize_pso(rosen, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_rosendisc(self): """Test against the Rosenbrock function constrained to a disk.""" cons = ({'type': 'ineq', 'fun': lambda x: -x[0]**2 - x[1]**2 + 2},) x0 = init_feasible(cons, low=-1.5, high=1.5, shape=(1000, 2)) sol = np.array([1., 1.]) def rosen(x): return (1 - x[0])**2 + 100*(x[1] - x[0]**2)**2 res = minimize_pso(rosen, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_ackley(self): """Test against the Ackley function.""" x0 = np.random.uniform(-5, 5, (1000, 2)) sol = np.array([0., 0.]) def ackley(x): return -20 * np.exp(-.2 * np.sqrt(0.5 * (x[0]**2 + x[1]**2))) - \ np.exp(.5 * (np.cos(2*np.pi*x[0]) + np.cos(2*np.pi*x[1]))) + \ np.e + 20 res = minimize_pso(ackley, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_levi(self): """Test against the Levi function.""" x0 = np.random.uniform(-10, 10, (1000, 2)) sol = np.array([1., 1.]) def levi(x): sin3x = np.sin(3 * np.pi * x[0])**2 sin2y = np.sin(2 * np.pi * x[1])**2 return sin3x + (x[0] - 1)**2 * (1 + sin3x) + \ (x[1] - 1)**2 * (1 + sin2y) res = minimize_pso(levi, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_constrained(self): """Test against the following function:: y = (x0 - 1)^2 + (x1 - 2.5)^2 under the constraints:: x0 - 2.x1 + 2 >= 0 -x0 - 2.x1 + 6 >= 0 -x0 + 2.x1 + 2 >= 0 x0, x1 >= 0 """ cons = ({ 'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2 }, { 'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6 }, { 'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2 }, { 'type': 'ineq', 'fun': lambda x: x[0] }, { 'type': 'ineq', 'fun': lambda x: x[1] }) x0 = init_feasible(cons, low=0, high=2, shape=(1000, 2)) options = { 'g_rate': 1., 'l_rate': 1., 'max_velocity': 4., 'stable_iter': 50 } sol = np.array([1.4, 1.7]) res = minimize_pso(lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2, x0, constraints=cons, options=options) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def test_mishra(self): """Test against the Mishra Bird function.""" cons = ( {'type': 'ineq', 'fun': lambda x: 25 - np.sum((x + 5) ** 2)},) x0 = init_feasible(cons, low=-10, high=0, shape=(1000, 2)) sol = np.array([-3.130, -1.582]) def mishra(x): cos = np.cos(x[0]) sin = np.sin(x[1]) return sin*np.e**((1 - cos)**2) + cos*np.e**((1 - sin)**2) + \ (x[0] - x[1])**2 res = minimize_pso(mishra, x0) converged = res.success assert converged, res.message np.testing.assert_array_almost_equal(sol, res.x, 3)
def return_fidelities2(chain_size, initial_state, final_state, couplings_initial, consts): """ # Final fidelity calculator. Should build the chain, calculate the fidelity over the chain and find the # optimisation. # Works only for chains of size > 3 """ res = minimize_pso(symmetric_chain_cost_function2, couplings_initial, args=(chain_size, initial_state, final_state), constraints=None, options={ 'stable_iter': 5, 'max_velocity': 1, 'verbose': True }) print( f"Best final fidelity: {1 - symmetric_chain_cost_function2(res.x, chain_size, initial_state, final_state)}" ) print(f"Final Couplings: {res.x}") return res