Exemplo n.º 1
0
    def init_params(self, params, algo_input):
        """
        Initialize via parameters dictionary and algorithm input instance

        Args:
            params (dict): parameters dictionary
            algo_input (EnergyInput): EnergyInput instance
        """
        if algo_input is None:
            raise AlgorithmError("EnergyInput instance is required.")

        operator = algo_input.qubit_op

        qaoa_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM)
        operator_mode = qaoa_params.get(QAOA.PROP_OPERATOR_MODE)
        p = qaoa_params.get(QAOA.PROP_P)
        initial_point = qaoa_params.get(QAOA.PROP_INIT_POINT)

        # Set up optimizer
        opt_params = params.get(QuantumAlgorithm.SECTION_KEY_OPTIMIZER)
        optimizer = get_optimizer_instance(opt_params['name'])
        optimizer.init_params(opt_params)

        if 'statevector' not in self._backend and operator_mode == 'matrix':
            logger.debug('Qasm simulation does not work on {} mode, changing \
                            the operator_mode to paulis'.format(operator_mode))
            operator_mode = 'paulis'

        self.init_args(operator,
                       operator_mode,
                       p,
                       optimizer,
                       opt_init_point=initial_point,
                       aux_operators=algo_input.aux_ops)
Exemplo n.º 2
0
    def init_params(self, params, algo_input):
        """
        Initialize via parameters dictionary and algorithm input instance

        Args:
            params (dict): parameters dictionary
            algo_input (EnergyInput): EnergyInput instance
        """
        if algo_input is None:
            raise AlgorithmError("EnergyInput instance is required.")

        operator = algo_input.qubit_op

        vqe_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM)
        operator_mode = vqe_params.get('operator_mode')
        initial_point = vqe_params.get('initial_point')

        # Set up initial state, we need to add computed num qubits to params
        init_state_params = params.get(
            QuantumAlgorithm.SECTION_KEY_INITIAL_STATE)
        init_state_params['num_qubits'] = operator.num_qubits
        init_state = get_initial_state_instance(init_state_params['name'])
        init_state.init_params(init_state_params)

        # Set up variational form, we need to add computed num qubits, and initial state to params
        var_form_params = params.get(QuantumAlgorithm.SECTION_KEY_VAR_FORM)
        var_form_params['num_qubits'] = operator.num_qubits
        var_form_params['initial_state'] = init_state
        var_form = get_variational_form_instance(var_form_params['name'])
        var_form.init_params(var_form_params)

        # Set up optimizer
        opt_params = params.get(QuantumAlgorithm.SECTION_KEY_OPTIMIZER)
        optimizer = get_optimizer_instance(opt_params['name'])
        optimizer.init_params(opt_params)

        if 'statevector' not in self._backend and operator_mode == 'matrix':
            logger.debug('Qasm simulation does not work on {} mode, changing \
                            the operator_mode to paulis'.format(operator_mode))
            operator_mode = 'paulis'

        self.init_args(operator,
                       operator_mode,
                       var_form,
                       optimizer,
                       opt_init_point=initial_point,
                       aux_operators=algo_input.aux_ops)
        logger.info(self.print_setting())
Exemplo n.º 3
0
    def init_params(self, params, algo_input):
        SVMQK_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM)

        circuit_depth = SVMQK_params.get('circuit_depth')
        print_info = SVMQK_params.get('print_info')

        # Set up optimizer
        opt_params = params.get(QuantumAlgorithm.SECTION_KEY_OPTIMIZER)
        optimizer = get_optimizer_instance(opt_params['name'])
        # hard-coded params if SPSA is used.
        if opt_params['name'] == 'SPSA' and opt_params['parameters'] is None:
            opt_params['parameters'] = np.asarray(
                [4.0, 0.1, 0.602, 0.101, 0.0])
        optimizer.init_params(opt_params)
        optimizer.set_options(save_steps=10)

        self.init_args(algo_input.training_dataset, algo_input.test_dataset,
                       algo_input.datapoints, optimizer, circuit_depth,
                       print_info)
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))

        optimizer = get_optimizer_instance('COBYLA')
        qubitOp, offset = maxcut.get_maxcut_qubitops(w)
        qaoa = get_algorithm_instance('QAOA')
        qaoa.init_args(qubitOp, 'matrix', p, optimizer)
        qaoa.setup_quantum_backend(backend='local_statevector_simulator',
                                   shots=100)

        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.º 5
0
 def test_nlopt(self, name):
     optimizer = get_optimizer_instance(name)
     optimizer.set_options(**{'max_evals': 50000})
     optimizer.init_args()
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 50000)
Exemplo n.º 6
0
 def test_tnc(self):
     optimizer = get_optimizer_instance('TNC')
     optimizer.set_options(**{'maxiter': 1000})
     optimizer.init_args(tol=1e-06)
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 10000)
Exemplo n.º 7
0
 def test_spsa(self):
     optimizer = get_optimizer_instance('SPSA')
     optimizer.set_options(**{})
     optimizer.init_args(max_trials=10000)
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 100000)
Exemplo n.º 8
0
 def test_powell(self):
     optimizer = get_optimizer_instance('POWELL')
     optimizer.set_options(**{'maxfev': 10000})
     optimizer.init_args(tol=1e-06)
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 10000)
Exemplo n.º 9
0
 def test_nelder_mead(self):
     optimizer = get_optimizer_instance('NELDER_MEAD')
     optimizer.set_options(**{'maxfev': 10000})
     optimizer.init_args(tol=1e-06)
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 10000)
Exemplo n.º 10
0
 def test_l_bfgs_b(self):
     optimizer = get_optimizer_instance('L_BFGS_B')
     optimizer.set_options(**{'maxfun': 1000})
     optimizer.init_args()
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 10000)