def test_is_modified(self): json_dict = self.parser.to_JSON() 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_load_from_dict(self): json_dict = self.parser.to_JSON() p = InputParser(json_dict) p.parse() dict1 = json.loads(json.dumps(self.parser.to_dictionary())) dict2 = json.loads(json.dumps(p.to_dictionary())) self.assertEqual(dict1, dict2)
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.to_dictionary())) dict2 = json.loads(json.dumps(p.to_dictionary())) self.assertEqual(dict1, dict2)
def _run_drive(self, input, save_json_algo_file): if input is None: raise QiskitChemistryError("Missing input.") self._parser = InputParser(input) self._parser.parse() driver_return = self._run_driver_from_parser(self._parser, save_json_algo_file) driver_return[1]['input'] = driver_return[2].to_params() driver_return[1]['input']['name'] = driver_return[2].configuration[ 'name'] return driver_return[1]
def test_validate(self): json_dict = self.parser.to_JSON() 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(AquaError, p.validate_merge_defaults)
def run(self, input, output=None, backend=None): """ Runs the Aqua Chemistry experiment Args: input (dictionary/filename): Input data output (filename): Output data backend (BaseBackend): backend object Returns: result dictionary """ if input is None: raise QiskitChemistryError("Missing input.") self._parser = InputParser(input) self._parser.parse() driver_return = self._run_driver_from_parser(self._parser, False) if driver_return[0] == QiskitChemistry._DRIVER_RUN_TO_HDF5: logger.info('No further process.') return {'printable': [driver_return[1]]} data = run_algorithm(driver_return[1], driver_return[2], True, backend) if not isinstance(data, dict): raise QiskitChemistryError( "Algorithm run result should be a dictionary") convert_json_to_dict(data) if logger.isEnabledFor(logging.DEBUG): logger.debug('Algorithm returned: {}'.format( pprint.pformat(data, indent=4))) lines, result = self._format_result(data) logger.info('Processing complete. Final result available') result['printable'] = lines if output is not None: with open(output, 'w') as f: for line in lines: print(line, file=f) return result
def new(self): from qiskit_chemistry.parser import InputParser 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): from qiskit_chemistry.parser import InputParser from qiskit_aqua.parser import JSONSchema from qiskit_aqua import get_provider_from_backend, get_backends_from_provider if filename is None: return [] try: self._parser = InputParser(filename) self._parser.parse() # before merging defaults attempts to find a provider for the backend provider = self._parser.get_section_property( JSONSchema.BACKEND, JSONSchema.PROVIDER) if provider is None: backend_name = self._parser.get_section_property( JSONSchema.BACKEND, JSONSchema.NAME) if backend_name is not None: self._parser.set_section_property( JSONSchema.BACKEND, JSONSchema.PROVIDER, get_provider_from_backend(backend_name)) else: try: if provider not in self.providers: self._custom_providers[ provider] = get_backends_from_provider(provider) except Exception as e: logger.debug(str(e)) 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 setUp(self): filepath = self._get_resource_path('test_input_parser.txt') self.parser = InputParser(filepath) self.parser.parse()