Beispiel #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)
Beispiel #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)
Beispiel #3
0
class TestInputParser(QISKitAcquaTestCase):
    """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)