def get_pluggable_section_names(self, section_name):
        from qiskit_aqua.parser import JSONSchema
        from qiskit_aqua import local_pluggables
        from qiskit_aqua_chemistry.parser import InputParser
        if not Model.is_pluggable_section(section_name):
            return []

        if JSONSchema.ALGORITHM == section_name:
            problem_name = None
            if self._parser is not None:
                problem_name = self.get_section_property(
                    JSONSchema.PROBLEM, JSONSchema.NAME)
            if problem_name is None:
                problem_name = self.get_property_default_value(
                    JSONSchema.PROBLEM, JSONSchema.NAME)

            if problem_name is None:
                return local_pluggables(JSONSchema.ALGORITHM)

            algo_names = []
            for algo_name in local_pluggables(JSONSchema.ALGORITHM):
                problems = InputParser.get_algorithm_problems(algo_name)
                if problem_name in problems:
                    algo_names.append(algo_name)

            return algo_names

        return local_pluggables(section_name)
示例#2
0
文件: _model.py 项目: takehuge/aqua
    def get_pluggable_section_names(self, section_name):
        if not Model.is_pluggable_section(section_name):
            return []

        if InputParser.ALGORITHM == section_name:
            problem_name = None
            if self._parser is not None:
                problem_name = self.get_section_property(
                    InputParser.PROBLEM, InputParser.NAME)
            if problem_name is None:
                problem_name = self.get_property_default_value(
                    InputParser.PROBLEM, InputParser.NAME)

            if problem_name is None:
                return local_pluggables(InputParser.ALGORITHM)

            algo_names = []
            for algo_name in local_pluggables(InputParser.ALGORITHM):
                problems = InputParser.get_algorithm_problems(algo_name)
                if problem_name in problems:
                    algo_names.append(algo_name)

            return algo_names

        return local_pluggables(section_name)
示例#3
0
    def _update_input_problem(self):
        problem_name = self.get_section_property(JSONSchema.PROBLEM,
                                                 JSONSchema.NAME)
        if problem_name is None:
            problem_name = self.get_property_default_value(
                JSONSchema.PROBLEM, JSONSchema.NAME)

        if problem_name is None:
            raise AquaError("No algorithm 'problem' section found on input.")

        input_name = self.get_section_property(PluggableType.INPUT.value,
                                               JSONSchema.NAME)
        if input_name is not None and problem_name in InputParser.get_input_problems(
                input_name):
            return

        for input_name in local_pluggables(PluggableType.INPUT):
            if problem_name in self.get_input_problems(input_name):
                # set to the first input to solve the problem
                self.set_section_property(PluggableType.INPUT.value,
                                          JSONSchema.NAME, input_name)
                return

        # no input solve this problem, remove section
        self.delete_section(PluggableType.INPUT.value)
示例#4
0
文件: _model.py 项目: simhan/aqua
    def _load_data(self):
        if self._data_loaded:
            return

        from qiskit_aqua import (local_pluggables_types, local_pluggables,
                                 get_pluggable_configuration)
        from qiskit_aqua.input import (local_inputs, get_input_configuration)

        self._property_titles = OrderedDict()
        self._sections = OrderedDict()
        self._sections[Model._INPUT_NAME] = OrderedDict()
        self._property_titles[Model._INPUT_NAME] = OrderedDict()
        for input_name in local_inputs():
            config = copy.deepcopy(get_input_configuration(input_name))
            self._populate_section(Model._INPUT_NAME, input_name, config)

        for pluggable_type in local_pluggables_types():
            self._sections[pluggable_type] = OrderedDict()
            self._property_titles[pluggable_type] = OrderedDict()
            for pluggable_name in local_pluggables(pluggable_type):
                config = copy.deepcopy(
                    get_pluggable_configuration(pluggable_type,
                                                pluggable_name))
                self._populate_section(pluggable_type, pluggable_name, config)

        self._data_loaded = True
示例#5
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
示例#6
0
    def populate_problem_names(self):
        """Populate enum list of problem names"""
        problems_dict = OrderedDict()
        for algo_name in local_pluggables(PluggableType.ALGORITHM):
            problems = JSONSchema.get_algorithm_problems(algo_name)
            for problem in problems:
                problems_dict[problem] = None

        problems_enum = {'enum': list(problems_dict.keys())}
        self._schema['properties'][JSONSchema.PROBLEM]['properties'][JSONSchema.NAME]['oneOf'] = [
            problems_enum]
示例#7
0
    def get_input_section_names(self):
        from qiskit_aqua.parser._inputparser import InputParser
        from qiskit_aqua import local_pluggables, PluggableType
        from qiskit_aqua.parser import JSONSchema
        problem_name = None
        if self._parser is not None:
            problem_name = self.get_section_property(JSONSchema.PROBLEM, JSONSchema.NAME)
        if problem_name is None:
            problem_name = self.get_property_default_value(JSONSchema.PROBLEM, JSONSchema.NAME)

        if problem_name is None:
            return local_pluggables(PluggableType.INPUT)

        input_names = []
        for input_name in local_pluggables(PluggableType.INPUT):
            problems = InputParser.get_input_problems(input_name)
            if problem_name in problems:
                input_names.append(input_name)

        return input_names
