Ejemplo n.º 1
0
    def test_load_from_dict(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        dict1 = json.loads(json.dumps(self.parser.get_sections()))
        dict2 = json.loads(json.dumps(p.get_sections()))
        self.assertEqual(dict1, dict2)
Ejemplo n.º 2
0
    def test_save(self):
        save_path = self._get_resource_path('output.txt')
        self.parser.save_to_file(save_path)

        p = InputParser(save_path)
        p.parse()
        os.remove(save_path)
        dict1 = json.loads(json.dumps(self.parser.get_sections()))
        dict2 = json.loads(json.dumps(p.get_sections()))
        self.assertEqual(dict1, dict2)
Ejemplo n.º 3
0
    def load_file(self, filename):
        if filename is None:
            return []
        try:
            self._parser = InputParser(filename)
            self._parser.parse()
            uipreferences = UIPreferences()
            if uipreferences.get_populate_defaults(True):
                self._parser.validate_merge_defaults()
                self._parser.commit_changes()

            return self._parser.get_section_names()
        except:
            self._parser = None
            raise
Ejemplo n.º 4
0
    def test_is_modified(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        p.set_section_property('optimizer', 'maxfun', 1002)
        self.assertTrue(p.is_modified())
        self.assertEqual(p.get_section_property('optimizer', 'maxfun'), 1002)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    def get_pluggable_section_names(self, section_name):
        from qiskit_aqua.parser import InputParser
        from qiskit_aqua import local_pluggables
        from qiskit_aqua.parser import JSONSchema
        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)
Ejemplo n.º 7
0
    def delete_section_property(self, section_name, property_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.delete_section_property(section_name, property_name)
        if property_name == InputParser.NAME and \
            (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            self._parser.delete_section_properties(section_name)
Ejemplo n.º 8
0
    def new(self):
        try:
            dict = {}
            jsonfile = os.path.join(os.path.dirname(__file__),
                                    'input_template.json')
            with open(jsonfile) as json_file:
                dict = json.load(json_file)

            self._parser = InputParser(dict)
            self._parser.parse()
            uipreferences = UIPreferences()
            if uipreferences.get_populate_defaults(True):
                self._parser.validate_merge_defaults()
                self._parser.commit_changes()

            return self._parser.get_section_names()
        except:
            self._parser = None
            raise
Ejemplo n.º 9
0
    def delete_section_property(self, section_name, property_name):
        from qiskit_aqua.parser import InputParser
        from qiskit_aqua.parser import JSONSchema
        if self._parser is None:
            raise Exception('Input not initialized.')

        self._parser.delete_section_property(section_name, property_name)
        if property_name == JSONSchema.NAME and \
                (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            self._parser.delete_section_properties(section_name)
Ejemplo n.º 10
0
    def test_validate(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        try:
            p.validate_merge_defaults()
        except Exception as e:
            self.fail(str(e))

        p.set_section_property('optimizer', 'dummy', 1002)
        self.assertRaises(AlgorithmError, p.validate_merge_defaults)
Ejemplo n.º 11
0
    def set_section_property(self, section_name, property_name, value):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.set_section_property(section_name, property_name, value)
        if property_name == InputParser.NAME and \
            (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            properties = self._parser.get_section_default_properties(
                section_name)
            if isinstance(properties, dict):
                properties[InputParser.NAME] = value
                self._parser.delete_section_properties(section_name)
                for property_name, property_value in properties.items():
                    self._parser.set_section_property(section_name,
                                                      property_name,
                                                      property_value)
Ejemplo n.º 12
0
    def set_section_property(self, section_name, property_name, value):
        from qiskit_aqua.parser import InputParser
        from qiskit_aqua.parser import JSONSchema
        if self._parser is None:
            raise Exception('Input not initialized.')

        self._parser.set_section_property(section_name, property_name, value)
        if property_name == JSONSchema.NAME and \
                (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            properties = self._parser.get_section_default_properties(
                section_name)
            if isinstance(properties, dict):
                properties[JSONSchema.NAME] = value
                self._parser.delete_section_properties(section_name)
                for property_name, property_value in properties.items():
                    self._parser.set_section_property(section_name,
                                                      property_name,
                                                      property_value)
Ejemplo n.º 13
0
    def get_input_section_names(self):
        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_inputs()

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

        return input_names
Ejemplo n.º 14
0
    def get_input_section_names(self):
        from qiskit_aqua.parser import InputParser
        from qiskit_aqua.input import local_inputs
        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_inputs()

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

        return input_names
Ejemplo n.º 15
0
 def is_pluggable_section(section_name):
     from qiskit_aqua.parser import InputParser
     return InputParser.is_pluggable_section(section_name)
Ejemplo n.º 16
0
 def is_pluggable_section(section_name):
     return InputParser.is_pluggable_section(section_name)
Ejemplo n.º 17
0
 def setUp(self):
     filepath = self._get_resource_path('H2-0.735.json')
     self.parser = InputParser(filepath)
     self.parser.parse()
Ejemplo n.º 18
0
class TestInputParser(QiskitAquaTestCase):
    """Input Parser and algorithms tests."""
    def setUp(self):
        filepath = self._get_resource_path('H2-0.735.json')
        self.parser = InputParser(filepath)
        self.parser.parse()

    def test_save(self):
        save_path = self._get_resource_path('output.txt')
        self.parser.save_to_file(save_path)

        p = InputParser(save_path)
        p.parse()
        os.remove(save_path)
        dict1 = json.loads(json.dumps(self.parser.get_sections()))
        dict2 = json.loads(json.dumps(p.get_sections()))
        self.assertEqual(dict1, dict2)

    def test_load_from_dict(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        dict1 = json.loads(json.dumps(self.parser.get_sections()))
        dict2 = json.loads(json.dumps(p.get_sections()))
        self.assertEqual(dict1, dict2)

    def test_is_modified(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        p.set_section_property('optimizer', 'maxfun', 1002)
        self.assertTrue(p.is_modified())
        self.assertEqual(p.get_section_property('optimizer', 'maxfun'), 1002)

    def test_validate(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        try:
            p.validate_merge_defaults()
        except Exception as e:
            self.fail(str(e))

        p.set_section_property('optimizer', 'dummy', 1002)
        self.assertRaises(AlgorithmError, p.validate_merge_defaults)

    def test_run_algorithm(self):
        filepath = self._get_resource_path('ExactEigensolver.json')
        params = None
        with open(filepath) as json_file:
            params = json.load(json_file)

        dict_ret = None
        try:
            dict_ret = run_algorithm(params, None, False)
        except Exception as e:
            self.fail(str(e))

        self.assertIsInstance(dict_ret, dict)
Ejemplo n.º 19
0
class Model(object):
    def __init__(self):
        """Create Model object."""
        self._parser = InputParser()

    def is_empty(self):
        return self._parser is None or len(
            self._parser.get_section_names()) == 0

    def new(self):
        try:
            dict = {}
            jsonfile = os.path.join(os.path.dirname(__file__),
                                    'input_template.json')
            with open(jsonfile) as json_file:
                dict = json.load(json_file)

            self._parser = InputParser(dict)
            self._parser.parse()
            uipreferences = UIPreferences()
            if uipreferences.get_populate_defaults(True):
                self._parser.validate_merge_defaults()
                self._parser.commit_changes()

            return self._parser.get_section_names()
        except:
            self._parser = None
            raise

    def load_file(self, filename):
        if filename is None:
            return []
        try:
            self._parser = InputParser(filename)
            self._parser.parse()
            uipreferences = UIPreferences()
            if uipreferences.get_populate_defaults(True):
                self._parser.validate_merge_defaults()
                self._parser.commit_changes()

            return self._parser.get_section_names()
        except:
            self._parser = None
            raise

    def get_filename(self):
        if self._parser is None:
            return None

        return self._parser.get_filename()

    def is_modified(self):
        if self._parser is None:
            return False

        return self._parser.is_modified()

    def save_to_file(self, filename):
        if self.is_empty():
            raise AlgorithmError("Empty input data.")

        self._parser.save_to_file(filename)

    def get_section_names(self):
        if self._parser is None:
            return []

        return self._parser.get_section_names()

    def get_property_default_values(self, section_name, property_name):
        if self._parser is None:
            return None

        return self._parser.get_property_default_values(
            section_name, property_name)

    def section_is_text(self, section_name):
        if self._parser is None:
            return False

        return self._parser.section_is_text(section_name)

    def get_section(self, section_name):
        return self._parser.get_section(
            section_name) if self._parser is not None else None

    def get_section_text(self, section_name):
        if self._parser is None:
            return ''

        return self._parser.get_section_text(section_name)

    def get_section_properties(self, section_name):
        if self._parser is None:
            return {}

        return self._parser.get_section_properties(section_name)

    def default_properties_equals_properties(self, section_name):
        if self.section_is_text(section_name):
            return self.get_section_default_properties(
                section_name) == self.get_section_data(section_name)

        default_properties = self.get_section_default_properties(section_name)
        if isinstance(default_properties, OrderedDict):
            default_properties = dict(default_properties)

        properties = self.get_section_properties(section_name)
        if isinstance(properties, OrderedDict):
            properties = dict(properties)

        if not isinstance(default_properties, dict) or not isinstance(
                properties, dict):
            return default_properties == properties

        if InputParser.NAME in properties:
            default_properties[InputParser.NAME] = properties[InputParser.NAME]

        return default_properties == properties

    def get_section_property(self, section_name, property_name):
        if self._parser is None:
            return None

        return self._parser.get_section_property(section_name, property_name)

    def set_section(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.set_section(section_name)
        value = self._parser.get_section_default_properties(section_name)
        if isinstance(value, dict):
            for property_name, property_value in value.items():
                self._parser.set_section_property(section_name, property_name,
                                                  property_value)

            # do one more time in case schema was updated
            value = self._parser.get_section_default_properties(section_name)
            for property_name, property_value in value.items():
                self._parser.set_section_property(section_name, property_name,
                                                  property_value)
        else:
            if value is None:
                types = self._parser.get_section_types(section_name)
                if 'null' not in types:
                    if 'string' in types:
                        value = ''
                    elif 'object' in types:
                        value = {}
                    elif 'array' in types:
                        value = []

            self._parser.set_section_data(section_name, value)

    def set_default_properties_for_name(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        name = self._parser.get_section_property(section_name,
                                                 InputParser.NAME)
        self._parser.delete_section_properties(section_name)
        value = self._parser.get_section_default_properties(section_name)
        if name is not None:
            self._parser.set_section_property(section_name, InputParser.NAME,
                                              name)
        if isinstance(value, dict):
            for property_name, property_value in value.items():
                if property_name != InputParser.NAME:
                    self._parser.set_section_property(section_name,
                                                      property_name,
                                                      property_value)
        else:
            if value is None:
                types = self._parser.get_section_types(section_name)
                if 'null' not in types:
                    if 'string' in types:
                        value = ''
                    elif 'object' in types:
                        value = {}
                    elif 'array' in types:
                        value = []

            self._parser.set_section_data(section_name, value)

    @staticmethod
    def is_pluggable_section(section_name):
        return InputParser.is_pluggable_section(section_name)

    def get_input_section_names(self):
        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_inputs()

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

        return input_names

    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)

    def delete_section(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.delete_section(section_name)

    def get_default_sections(self):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        return self._parser.get_default_sections()

    def get_section_default_properties(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        return self._parser.get_section_default_properties(section_name)

    def allows_additional_properties(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        return self._parser.allows_additional_properties(section_name)

    def get_property_default_value(self, section_name, property_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        return self._parser.get_property_default_value(section_name,
                                                       property_name)

    def get_property_types(self, section_name, property_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        return self._parser.get_property_types(section_name, property_name)

    def set_section_property(self, section_name, property_name, value):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.set_section_property(section_name, property_name, value)
        if property_name == InputParser.NAME and \
            (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            properties = self._parser.get_section_default_properties(
                section_name)
            if isinstance(properties, dict):
                properties[InputParser.NAME] = value
                self._parser.delete_section_properties(section_name)
                for property_name, property_value in properties.items():
                    self._parser.set_section_property(section_name,
                                                      property_name,
                                                      property_value)

    def delete_section_property(self, section_name, property_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.delete_section_property(section_name, property_name)
        if property_name == InputParser.NAME and \
            (InputParser.is_pluggable_section(section_name) or section_name == InputParser.INPUT):
            self._parser.delete_section_properties(section_name)

    def set_section_text(self, section_name, value):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.set_section_data(section_name, value)

    def delete_section_text(self, section_name):
        if self._parser is None:
            raise AlgorithmError('Input not initialized.')

        self._parser.delete_section_text(section_name)
Ejemplo n.º 20
0
 def __init__(self):
     """Create Model object."""
     self._parser = InputParser()