def test_to_quadratic_program(self): """Test to_quadratic_program""" maxcut = Maxcut(self.graph) op = maxcut.to_quadratic_program() # Test name self.assertEqual(op.name, "Max-cut") # Test variables self.assertEqual(op.get_num_vars(), 4) for var in op.variables: self.assertEqual(var.vartype, VarType.BINARY) # Test objective obj = op.objective self.assertEqual(obj.sense, QuadraticObjective.Sense.MAXIMIZE) self.assertEqual(obj.constant, 0) self.assertDictEqual(obj.linear.to_dict(), { 0: 3.0, 1: 3.0, 2: 3.0, 3: 3.0 }) self.assertDictEqual( obj.quadratic.to_dict(), { (0, 1): -2.0, (0, 2): -2.0, (1, 2): -2.0, (0, 3): -2.0, (1, 3): -2.0, (2, 3): -2.0 }) # Test constraint lin = op.linear_constraints self.assertEqual(len(lin), 0)
def test_draw_without_maxplotlin(self): """Test whether draw raises an error if matplotlib is not installed""" maxcut = Maxcut(self.graph) from qiskit.exceptions import MissingOptionalLibraryError with self.assertRaises(MissingOptionalLibraryError): maxcut.draw()
def test_max_cut(self): """Basic test on the max cut problem.""" graph = np.array([ [0.0, 1.0, 2.0, 0.0], [1.0, 0.0, 1.0, 0.0], [2.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], ]) presolver = GoemansWilliamsonOptimizer(num_cuts=10) problem = Maxcut(graph).to_quadratic_program() backend = BasicAer.get_backend("statevector_simulator") qaoa = QAOA(quantum_instance=backend, reps=1) aggregator = MeanAggregator() optimizer = WarmStartQAOAOptimizer( pre_solver=presolver, relax_for_pre_solver=False, qaoa=qaoa, epsilon=0.25, num_initial_solutions=10, aggregator=aggregator, converters=[], ) result_warm = optimizer.solve(problem) self.assertIsNotNone(result_warm) self.assertIsNotNone(result_warm.x) np.testing.assert_almost_equal([0, 0, 1, 0], result_warm.x, 3) self.assertIsNotNone(result_warm.fval) np.testing.assert_almost_equal(4, result_warm.fval, 3)
def test_all_cuts(self): """Basic test of the Goemans-Williamson optimizer.""" try: graph = np.array([[0., 1., 2., 0.], [1., 0., 1., 0.], [2., 1., 0., 1.], [0., 0., 1., 0.]]) optimizer = GoemansWilliamsonOptimizer(num_cuts=10, seed=0) problem = Maxcut(graph).to_quadratic_program() self.assertIsNotNone(problem) results = optimizer.solve(problem) self.assertIsNotNone(results) self.assertIsInstance(results, GoemansWilliamsonOptimizationResult) self.assertIsNotNone(results.x) np.testing.assert_almost_equal([0, 1, 1, 0], results.x, 3) self.assertIsNotNone(results.fval) np.testing.assert_almost_equal(4, results.fval, 3) self.assertIsNotNone(results.samples) self.assertEqual(3, len(results.samples)) except MissingOptionalLibraryError as ex: self.skipTest(str(ex))
def test_all_cuts(self): """Basic test of the Goemans-Williamson optimizer.""" graph = np.array([ [0.0, 1.0, 2.0, 0.0], [1.0, 0.0, 1.0, 0.0], [2.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], ]) optimizer = GoemansWilliamsonOptimizer(num_cuts=10, seed=0) problem = Maxcut(graph).to_quadratic_program() self.assertIsNotNone(problem) results = optimizer.solve(problem) self.assertIsNotNone(results) self.assertIsInstance(results, GoemansWilliamsonOptimizationResult) self.assertIsNotNone(results.x) np.testing.assert_almost_equal([0, 1, 1, 0], results.x, 3) self.assertIsNotNone(results.fval) np.testing.assert_almost_equal(4, results.fval, 3) self.assertIsNotNone(results.samples) self.assertEqual(3, len(results.samples))
def test_minimization_problem(self): """Tests the optimizer with a minimization problem""" optimizer = GoemansWilliamsonOptimizer(num_cuts=10, seed=0) problem = Maxcut(self.graph).to_quadratic_program() # artificially convert to minimization max2min = MaximizeToMinimize() problem = max2min.convert(problem) results = optimizer.solve(problem) np.testing.assert_almost_equal([0, 1, 1, 0], results.x, 3) np.testing.assert_almost_equal(4, results.fval, 3)
def test_draw(self): """Test whether draw raises an error if matplotlib is not installed""" maxcut = Maxcut(self.graph) try: import matplotlib as _ maxcut.draw() except ImportError: with self.assertRaises(MissingOptionalLibraryError): maxcut.draw()
def test_node_color(self): """Test _node_color""" maxcut = Maxcut(self.graph) self.assertEqual(maxcut._node_color(self.result), ['b', 'b', 'r', 'r'])
def test_interpret(self): """Test interpret""" maxcut = Maxcut(self.graph) self.assertEqual(maxcut.interpret(self.result), [[2, 3], [0, 1]])
def test_node_color(self): """Test _node_color""" maxcut = Maxcut(self.graph) self.assertEqual(maxcut._node_color(self.result), ["b", "b", "r", "r"])