Example #1
0
    def test_validation(self):
        """ Validation Test """
        num_var = 3
        # validate an object type of the input.
        with self.assertRaises(AquaError):
            docplex._validate_input_model("Model")

        # validate the types of the variables are binary or not
        with self.assertRaises(AquaError):
            mdl = Model(name='Error_integer_variables')
            x = {
                i: mdl.integer_var(name='x_{0}'.format(i))
                for i in range(num_var)
            }
            obj_func = mdl.sum(x[i] for i in range(num_var))
            mdl.maximize(obj_func)
            docplex.get_qubit_op(mdl)

        # validate types of constraints are equality constraints or not.
        with self.assertRaises(AquaError):
            mdl = Model(name='Error_inequality_constraints')
            x = {
                i: mdl.binary_var(name='x_{0}'.format(i))
                for i in range(num_var)
            }
            obj_func = mdl.sum(x[i] for i in range(num_var))
            mdl.maximize(obj_func)
            mdl.add_constraint(mdl.sum(x[i] for i in range(num_var)) <= 1)
            docplex.get_qubit_op(mdl)
Example #2
0
    def test_docplex_constant_and_quadratic_terms_in_object_function(self):
        """ Docplex Constant and Quadratic terms in Object function test """
        # Create an Ising Hamiltonian with docplex
        laplacian = np.array([[-3., 1., 1., 1.],
                              [1., -2., 1., -0.],
                              [1., 1., -3., 1.],
                              [1., -0., 1., -2.]])

        mdl = Model()
        n = laplacian.shape[0]
        bias = [0] * 4
        x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(n)}
        couplers_func = mdl.sum(
            2 * laplacian[i, j] * (2 * x[i] - 1) * (2 * x[j] - 1)
            for i in range(n - 1) for j in range(i, n))
        bias_func = mdl.sum(float(bias[i]) * x[i] for i in range(n))
        ising_func = couplers_func + bias_func
        mdl.minimize(ising_func)
        qubit_op, offset = docplex.get_qubit_op(mdl)

        e_e = ExactEigensolver(qubit_op, k=1)
        result = e_e.run()

        expected_result = -22

        # Compare objective
        self.assertEqual(result['energy'] + offset, expected_result)
Example #3
0
    def test_docplex_maxcut(self):
        """ Docplex maxcut test """
        # Generating a graph of 4 nodes
        n = 4
        graph = nx.Graph()
        graph.add_nodes_from(np.arange(0, n, 1))
        elist = [(0, 1, 1.0), (0, 2, 1.0), (0, 3, 1.0), (1, 2, 1.0), (2, 3, 1.0)]
        graph.add_weighted_edges_from(elist)
        # Computing the weight matrix from the random graph
        w = np.zeros([n, n])
        for i in range(n):
            for j in range(n):
                temp = graph.get_edge_data(i, j, default=0)
                if temp != 0:
                    w[i, j] = temp['weight']

        # Create an Ising Hamiltonian with docplex.
        mdl = Model(name='max_cut')
        mdl.node_vars = mdl.binary_var_list(list(range(4)), name='node')
        maxcut_func = mdl.sum(w[i, j] * mdl.node_vars[i] * (1 - mdl.node_vars[j])
                              for i in range(n) for j in range(n))
        mdl.maximize(maxcut_func)
        qubit_op, offset = docplex.get_qubit_op(mdl)

        e_e = ExactEigensolver(qubit_op, k=1)
        result = e_e.run()

        ee_expected = ExactEigensolver(QUBIT_OP_MAXCUT, k=1)
        expected_result = ee_expected.run()

        # Compare objective
        self.assertEqual(result['energy'] + offset, expected_result['energy'] + OFFSET_MAXCUT)
Example #4
0
    def test_docplex_integer_constraints(self):
        """ Docplex Integer Constraints test """
        # Create an Ising Hamiltonian with docplex
        mdl = Model(name='integer_constraints')
        x = {i: mdl.binary_var(name='x_{0}'.format(i)) for i in range(1, 5)}
        max_vars_func = mdl.sum(x[i] for i in range(1, 5))
        mdl.maximize(max_vars_func)
        mdl.add_constraint(mdl.sum(i * x[i] for i in range(1, 5)) == 3)
        qubit_op, offset = docplex.get_qubit_op(mdl)

        e_e = ExactEigensolver(qubit_op, k=1)
        result = e_e.run()

        expected_result = -2

        # Compare objective
        self.assertEqual(result['energy'] + offset, expected_result)