示例#8
0
    def _load_data(self):
        if self._data_loaded:
            return

        from qiskit_aqua import (local_pluggables_types,
                                 local_pluggables,
                                 get_pluggable_configuration)

        self._property_titles = OrderedDict()
        self._sections = OrderedDict()
        for pluggable_type in local_pluggables_types():
            self._sections[pluggable_type.value] = OrderedDict()
            self._property_titles[pluggable_type.value] = OrderedDict()
            for pluggable_name in local_pluggables(pluggable_type):
                config = copy.deepcopy(get_pluggable_configuration(pluggable_type, pluggable_name))
                self._populate_section(pluggable_type.value, pluggable_name, config)

        self._data_loaded = True
示例#9
0
    def __init__(self):
        """Create Model object."""

        self._property_titles = OrderedDict()
        self._sections = OrderedDict()
        self._sections[Model._INPUT_NAME] = OrderedDict()
        self._property_titles[Model._INPUT_NAME] = OrderedDict()
        for input_name in local_inputs():
            config = copy.deepcopy(get_input_configuration(input_name))
            self._populate_section(Model._INPUT_NAME, input_name, config)

        for pluggable_type in local_pluggables_types():
            self._sections[pluggable_type] = OrderedDict()
            self._property_titles[pluggable_type] = OrderedDict()
            for pluggable_name in local_pluggables(pluggable_type):
                config = copy.deepcopy(
                    get_pluggable_configuration(pluggable_type,
                                                pluggable_name))
                self._populate_section(pluggable_type, pluggable_name, config)
示例#10
0
    def _update_algorithm_problem(self):
        problem_name = self.get_section_property(JSONSchema.PROBLEM, JSONSchema.NAME)
        if problem_name is None:
            problem_name = self.get_property_default_value(JSONSchema.PROBLEM, JSONSchema.NAME)

        if problem_name is None:
            raise AquaChemistryError("No algorithm 'problem' section found on input.")

        algo_name = self.get_section_property(PluggableType.ALGORITHM.value, JSONSchema.NAME)
        if algo_name is not None and problem_name in InputParser.get_algorithm_problems(algo_name):
            return

        for algo_name in local_pluggables(PluggableType.ALGORITHM):
            if problem_name in self.get_algorithm_problems(algo_name):
                # set to the first algorithm to solve the problem
                self.set_section_property(
                    PluggableType.ALGORITHM.value, JSONSchema.NAME, algo_name)
                return

        # no algorithm solve this problem, remove section
        self.delete_section(PluggableType.ALGORITHM.value)
示例#11
0
    def _update_algorithm_input_schema(self):
        # find algorithm input
        default_name = self.get_property_default_value(
            PluggableType.INPUT.value, JSONSchema.NAME)
        input_name = self.get_section_property(PluggableType.INPUT.value,
                                               JSONSchema.NAME, default_name)
        if input_name is None:
            # find the first valid input for the problem
            problem_name = self.get_section_property(JSONSchema.PROBLEM,
                                                     JSONSchema.NAME)
            if problem_name is None:
                problem_name = self.get_property_default_value(
                    JSONSchema.PROBLEM, JSONSchema.NAME)

            if problem_name is None:
                raise AquaError(
                    "No algorithm 'problem' section found on input.")

            for name in local_pluggables(PluggableType.INPUT):
                if problem_name in self.get_input_problems(name):
                    # set to the first input to solve the problem
                    input_name = name
                    break

        if input_name is None:
            # just remove fromm schema if none solves the problem
            if PluggableType.INPUT.value in self._json_schema.schema[
                    'properties']:
                del self._json_schema.schema['properties'][
                    PluggableType.INPUT.value]
            return

        if default_name is None:
            default_name = input_name

        config = {}
        try:
            config = get_pluggable_configuration(PluggableType.INPUT,
                                                 input_name)
        except:
            pass

        input_schema = config[
            'input_schema'] if 'input_schema' in config else {}
        properties = input_schema[
            'properties'] if 'properties' in input_schema else {}
        properties[JSONSchema.NAME] = {'type': 'string'}
        required = input_schema[
            'required'] if 'required' in input_schema else []
        additionalProperties = input_schema[
            'additionalProperties'] if 'additionalProperties' in input_schema else True
        if default_name is not None:
            properties[JSONSchema.NAME]['default'] = default_name
            required.append(JSONSchema.NAME)

        if PluggableType.INPUT.value not in self._json_schema.schema[
                'properties']:
            self._json_schema.schema['properties'][
                PluggableType.INPUT.value] = {
                    'type': 'object'
                }

        self._json_schema.schema['properties'][
            PluggableType.INPUT.value]['properties'] = properties
        self._json_schema.schema['properties'][
            PluggableType.INPUT.value]['required'] = required
        self._json_schema.schema['properties'][PluggableType.INPUT.value][
            'additionalProperties'] = additionalProperties