Пример #1
0
    def run_algorithm_from_json(self, params, output=None, backend=None):
        """
        Runs the Aqua Chemistry experiment from json dictionary

        Args:
            params (dictionary): Input data
            output (filename):  Output data
            backend (BaseBackend): backend object

        Returns:
            result dictionary
        """
        ret = run_algorithm(params, None, True, backend)
        if not isinstance(ret, dict):
            raise AquaChemistryError(
                "Algorithm run result should be a dictionary")

        convert_json_to_dict(ret)
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug('Algorithm returned: {}'.format(
                pprint.pformat(ret, indent=4)))

        print('Output:')
        if isinstance(ret, dict):
            for k, v in ret.items():
                print("'{}': {}".format(k, v))
        else:
            print(ret)

        return ret
Пример #2
0
    def run(self, input, output=None):
        if input is None:
            raise AquaChemistryError("Missing input.")

        self._parser = InputParser(input)
        self._parser.parse()
        driver_return = self._run_driver_from_parser(self._parser, False)
        if driver_return[0] == AquaChemistry._DRIVER_RUN_TO_HDF5:
            logger.info('No further process.')
            return {'printable': [driver_return[1]]}

        data = run_algorithm(driver_return[1], driver_return[2], True)
        if not isinstance(data, dict):
            raise AquaChemistryError(
                "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
Пример #3
0
def main_algorithm():
    try:
        from qiskit_aqua._logging import get_logging_level, build_logging_config, set_logging_config
        from qiskit_aqua_cmd import Preferences
        from qiskit_aqua import run_algorithm
        from qiskit_aqua.utils import convert_json_to_dict
        parser = argparse.ArgumentParser(
            description='Qiskit Aqua Command Line Tool')
        parser.add_argument('input',
                            metavar='input',
                            help='Algorithm JSON input file')
        parser.add_argument('-jo',
                            metavar='output',
                            help='Algorithm JSON output file name',
                            required=False)

        args = parser.parse_args()

        # update logging setting with latest external packages
        preferences = Preferences()
        logging_level = logging.INFO
        if preferences.get_logging_config() is not None:
            set_logging_config(preferences.get_logging_config())
            logging_level = get_logging_level()

        preferences.set_logging_config(build_logging_config(logging_level))
        preferences.save()

        set_logging_config(preferences.get_logging_config())

        params = None
        with open(args.input) as json_file:
            params = json.load(json_file)

        ret = run_algorithm(params, None, True)

        if args.jo is not None:
            with open(args.jo, 'w') as f:
                print('{}'.format(ret), file=f)
        else:
            convert_json_to_dict(ret)
            print(
                '\n\n--------------------------------- R E S U L T ------------------------------------\n'
            )
            if isinstance(ret, dict):
                for k, v in ret.items():
                    print("'{}': {}".format(k, v))
            else:
                print(ret)
    finally:
        global _ROOT
        if _ROOT is not None:
            _ROOT.destroy()
            _ROOT = None
Пример #4
0
    def run_algorithm_from_json(self, params, output=None):
        ret = run_algorithm(params, None, True)
        if not isinstance(ret, dict):
            raise AquaChemistryError(
                "Algorithm run result should be a dictionary")

        convert_json_to_dict(ret)
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug('Algorithm returned: {}'.format(
                pprint.pformat(ret, indent=4)))

        print('Output:')
        if isinstance(ret, dict):
            for k, v in ret.items():
                print("'{}': {}".format(k, v))
        else:
            print(ret)

        return ret
Пример #5
0
def main():
    parser = argparse.ArgumentParser(description='Qiskit Aqua Command Line Tool')
    parser.add_argument('input', 
                        metavar='input', 
                        help='Algorithm JSON input file')
    parser.add_argument('-jo', 
                        metavar='output', 
                        help='Algorithm JSON output file name',
                        required=False)
    
    args = parser.parse_args()
    
    preferences = Preferences()
    if preferences.get_logging_config() is None:
        logging_config = build_logging_config(['qiskit_aqua'], logging.INFO)
        preferences.set_logging_config(logging_config)
        preferences.save()
    
    set_logger_config(preferences.get_logging_config())
    
    params = None
    with open(args.input) as json_file:
        params = json.load(json_file)
    
    ret = run_algorithm(params, None, True)
    
    if args.jo is not None:
        with open(args.jo, 'w') as f:
            print('{}'.format(ret), file=f)
    else:
        convert_json_to_dict(ret)
        print('\n\n--------------------------------- R E S U L T ------------------------------------\n')
        if isinstance(ret,dict):
            for k, v in ret.items():
                print("'{}': {}".format(k, v))
        else:
            print(ret)