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
def body(self, parent, options): from qiskit_aqua._logging import (get_logging_level, set_logging_config) from qiskit_aqua_cmd import Preferences preferences = Preferences() logging_config = preferences.get_logging_config() if logging_config is not None: set_logging_config(logging_config) uipreferences = UIPreferences() populate = uipreferences.get_populate_defaults(True) self._populateDefaults.set(1 if populate else 0) credentialsGroup = ttk.LabelFrame(parent, text='IBMQ Credentials', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) credentialsGroup.grid(padx=(7, 7), pady=6, row=0, column=0, sticky='nsew') self._credentialsview = CredentialsView(credentialsGroup) defaultsGroup = ttk.LabelFrame(parent, text='Defaults', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) defaultsGroup.grid(padx=(7, 7), pady=6, row=1, column=0, sticky='nsw') defaultsGroup.columnconfigure(1, pad=7) self._checkButton = ttk.Checkbutton(defaultsGroup, text="Populate on file new/open", variable=self._populateDefaults) self._checkButton.grid(row=0, column=1, sticky='nsw') packagesGroup = ttk.LabelFrame(parent, text='Packages', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) packagesGroup.grid(padx=(7, 7), pady=6, row=2, column=0, sticky='nsw') packagesGroup.columnconfigure(1, pad=7) frame = ttk.Frame(packagesGroup) frame.grid(row=0, column=0, sticky='nsew') self._packagesPage = PackagesPage(frame, preferences) self._packagesPage.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.TRUE) self._packagesPage.show_add_button(True) self._packagesPage.show_remove_button( self._packagesPage.has_selection()) self._packagesPage.show_defaults_button(False) loggingGroup = ttk.LabelFrame(parent, text='Logging Configuration', padding=(6, 6, 6, 6), borderwidth=4, relief=tk.GROOVE) loggingGroup.grid(padx=(7, 7), pady=6, row=3, column=0, sticky='nsw') loggingGroup.columnconfigure(1, pad=7) loglevel = get_logging_level() ttk.Label(loggingGroup, text="Level:", borderwidth=0, anchor=tk.E).grid(row=0, column=0, sticky='nsew') self._levelCombo = ttk.Combobox(loggingGroup, exportselection=0, state='readonly', values=list(PreferencesDialog._LOG_LEVELS.values())) index = list(PreferencesDialog._LOG_LEVELS.keys()).index(loglevel) self._levelCombo.current(index) self._levelCombo.grid(row=0, column=1, sticky='nsw') self.entry = self._credentialsview.initial_focus return self.entry # initial focus
def main(): from qiskit.aqua._logging import (get_logging_level, build_logging_config, set_logging_config, set_aqua_logging) from qiskit_aqua_cmd import Preferences from qiskit.aqua import run_algorithm from qiskit.aqua.utils import convert_json_to_dict preferences = Preferences() _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_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) 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)