Exemplo n.º 1
0
    def setUp(self):
        if 'CPLEX.Ising' not in local_pluggables('algorithm'):
            self.skipTest(
                'CPLEX.Ising algorithm not found - CPLEX not installed?')

        np.random.seed(8123179)
        self.w = maxcut.random_graph(4, edge_prob=0.5, weight_range=10)
        self.qubit_op, self.offset = maxcut.get_maxcut_qubitops(self.w)
        self.algo_input = get_input_instance('EnergyInput')
        self.algo_input.qubit_op = self.qubit_op
    def set_adj_matrix(self, adj_matrix):
        maxcut_op, self.maxcut_shift = maxcut.get_maxcut_qubitops(adj_matrix)
        # print("maxcut_op: ", maxcut_op, ", maxcut_shift: ", maxcut_shift)

        # TODO: Find different approach of calculating and retrieving diagonal
        maxcut_op._paulis_to_matrix()
        self.eigenvalues = maxcut_op._dia_matrix

        self.calc_expectation_value()
        self.draw_expectation_grid()
Exemplo n.º 3
0
    def test_qaoa(self, w, p, solutions):
        self.log.debug('Testing {}-step QAOA with MaxCut on graph\n{}'.format(
            p, w))
        np.random.seed(0)
        optimizer = get_optimizer_instance('COBYLA')
        qubitOp, offset = maxcut.get_maxcut_qubitops(w)
        qaoa = get_algorithm_instance('QAOA.Variational')
        qaoa.setup_quantum_backend(backend='statevector_simulator', shots=100)
        qaoa.init_args(qubitOp, 'matrix', p, optimizer)

        result = qaoa.run()
        x = maxcut.sample_most_likely(result['eigvecs'][0])
        graph_solution = maxcut.get_graph_solution(x)
        self.log.debug('energy:             {}'.format(result['energy']))
        self.log.debug('time:               {}'.format(result['eval_time']))
        self.log.debug('maxcut objective:   {}'.format(result['energy'] +
                                                       offset))
        self.log.debug('solution:           {}'.format(graph_solution))
        self.log.debug('solution objective: {}'.format(
            maxcut.maxcut_value(x, w)))
        self.assertIn(''.join([str(int(i)) for i in graph_solution]),
                      solutions)
Exemplo n.º 4
0
    def test_qaoa(self, w, p, solutions):
        self.log.debug('Testing {}-step QAOA with MaxCut on graph\n{}'.format(
            p, w))
        np.random.seed(0)

        backend = Aer.get_backend('statevector_simulator')
        optimizer = COBYLA()
        qubitOp, offset = maxcut.get_maxcut_qubitops(w)

        qaoa = QAOA(qubitOp, optimizer, p, operator_mode='matrix')
        quantum_instance = QuantumInstance(backend)

        result = qaoa.run(quantum_instance)
        x = maxcut.sample_most_likely(result['eigvecs'][0])
        graph_solution = maxcut.get_graph_solution(x)
        self.log.debug('energy:             {}'.format(result['energy']))
        self.log.debug('time:               {}'.format(result['eval_time']))
        self.log.debug('maxcut objective:   {}'.format(result['energy'] +
                                                       offset))
        self.log.debug('solution:           {}'.format(graph_solution))
        self.log.debug('solution objective: {}'.format(
            maxcut.maxcut_value(x, w)))
        self.assertIn(''.join([str(int(i)) for i in graph_solution]),
                      solutions)
Exemplo n.º 5
0
 def setUp(self):
     super().setUp()
     np.random.seed(8123179)
     self.w = maxcut.random_graph(4, edge_prob=0.5, weight_range=10)
     self.qubit_op, self.offset = maxcut.get_maxcut_qubitops(self.w)
     self.algo_input = EnergyInput(self.qubit_op)