예제 #1
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
예제 #2
0
def set_preferences_logging():
    """Update logging setting with latest external packages"""
    from qiskit.aqua._logging import get_logging_level, build_logging_config, set_logging_config
    guiprovider = AquaGUIProvider()
    preferences = guiprovider.create_uipreferences()
    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())
예제 #3
0
def set_preferences_logging():
    """
    Update logging setting with latest external packages
    """
    from qiskit.aqua._logging import get_logging_level, build_logging_config, set_logging_config
    from qiskit_aqua_cmd.preferences import Preferences
    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())
예제 #4
0
def _run():
    _check_extra_requires('console_scripts', 'qiskit_aqua_cmd')
    from qiskit.aqua._logging import (get_logging_level, build_logging_config,
                                      set_logging_config,
                                      set_qiskit_aqua_logging)
    from qiskit_aqua_interfaces.aqua.user_interface import UIPreferences
    from qiskit.aqua import run_algorithm
    from qiskit.aqua.utils import convert_json_to_dict

    preferences = UIPreferences()
    log_levels = OrderedDict([
        (logging.getLevelName(logging.CRITICAL).lower(), logging.CRITICAL),
        (logging.getLevelName(logging.ERROR).lower(), logging.ERROR),
        (logging.getLevelName(logging.WARNING).lower(), logging.WARNING),
        (logging.getLevelName(logging.INFO).lower(), logging.INFO),
        (logging.getLevelName(logging.DEBUG).lower(), logging.DEBUG),
        (logging.getLevelName(logging.NOTSET).lower(), logging.NOTSET)
    ])

    parser = argparse.ArgumentParser(
        prog='qiskit_aqua_cmd',
        formatter_class=argparse.RawTextHelpFormatter,
        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')
    parser.add_argument('-l',
                        metavar='logging',
                        choices=log_levels.keys(),
                        help=textwrap.dedent('''\
                            Logging level:
                            {}
                            (defaults to level from preferences file: {})
                             '''.format(list(log_levels.keys()),
                                        preferences.filepath)))

    args = parser.parse_args()

    if args.l is not None:
        set_qiskit_aqua_logging(log_levels.get(args.l, logging.INFO))
    else:
        # update logging setting with latest external packages
        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)

    print(APP_DEPRECATION_MSG)
    ret = run_algorithm(params, None, True)

    if args.jo is not None:
        with open(args.jo, 'w') as run_output:
            print('{}'.format(ret), file=run_output)
    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)