def _update_driver_sections(self): driver_name = self.get_section_property(InputParser.DRIVER, JSONSchema.NAME) if driver_name is not None: driver_name = driver_name.strip().lower() for name in local_drivers(): name = name.lower() if driver_name is not None and driver_name == name: continue if name in self._sections: del self._sections[name] if driver_name is not None and driver_name not in self._sections: self.set_section(driver_name) value = self.get_section_default_properties(driver_name) if isinstance(value, dict): for property_name, property_value in value.items(): self.set_section_property(driver_name, property_name, property_value) else: if value is None: types = self.get_section_types(driver_name) if 'null' not in types: if 'string' in types: value = '' elif 'object' in types: value = {} elif 'array' in types: value = [] self.set_section_data(driver_name, value)
def _update_driver_input_schemas(self): # find driver name default_name = self.get_property_default_value(InputParser.DRIVER, JSONSchema.NAME) driver_name = self.get_section_property(InputParser.DRIVER, JSONSchema.NAME, default_name) if driver_name is not None: driver_name = driver_name.strip().lower() for name in local_drivers(): name_orig = name name = name.lower() if driver_name is not None and driver_name == name: config = get_driver_configuration(name_orig) input_schema = copy.deepcopy( config['input_schema']) if 'input_schema' in config else { 'type': 'object' } if '$schema' in input_schema: del input_schema['$schema'] if 'id' in input_schema: del input_schema['id'] self.json_schema.schema['properties'][ driver_name] = input_schema else: if name in self.json_schema.schema['properties']: del self.json_schema.schema['properties'][name]
def get_combobox_parameters(self, section_name, property_name): from qiskit.aqua.parser import JSONSchema from qiskit.chemistry.parser import InputParser from qiskit.chemistry.drivers import local_drivers values = None types = ['string'] combobox_state = 'readonly' if InputParser.OPERATOR == section_name and JSONSchema.NAME == property_name: values = self.model.get_operator_section_names() elif InputParser.DRIVER == section_name and JSONSchema.NAME == property_name: values = local_drivers() else: combobox_state, types, values = \ super().get_combobox_parameters(section_name, property_name) return combobox_state, types, values
def run_driver(self, params, backend=None): """ Runs the Qiskit Chemistry driver Args: params (Union(dictionary, filename)): Chemistry input data backend (QuantumInstance or BaseBackend): the experimental settings to be used in place of backend name Raises: QiskitChemistryError: Missing Input """ if params is None: raise QiskitChemistryError("Missing input.") self._operator = None self._chemistry_result = None self._qiskit_aqua = None self._hdf5_file = None self._parser = InputParser(params) self._parser.parse() # before merging defaults attempts to find a provider for the backend in case no # provider was passed if backend is None and \ self._parser.get_section_property(JSONSchema.BACKEND, JSONSchema.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)) # set provider and name in input file for proper backend schema dictionary build if isinstance(backend, BaseBackend): self._parser.backend = backend self._parser.add_section_properties( JSONSchema.BACKEND, { JSONSchema.PROVIDER: get_provider_from_backend(backend), JSONSchema.NAME: backend.name(), }) self._parser.validate_merge_defaults() experiment_name = "-- no &NAME section found --" if JSONSchema.NAME in self._parser.get_section_names(): name_sect = self._parser.get_section(JSONSchema.NAME) if name_sect is not None: experiment_name = str(name_sect) logger.info('Running chemistry problem from input file: %s', self._parser.get_filename()) logger.info('Experiment description: %s', experiment_name.rstrip()) driver_name = self._parser.get_section_property( InputParser.DRIVER, JSONSchema.NAME) if driver_name is None: raise QiskitChemistryError( 'Property "{0}" missing in section "{1}"'.format( JSONSchema.NAME, InputParser.DRIVER)) self._hdf5_file = \ self._parser.get_section_property(InputParser.DRIVER, InputParser.HDF5_OUTPUT) if driver_name not in local_drivers(): raise QiskitChemistryError( 'Driver "{0}" missing in local drivers'.format(driver_name)) work_path = None input_file = self._parser.get_filename() if input_file is not None: work_path = os.path.dirname(os.path.realpath(input_file)) section = self._parser.get_section(driver_name) driver = get_driver_class(driver_name).init_from_input(section) driver.work_path = work_path molecule = driver.run() if work_path is not None and \ self._hdf5_file is not None and not os.path.isabs(self._hdf5_file): self._hdf5_file = os.path.abspath( os.path.join(work_path, self._hdf5_file)) molecule.log() if self._hdf5_file is not None: molecule.save(self._hdf5_file) logger.info("HDF5 file saved '%s'", self._hdf5_file) # Run the Hamiltonian to process the QMolecule and get an input for algorithms clazz = get_chemistry_operator_class( self._parser.get_section_property(InputParser.OPERATOR, JSONSchema.NAME)) self._operator = clazz.init_params( self._parser.get_section_properties(InputParser.OPERATOR)) qubit_op, aux_ops = self.operator.run(molecule) input_object = EnergyInput(qubit_op, aux_ops) logger.debug('Core computed substitution variables %s', self.operator.molecule_info) result = self._parser.process_substitutions( self.operator.molecule_info) logger.debug('Substitutions %s', result) aqua_params = {} for section_name, section in self._parser.get_sections().items(): if section_name == JSONSchema.NAME or \ section_name == InputParser.DRIVER or \ section_name == driver_name.lower() or \ section_name == InputParser.OPERATOR or \ not isinstance(section, dict): continue aqua_params[section_name] = copy.deepcopy(section) if JSONSchema.PROBLEM == section_name and \ InputParser.AUTO_SUBSTITUTIONS in aqua_params[section_name]: del aqua_params[section_name][InputParser.AUTO_SUBSTITUTIONS] self._qiskit_aqua = QiskitAqua(aqua_params, input_object, backend)
def _load_driver_names(): if InputParser._DRIVER_NAMES is None: InputParser._DRIVER_NAMES = [ name.lower() for name in local_drivers() ]
def parse(self): """Parse the data.""" if self._inputdict is None: if self._filename is None: raise QiskitChemistryError("Missing input file") section = None self._sections = OrderedDict() contents = '' with open(self._filename, 'rt', encoding="utf8", errors='ignore') as f: for line in f: contents += line section = self._process_line(section, line) if self._sections: # convert to aqua compatible json dictionary based on schema driver_configs = OrderedDict() for driver_name in local_drivers(): driver_configs[driver_name.lower( )] = get_driver_configuration(driver_name) json_dict = OrderedDict() for section_name, section in self._sections.items(): types = [] if section_name.lower() in driver_configs: config = driver_configs[section_name.lower()] input_schema = copy.deepcopy( config['input_schema'] ) if 'input_schema' in config else { 'type': 'string' } if 'type' not in input_schema: input_schema['type'] = 'string' types = [input_schema['type']] else: types = self.get_section_types(section_name.lower()) if 'string' in types: json_dict[section_name] = section[ 'data'] if 'data' in section else '' else: json_dict[section_name] = section[ 'properties'] if 'properties' in section else OrderedDict( ) self._sections = json_dict else: contents = contents.strip().replace('\n', '').replace('\r', '') if len(contents) > 0: # check if input file was dictionary try: v = ast.literal_eval(contents) if isinstance(v, dict): self._inputdict = json.loads(json.dumps(v)) self._load_parser_from_dict() except: pass else: self._load_parser_from_dict() # check for old enable_substitutions name old_enable_substitutions = self.get_section_property( JSONSchema.PROBLEM, InputParser._OLD_ENABLE_SUBSTITUTIONS) if old_enable_substitutions is not None: self.delete_section_property(JSONSchema.PROBLEM, InputParser._OLD_ENABLE_SUBSTITUTIONS) self.set_section_property(JSONSchema.PROBLEM, InputParser.AUTO_SUBSTITUTIONS, old_enable_substitutions) self.json_schema.update_backend_schema() self.json_schema.update_pluggable_schemas(self) self._update_driver_input_schemas() self._update_operator_input_schema() self._sections = self._order_sections(self._sections) self._original_sections = copy.deepcopy(self._sections)
def _run_driver_from_parser(self, p, save_json_algo_file): if p is None: raise QiskitChemistryError("Missing parser") # before merging defaults attempts to find a provider for the backend in case no # provider was passed if p.get_section_property(JSONSchema.BACKEND, JSONSchema.PROVIDER) is None: backend_name = p.get_section_property(JSONSchema.BACKEND, JSONSchema.NAME) if backend_name is not None: p.set_section_property(JSONSchema.BACKEND, JSONSchema.PROVIDER, get_provider_from_backend(backend_name)) p.validate_merge_defaults() # logger.debug('ALgorithm Input Schema: {}'.format(json.dumps(p..get_sections(), sort_keys=True, indent=4))) experiment_name = "-- no &NAME section found --" if JSONSchema.NAME in p.get_section_names(): name_sect = p.get_section(JSONSchema.NAME) if name_sect is not None: experiment_name = str(name_sect) logger.info('Running chemistry problem from input file: {}'.format( p.get_filename())) logger.info('Experiment description: {}'.format( experiment_name.rstrip())) driver_name = p.get_section_property(InputParser.DRIVER, JSONSchema.NAME) if driver_name is None: raise QiskitChemistryError( 'Property "{0}" missing in section "{1}"'.format( JSONSchema.NAME, InputParser.DRIVER)) hdf5_file = p.get_section_property(InputParser.DRIVER, QiskitChemistry.KEY_HDF5_OUTPUT) if driver_name not in local_drivers(): raise QiskitChemistryError( 'Driver "{0}" missing in local drivers'.format(driver_name)) work_path = None input_file = p.get_filename() if input_file is not None: work_path = os.path.dirname(os.path.realpath(input_file)) section = p.get_section(driver_name) driver = get_driver_class(driver_name).init_from_input(section) driver.work_path = work_path molecule = driver.run() if work_path is not None and hdf5_file is not None and not os.path.isabs( hdf5_file): hdf5_file = os.path.abspath(os.path.join(work_path, hdf5_file)) molecule.log() if hdf5_file is not None: molecule._origin_driver_name = driver_name molecule._origin_driver_config = section if isinstance( section, str) else json.dumps( section, sort_keys=True, indent=4) molecule.save(hdf5_file) text = "HDF5 file saved '{}'".format(hdf5_file) logger.info(text) if not save_json_algo_file: logger.info('Run ended with hdf5 file saved.') return QiskitChemistry._DRIVER_RUN_TO_HDF5, text # Run the Hamiltonian to process the QMolecule and get an input for algorithms cls = get_chemistry_operator_class( p.get_section_property(InputParser.OPERATOR, JSONSchema.NAME)) self._core = cls.init_params( p.get_section_properties(InputParser.OPERATOR)) input_object = self._core.run(molecule) logger.debug('Core computed substitution variables {}'.format( self._core.molecule_info)) result = p.process_substitutions(self._core.molecule_info) logger.debug('Substitutions {}'.format(result)) params = {} for section_name, section in p.get_sections().items(): if section_name == JSONSchema.NAME or \ section_name == InputParser.DRIVER or \ section_name == driver_name.lower() or \ section_name == InputParser.OPERATOR or \ not isinstance(section, dict): continue params[section_name] = copy.deepcopy(section) if JSONSchema.PROBLEM == section_name and InputParser.AUTO_SUBSTITUTIONS in params[ section_name]: del params[section_name][InputParser.AUTO_SUBSTITUTIONS] return QiskitChemistry._DRIVER_RUN_TO_ALGO_INPUT, params, input_object
def create_popup(self, section_name, property_name, parent, value): from qiskit.chemistry.parser import InputParser from qiskit.aqua.parser import JSONSchema from qiskit.chemistry.drivers import local_drivers values = None types = ['string'] combobox_state = 'readonly' if InputParser.OPERATOR == section_name and JSONSchema.NAME == property_name: values = self.model.get_operator_section_names() elif InputParser.DRIVER == section_name and JSONSchema.NAME == property_name: values = local_drivers() elif JSONSchema.NAME == property_name and Model.is_pluggable_section( section_name): values = self.model.get_pluggable_section_names(section_name) elif JSONSchema.BACKEND == section_name and \ (JSONSchema.NAME == property_name or JSONSchema.PROVIDER == property_name): values = [] if JSONSchema.PROVIDER == property_name: combobox_state = 'normal' for provider, _ in self.model.providers.items(): values.append(provider) else: provider_name = self.model.get_section_property( JSONSchema.BACKEND, JSONSchema.PROVIDER) values = self.model.providers.get(provider_name, []) else: values = self.model.get_property_default_values( section_name, property_name) types = self.model.get_property_types(section_name, property_name) if values is not None: value = '' if value is None else str(value) values = [str(v) for v in values] widget = ComboboxPopup(self, section_name, property_name, parent, exportselection=0, state=combobox_state, values=values) widget._text = value if len(values) > 0: if value in values: widget.current(values.index(value)) else: widget.current(0) return widget value = '' if value is None else value if 'number' in types or 'integer' in types: vcmd = self._validate_integer_command if 'integer' in types else self._validate_float_command vcmd = (vcmd, '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W') widget = EntryPopup(self, section_name, property_name, parent, value, validate='all', validatecommand=vcmd, state=tk.NORMAL) widget.selectAll() return widget if 'object' in types or 'array' in types: try: if isinstance(value, str): value = value.strip() if len(value) > 0: value = ast.literal_eval(value) if isinstance(value, dict) or isinstance(value, list): value = json.dumps(value, sort_keys=True, indent=4) except: pass widget = TextPopup(self, section_name, property_name, parent, value) widget.selectAll() return widget