示例#1
0
    def _update_operator_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 QiskitChemistryError(
                "No algorithm 'problem' section found on input.")

        operator_name = self.get_section_property(InputParser.OPERATOR,
                                                  JSONSchema.NAME)
        if operator_name is not None and problem_name in InputParser.get_operator_problems(
                operator_name):
            return

        for operator_name in local_chemistry_operators():
            if problem_name in self.get_operator_problems(operator_name):
                # set to the first input to solve the problem
                self.set_section_property(InputParser.OPERATOR,
                                          JSONSchema.NAME, operator_name)
                return

        # no input solve this problem, remove section
        self.delete_section(InputParser.OPERATOR)
示例#2
0
    def get_operator_section_names(self):
        from qiskit.chemistry.parser import InputParser
        from qiskit.aqua.parser import JSONSchema
        from qiskit.chemistry.core import local_chemistry_operators
        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_chemistry_operators()

        operator_names = []
        for operator_name in local_chemistry_operators():
            problems = InputParser.get_operator_problems(operator_name)
            if problem_name in problems:
                operator_names.append(operator_name)

        return operator_names
示例#3
0
    def _update_operator_input_schema(self):
        # find operator
        default_name = self.get_property_default_value(InputParser.OPERATOR, JSONSchema.NAME)
        operator_name = self.get_section_property(InputParser.OPERATOR, JSONSchema.NAME, default_name)
        if operator_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 QiskitChemistryError("No algorithm 'problem' section found on input.")

            for name in local_chemistry_operators():
                if problem_name in self.get_operator_problems(name):
                    # set to the first input to solve the problem
                    operator_name = name
                    break

        if operator_name is None:
            # just remove fromm schema if none solves the problem
            if InputParser.OPERATOR in self.json_schema.schema['properties']:
                del self.json_schema.schema['properties'][InputParser.OPERATOR]

            return

        if default_name is None:
            default_name = operator_name

        config = {}
        try:
            config = get_chemistry_operator_configuration(operator_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 InputParser.OPERATOR not in self.json_schema.schema['properties']:
            self.json_schema.schema['properties'][InputParser.OPERATOR] = {'type': 'object'}

        self.json_schema.schema['properties'][InputParser.OPERATOR]['properties'] = properties
        self.json_schema.schema['properties'][InputParser.OPERATOR]['required'] = required
        self.json_schema.schema['properties'][InputParser.OPERATOR]['additionalProperties'] = additionalProperties