def run_algorithm_to_json(params, algo_input=None, jsonfile='algorithm.json'): """ Run algorithm as named in params. Using params and algo_input as input data and save the combined input as a json file. This json is self-contained and can later be used as a basis to call run_algorithm Args: params (dict): Dictionary of params for algo and dependent objects algo_input (AlgorithmInput): Main input data for algorithm. Optional, an algo may run entirely from params jsonfile (str): Name of file in which json should be saved Returns: Result dictionary containing the jsonfile name """ _discover_on_demand() inputparser = InputParser(params) inputparser.parse() inputparser.validate_merge_defaults() algo_params = copy.deepcopy(inputparser.get_sections()) if algo_input is not None: input_params = algo_input.to_params() convert_dict_to_json(input_params) algo_params['input'] = input_params algo_params['input']['name'] = algo_input.configuration['name'] logger.debug('Result: {}'.format( json.dumps(algo_params, sort_keys=True, indent=4))) with open(jsonfile, 'w') as fp: json.dump(algo_params, fp, sort_keys=True, indent=4) logger.info("Algorithm input file saved: '{}'".format(jsonfile)) return {'jsonfile': jsonfile}
def run_algorithm(params, algo_input=None, json_output=False, backend=None): """ Run algorithm as named in params. Using params and algo_input as input data and returning a result dictionary Args: params (dict): Dictionary of params for algo and dependent objects algo_input (AlgorithmInput): Main input data for algorithm. Optional, an algo may run entirely from params json_output (bool): False for regular python dictionary return, True for json conversion backend (BaseBackend): Backend object to be used in place of backend name Returns: Result dictionary containing result of algorithm computation """ algorithm, quantum_instance = build_algorithm_from_dict( params, algo_input, backend) value = algorithm.run(quantum_instance) if isinstance(value, dict) and json_output: convert_dict_to_json(value) return value
def json_result(self): """Return Experiment Result as JSON.""" return convert_dict_to_json(self.result) if isinstance( self.result, dict) else None