Ejemplo n.º 1
0
    def _update_pluggable_input_schema(self, pluggable_type, pluggable_name, default_name):
        config = {}
        try:
            if pluggable_type is not None and pluggable_name is not None:
                config = get_pluggable_configuration(pluggable_type, pluggable_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 pluggable_type not in self._schema['properties']:
            self._schema['properties'][pluggable_type] = {'type': 'object'}

        self._schema['properties'][pluggable_type]['properties'] = properties
        if len(required) > 0:
            self._schema['properties'][pluggable_type]['required'] = required
        elif 'required' in self._schema['properties'][pluggable_type]:
            del self._schema['properties'][pluggable_type]['required']

        self._schema['properties'][pluggable_type]['additionalProperties'] = additionalProperties
Ejemplo n.º 2
0
    def _update_pluggable_input_schema(self, pluggable_type, pluggable_name,
                                       default_name):
        config = {}
        try:
            config = get_pluggable_configuration(pluggable_type,
                                                 pluggable_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[InputParser.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[InputParser.NAME]['default'] = default_name
            required.append(InputParser.NAME)

        if pluggable_type not in self._schema['definitions']:
            self._schema['definitions'][pluggable_type] = {'type': 'object'}

        if pluggable_type not in self._schema['properties']:
            self._schema['properties'][pluggable_type] = {
                '$ref': "#/definitions/{}".format(pluggable_type)
            }

        self._schema['definitions'][pluggable_type]['properties'] = properties
        self._schema['definitions'][pluggable_type]['required'] = required
        self._schema['definitions'][pluggable_type][
            'additionalProperties'] = additionalProperties
Ejemplo n.º 3
0
    def _update_dependency_sections(self):
        algo_name = self.get_section_property(PluggableType.ALGORITHM.value, JSONSchema.NAME)
        config = {} if algo_name is None else get_pluggable_configuration(PluggableType.ALGORITHM, algo_name)
        classical = config['classical'] if 'classical' in config else False
        pluggable_dependencies = [] if 'depends' not in config else config['depends']
        pluggable_defaults = {} if 'defaults' not in config else config['defaults']
        for pluggable_type in local_pluggables_types():
            # remove pluggables from input that are not in the dependencies
            if pluggable_type not in [PluggableType.INPUT, PluggableType.ALGORITHM] and \
                    pluggable_type.value not in pluggable_dependencies and \
                    pluggable_type.value in self._sections:
                del self._sections[pluggable_type.value]

        for pluggable_type in pluggable_dependencies:
            pluggable_name = None
            if pluggable_type in pluggable_defaults:
                if JSONSchema.NAME in pluggable_defaults[pluggable_type]:
                    pluggable_name = pluggable_defaults[pluggable_type][JSONSchema.NAME]

            if pluggable_name is not None and pluggable_type not in self._sections:
                self.set_section_property(pluggable_type, JSONSchema.NAME, pluggable_name)
                # update default values for new dependency pluggable types
                self.set_section_properties(pluggable_type, self.get_section_default_properties(pluggable_type))

        # update backend based on classical
        if classical:
            if JSONSchema.BACKEND in self._sections:
                del self._sections[JSONSchema.BACKEND]
        else:
            if JSONSchema.BACKEND not in self._sections:
                self.set_section_properties(JSONSchema.BACKEND, self.get_section_default_properties(JSONSchema.BACKEND))

        # reorder sections
        self._sections = self._order_sections(self._sections)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    def update_pluggable_input_schemas(self, input_parser):
        """
        Updates schemas of all pluggables

        Args:
            input_parser (obj): input parser
        """
        # find algorithm
        default_algo_name = self.get_property_default_value(PluggableType.ALGORITHM.value, JSONSchema.NAME)
        algo_name = input_parser.get_section_property(PluggableType.ALGORITHM.value, JSONSchema.NAME, default_algo_name)

        # update algorithm scheme
        if algo_name is not None:
            self._update_pluggable_input_schema(PluggableType.ALGORITHM.value, algo_name, default_algo_name)

        # update algorithm depoendencies scheme
        config = {} if algo_name is None else get_pluggable_configuration(PluggableType.ALGORITHM, algo_name)
        classical = config['classical'] if 'classical' in config else False
        pluggable_dependencies = [] if 'depends' not in config else config['depends']
        pluggable_defaults = {
        } if 'defaults' not in config else config['defaults']
        pluggable_types = local_pluggables_types()
        for pluggable_type in pluggable_types:
            if pluggable_type not in [PluggableType.INPUT, PluggableType.ALGORITHM] and \
                    pluggable_type.value not in pluggable_dependencies:
                # remove pluggables from schema that ate not in the dependencies
                if pluggable_type.value in self._schema['properties']:
                    del self._schema['properties'][pluggable_type.value]

        # update algorithm backend from schema if it is classical or not
        if classical:
            if JSONSchema.BACKEND in self._schema['properties']:
                del self._schema['properties'][JSONSchema.BACKEND]
        else:
            if JSONSchema.BACKEND not in self._schema['properties']:
                self._schema['properties'][JSONSchema.BACKEND] = self._original_schema['properties'][JSONSchema.BACKEND]

        # update schema with dependencies
        for pluggable_type in pluggable_dependencies:
            pluggable_name = None
            default_properties = {}
            if pluggable_type in pluggable_defaults:
                for key, value in pluggable_defaults[pluggable_type].items():
                    if key == JSONSchema.NAME:
                        pluggable_name = pluggable_defaults[pluggable_type][key]
                    else:
                        default_properties[key] = value

            default_name = pluggable_name
            pluggable_name = input_parser.get_section_property(
                pluggable_type, JSONSchema.NAME, pluggable_name)

            # update dependency schema
            self._update_pluggable_input_schema(
                pluggable_type, pluggable_name, default_name)
            for property_name in self._schema['properties'][pluggable_type]['properties'].keys():
                if property_name in default_properties:
                    self._schema['properties'][pluggable_type]['properties'][property_name]['default'] = default_properties[property_name]
Ejemplo n.º 6
0
    def _merge_dependencies(self):
        algo_name = self.get_section_property(PluggableType.ALGORITHM.value,
                                              JSONSchema.NAME)
        if algo_name is None:
            return

        config = get_pluggable_configuration(PluggableType.ALGORITHM,
                                             algo_name)
        pluggable_dependencies = [] if 'depends' not in config else config[
            'depends']
        pluggable_defaults = {} if 'defaults' not in config else config[
            'defaults']
        for pluggable_type in local_pluggables_types():
            if pluggable_type not in [PluggableType.INPUT, PluggableType.ALGORITHM] and \
                    pluggable_type.value not in pluggable_dependencies:
                # remove pluggables from input that are not in the dependencies
                if pluggable_type.value in self._sections:
                    del self._sections[pluggable_type.value]

        section_names = self.get_section_names()
        for pluggable_type in pluggable_dependencies:
            pluggable_name = None
            new_properties = {}
            if pluggable_type in pluggable_defaults:
                for key, value in pluggable_defaults[pluggable_type].items():
                    if key == JSONSchema.NAME:
                        pluggable_name = pluggable_defaults[pluggable_type][
                            key]
                    else:
                        new_properties[key] = value

            if pluggable_name is None:
                continue

            if pluggable_type not in section_names:
                self.set_section(pluggable_type)

            if self.get_section_property(pluggable_type,
                                         JSONSchema.NAME) is None:
                self.set_section_property(pluggable_type, JSONSchema.NAME,
                                          pluggable_name)

            if pluggable_name == self.get_section_property(
                    pluggable_type, JSONSchema.NAME):
                properties = self.get_section_properties(pluggable_type)
                if new_properties:
                    new_properties.update(properties)
                else:
                    new_properties = properties

                self.set_section_properties(pluggable_type, new_properties)
Ejemplo n.º 7
0
    def get_algorithm_problems(algo_name):
        """
        Get algorithm problem name list
        Args:
            algo_name (string): algorithm name

        Returns:
            Returns list of problem names
        """
        config = get_pluggable_configuration(PluggableType.ALGORITHM, algo_name)
        if 'problems' in config:
            return config['problems']

        return []
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 10
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
Ejemplo n.º 11
0
    def get_input_problems(input_name):
        config = get_pluggable_configuration(PluggableType.INPUT, input_name)
        if 'problems' in config:
            return config['problems']

        return []