def __init__(self, input=None): """Create InputParser object.""" self._original_sections = None self._filename = None self._sections = None if input is not None: if isinstance(input, dict): self._sections = input elif isinstance(input, str): self._filename = input else: raise AquaError("Invalid parser input type.") self._section_order = [ JSONSchema.PROBLEM, PluggableType.INPUT.value, PluggableType.ALGORITHM.value ] for pluggable_type in local_pluggables_types(): if pluggable_type not in [ PluggableType.INPUT, PluggableType.ALGORITHM ]: self._section_order.append(pluggable_type.value) self._section_order.extend([JSONSchema.BACKEND, InputParser._UNKNOWN]) self._json_schema = JSONSchema( os.path.join(os.path.dirname(__file__), 'input_schema.json')) self._json_schema.populate_problem_names() self._json_schema.commit_changes()
def is_pluggable_section(section_name): section_name = JSONSchema.format_section_name(section_name) for pluggable_type in local_pluggables_types(): if section_name == pluggable_type.value: return True return False
def _update_dependency_sections(self): algo_name = self.get_section_property(JSONSchema.ALGORITHM,JSONSchema.NAME) config = {} if algo_name is None else get_algorithm_configuration(algo_name) classical = config['classical'] if 'classical' in config else False pluggable_dependencies = [] if 'depends' not in config else config['depends'] pluggable_defaults = {} if 'defaults' not in config else config['defaults'] pluggable_types = local_pluggables_types() for pluggable_type in pluggable_types: # remove pluggables from input that are not in the dependencies if pluggable_type != JSONSchema.ALGORITHM and pluggable_type not in pluggable_dependencies and pluggable_type in self._sections: del self._sections[pluggable_type] for pluggable_type in pluggable_dependencies: pluggable_name = None if pluggable_type in pluggable_defaults: if JSONSchema.NAME in pluggable_defaults[pluggable_type]: pluggable_name = pluggable_defaults[pluggable_type][JSONSchema.NAME] if pluggable_name is not None and pluggable_type not in self._sections: self.set_section_property(pluggable_type,JSONSchema.NAME,pluggable_name) # update default values for new dependency pluggable types self.set_section_properties(pluggable_type,self.get_section_default_properties(pluggable_type)) # update backend based on classical if classical: if JSONSchema.BACKEND in self._sections: del self._sections[JSONSchema.BACKEND] else: if JSONSchema.BACKEND not in self._sections: self._sections[JSONSchema.BACKEND] = self.get_section_default_properties(JSONSchema.BACKEND) #reorder sections self._sections = self._order_sections(self._sections)
def __init__(self, input=None): """Create InputParser object.""" self._original_sections = None self._filename = None self._sections = None if input is not None: if isinstance(input, dict): self._sections = input elif isinstance(input, str): self._filename = input else: raise AlgorithmError("Invalid parser input type.") self._section_order = [ InputParser.PROBLEM, InputParser.INPUT, InputParser.ALGORITHM ] for pluggable_type in local_pluggables_types(): if pluggable_type != InputParser.ALGORITHM: self._section_order.append(pluggable_type) self._section_order.extend([InputParser.BACKEND, InputParser._UNKNOWN]) problems_dict = OrderedDict() for algo_name in local_algorithms(): problems = InputParser.get_algorithm_problems(algo_name) for problem in problems: problems_dict[problem] = None problems_enum = {'enum': list(problems_dict.keys())} jsonfile = os.path.join(os.path.dirname(__file__), 'input_schema.json') with open(jsonfile) as json_file: self._schema = json.load(json_file) self._schema['definitions'][InputParser.PROBLEM]['properties'][ InputParser.NAME]['oneOf'] = [problems_enum] self._original_schema = copy.deepcopy(self._schema)
def _load_data(self): if self._data_loaded: return from qiskit_aqua import (local_pluggables_types, local_pluggables, get_pluggable_configuration) from qiskit_aqua.input import (local_inputs, get_input_configuration) self._property_titles = OrderedDict() self._sections = OrderedDict() self._sections[Model._INPUT_NAME] = OrderedDict() self._property_titles[Model._INPUT_NAME] = OrderedDict() for input_name in local_inputs(): config = copy.deepcopy(get_input_configuration(input_name)) self._populate_section(Model._INPUT_NAME, input_name, config) for pluggable_type in local_pluggables_types(): self._sections[pluggable_type] = OrderedDict() self._property_titles[pluggable_type] = OrderedDict() for pluggable_name in local_pluggables(pluggable_type): config = copy.deepcopy( get_pluggable_configuration(pluggable_type, pluggable_name)) self._populate_section(pluggable_type, pluggable_name, config) self._data_loaded = True
def update_pluggable_input_schemas(self, input_parser): """ Updates schemas of all pluggables Args: input_parser (obj): input parser """ # find algorithm default_algo_name = self.get_property_default_value(PluggableType.ALGORITHM.value, JSONSchema.NAME) algo_name = input_parser.get_section_property(PluggableType.ALGORITHM.value, JSONSchema.NAME, default_algo_name) # update algorithm scheme if algo_name is not None: self._update_pluggable_input_schema(PluggableType.ALGORITHM.value, algo_name, default_algo_name) # update algorithm depoendencies scheme config = {} if algo_name is None else get_pluggable_configuration(PluggableType.ALGORITHM, algo_name) classical = config['classical'] if 'classical' in config else False pluggable_dependencies = [] if 'depends' not in config else config['depends'] pluggable_defaults = { } if 'defaults' not in config else config['defaults'] pluggable_types = local_pluggables_types() for pluggable_type in pluggable_types: if pluggable_type not in [PluggableType.INPUT, PluggableType.ALGORITHM] and \ pluggable_type.value not in pluggable_dependencies: # remove pluggables from schema that ate not in the dependencies if pluggable_type.value in self._schema['properties']: del self._schema['properties'][pluggable_type.value] # update algorithm backend from schema if it is classical or not if classical: if JSONSchema.BACKEND in self._schema['properties']: del self._schema['properties'][JSONSchema.BACKEND] else: if JSONSchema.BACKEND not in self._schema['properties']: self._schema['properties'][JSONSchema.BACKEND] = self._original_schema['properties'][JSONSchema.BACKEND] # update schema with dependencies for pluggable_type in pluggable_dependencies: pluggable_name = None default_properties = {} if pluggable_type in pluggable_defaults: for key, value in pluggable_defaults[pluggable_type].items(): if key == JSONSchema.NAME: pluggable_name = pluggable_defaults[pluggable_type][key] else: default_properties[key] = value default_name = pluggable_name pluggable_name = input_parser.get_section_property( pluggable_type, JSONSchema.NAME, pluggable_name) # update dependency schema self._update_pluggable_input_schema( pluggable_type, pluggable_name, default_name) for property_name in self._schema['properties'][pluggable_type]['properties'].keys(): if property_name in default_properties: self._schema['properties'][pluggable_type]['properties'][property_name]['default'] = default_properties[property_name]
def _merge_dependencies(self): algo_name = self.get_section_property(PluggableType.ALGORITHM.value, JSONSchema.NAME) if algo_name is None: return config = get_pluggable_configuration(PluggableType.ALGORITHM, algo_name) pluggable_dependencies = [] if 'depends' not in config else config[ 'depends'] pluggable_defaults = {} if 'defaults' not in config else config[ 'defaults'] for pluggable_type in local_pluggables_types(): if pluggable_type not in [PluggableType.INPUT, PluggableType.ALGORITHM] and \ pluggable_type.value not in pluggable_dependencies: # remove pluggables from input that are not in the dependencies if pluggable_type.value in self._sections: del self._sections[pluggable_type.value] section_names = self.get_section_names() for pluggable_type in pluggable_dependencies: pluggable_name = None new_properties = {} if pluggable_type in pluggable_defaults: for key, value in pluggable_defaults[pluggable_type].items(): if key == JSONSchema.NAME: pluggable_name = pluggable_defaults[pluggable_type][ key] else: new_properties[key] = value if pluggable_name is None: continue if pluggable_type not in section_names: self.set_section(pluggable_type) if self.get_section_property(pluggable_type, JSONSchema.NAME) is None: self.set_section_property(pluggable_type, JSONSchema.NAME, pluggable_name) if pluggable_name == self.get_section_property( pluggable_type, JSONSchema.NAME): properties = self.get_section_properties(pluggable_type) if new_properties: new_properties.update(properties) else: new_properties = properties self.set_section_properties(pluggable_type, new_properties)
def _merge_default_values(self): section_names = self.get_section_names() if JSONSchema.NAME not in section_names: self.set_section(JSONSchema.NAME) if JSONSchema.ALGORITHM in section_names: if JSONSchema.PROBLEM not in section_names: self.set_section(JSONSchema.PROBLEM) self._json_schema.update_pluggable_input_schemas(self) self._merge_dependencies() self._update_driver_sections() self._update_driver_input_schemas() self._update_operator_input_schema() # do not merge any pluggable that doesn't have name default in schema default_section_names = [] pluggable_types = local_pluggables_types() for section_name in self.get_default_section_names(): if section_name in pluggable_types: if self.get_property_default_value( section_name, JSONSchema.NAME) is not None: default_section_names.append(section_name) else: default_section_names.append(section_name) section_names = set( self.get_section_names()) | set(default_section_names) for section_name in section_names: if section_name not in self._sections: self.set_section(section_name) new_properties = self.get_section_default_properties(section_name) if new_properties is not None: if self.section_is_text(section_name): text = self.get_section_text(section_name) if (text is None or len(text) == 0) and \ isinstance(new_properties, str) and \ len(new_properties) > 0 and \ text != new_properties: self.set_section_data(section_name, new_properties) else: properties = self.get_section_properties(section_name) new_properties.update(properties) self.set_section_properties(section_name, new_properties) self._sections = self._order_sections(self._sections)
def _load_data(self): if self._data_loaded: return from qiskit_aqua import (local_pluggables_types, local_pluggables, get_pluggable_configuration) self._property_titles = OrderedDict() self._sections = OrderedDict() for pluggable_type in local_pluggables_types(): self._sections[pluggable_type.value] = OrderedDict() self._property_titles[pluggable_type.value] = OrderedDict() for pluggable_name in local_pluggables(pluggable_type): config = copy.deepcopy(get_pluggable_configuration(pluggable_type, pluggable_name)) self._populate_section(pluggable_type.value, pluggable_name, config) self._data_loaded = True
def __init__(self, input=None): """Create InputParser object.""" self._sections = OrderedDict() self._original_sections = OrderedDict() self._filename = None self._inputdict = None if input is not None: if isinstance(input, dict): self._inputdict = input elif isinstance(input, str): self._filename = input else: raise AquaChemistryError("Invalid parser input type.") self._section_order = [ JSONSchema.NAME, JSONSchema.PROBLEM, InputParser.DRIVER, InputParser._UNKNOWN, InputParser.OPERATOR, JSONSchema.ALGORITHM ] for pluggable_type in local_pluggables_types(): if pluggable_type != JSONSchema.ALGORITHM: self._section_order.append(pluggable_type) self._section_order.append(JSONSchema.BACKEND) jsonfile = os.path.join(os.path.dirname(__file__), 'substitutions.json') with open(jsonfile) as json_file: self._substitutions = json.load(json_file) self._json_schema = JSONSchema( os.path.join(os.path.dirname(__file__), 'input_schema.json')) # get some properties from algorithms schema self._json_schema.copy_section_from_aqua_schema(JSONSchema.ALGORITHM) self._json_schema.copy_section_from_aqua_schema(JSONSchema.BACKEND) self._json_schema.copy_section_from_aqua_schema(JSONSchema.PROBLEM) self._json_schema.schema['properties'][JSONSchema.PROBLEM][ 'properties'][InputParser.AUTO_SUBSTITUTIONS] = { "type": "boolean", "default": "true" } self._json_schema.populate_problem_names() self._json_schema.commit_changes()
def __init__(self): """Create Model object.""" self._property_titles = OrderedDict() self._sections = OrderedDict() self._sections[Model._INPUT_NAME] = OrderedDict() self._property_titles[Model._INPUT_NAME] = OrderedDict() for input_name in local_inputs(): config = copy.deepcopy(get_input_configuration(input_name)) self._populate_section(Model._INPUT_NAME, input_name, config) for pluggable_type in local_pluggables_types(): self._sections[pluggable_type] = OrderedDict() self._property_titles[pluggable_type] = OrderedDict() for pluggable_name in local_pluggables(pluggable_type): config = copy.deepcopy( get_pluggable_configuration(pluggable_type, pluggable_name)) self._populate_section(pluggable_type, pluggable_name, config)
def _update_dependency_sections(self): algo_name = self.get_section_property(InputParser.ALGORITHM, InputParser.NAME) config = {} if algo_name is None else get_algorithm_configuration( algo_name) classical = config['classical'] if 'classical' in config else False pluggable_dependencies = [] if 'depends' not in config else config[ 'depends'] pluggable_defaults = {} if 'defaults' not in config else config[ 'defaults'] pluggable_types = local_pluggables_types() for pluggable_type in pluggable_types: if pluggable_type != InputParser.ALGORITHM and pluggable_type not in pluggable_dependencies: # remove pluggables from input that are not in the dependencies if pluggable_type in self._sections: del self._sections[pluggable_type] for pluggable_type in pluggable_dependencies: pluggable_name = None if pluggable_type in pluggable_defaults: if InputParser.NAME in pluggable_defaults[pluggable_type]: pluggable_name = pluggable_defaults[pluggable_type][ InputParser.NAME] if pluggable_name is not None and pluggable_type not in self._sections: self.set_section_property(pluggable_type, InputParser.NAME, pluggable_name) # update backend based on classical if classical: if InputParser.BACKEND in self._sections: del self._sections[InputParser.BACKEND] else: if InputParser.BACKEND not in self._sections: self._sections[ InputParser.BACKEND] = self.get_section_default_properties( InputParser.BACKEND)
def is_pluggable_section(section_name): return JSONSchema.format_section_name(section_name) in local_pluggables_types()
def _update_pluggable_input_schemas(self): # find alogorithm default_algo_name = self.get_property_default_value( InputParser.ALGORITHM, InputParser.NAME) algo_name = self.get_section_property(InputParser.ALGORITHM, InputParser.NAME, default_algo_name) # update alogorithm scheme if algo_name is not None: self._update_pluggable_input_schema(InputParser.ALGORITHM, algo_name, default_algo_name) # update alogorithm depoendencies scheme config = {} if algo_name is None else get_algorithm_configuration( algo_name) classical = config['classical'] if 'classical' in config else False pluggable_dependencies = [] if 'depends' not in config else config[ 'depends'] pluggable_defaults = {} if 'defaults' not in config else config[ 'defaults'] pluggable_types = local_pluggables_types() for pluggable_type in pluggable_types: if pluggable_type != InputParser.ALGORITHM and pluggable_type not in pluggable_dependencies: # remove pluggables from schema that ate not in the dependencies if pluggable_type in self._schema['definitions']: del self._schema['definitions'][pluggable_type] if pluggable_type in self._schema['properties']: del self._schema['properties'][pluggable_type] # update algorithm backend from schema if it is classical or not if classical: if InputParser.BACKEND in self._schema['definitions']: del self._schema['definitions'][InputParser.BACKEND] if InputParser.BACKEND in self._schema['properties']: del self._schema['properties'][InputParser.BACKEND] else: if InputParser.BACKEND not in self._schema['definitions']: self._schema['definitions'][ InputParser.BACKEND] = self._original_schema[ 'definitions'][InputParser.BACKEND] if InputParser.BACKEND not in self._schema['properties']: self._schema['properties'][ InputParser.BACKEND] = self._original_schema['properties'][ InputParser.BACKEND] # update schema with dependencies for pluggable_type in pluggable_dependencies: pluggable_name = None default_properties = {} if pluggable_type in pluggable_defaults: for key, value in pluggable_defaults[pluggable_type].items(): if key == InputParser.NAME: pluggable_name = pluggable_defaults[pluggable_type][ key] else: default_properties[key] = value default_name = pluggable_name pluggable_name = self.get_section_property(pluggable_type, InputParser.NAME, pluggable_name) if pluggable_name is None: continue # update dependency schema self._update_pluggable_input_schema(pluggable_type, pluggable_name, default_name) for property_name in self._schema['definitions'][pluggable_type][ 'properties'].keys(): if property_name in default_properties: self._schema['definitions'][pluggable_type]['properties'][ property_name]['default'] = default_properties[ property_name]
def is_pluggable_section(section_name): return InputParser._format_section_name( section_name) in local_pluggables_types()