def apply(self): try: if self._credentialsview: from qiskit_aqua_cmd import Preferences from qiskit.aqua import disable_ibmq_account preferences = Preferences() disable_ibmq_account(preferences.get_url(), preferences.get_token(), preferences.get_proxies({})) self._credentialsview.apply(preferences) preferences.save() level_name = self._levelCombo.get() levels = [ key for key, value in PreferencesDialog._LOG_LEVELS.items() if value == level_name ] loglevel = levels[0] logging_config = self._guiprovider.build_logging_config(loglevel) preferences = self._guiprovider.create_preferences() preferences.set_logging_config(logging_config) preferences.save() self._guiprovider.set_logging_config(logging_config) populate = self._populateDefaults.get() preferences = self._guiprovider.create_uipreferences() preferences.set_populate_defaults(False if populate == 0 else True) preferences.save() self._controller.model.get_available_providers() except Exception as e: self.controller.outputview.write_line(str(e))
def _load_provider(provider_name): index = provider_name.rfind(".") if index < 1: raise ImportError("Invalid provider name '{}'".format(provider_name)) modulename = provider_name[0:index] objectname = provider_name[index + 1:len(provider_name)] module = importlib.import_module(modulename) if module is None: raise ImportError( "Failed to import provider '{}'".format(provider_name)) provider_object = getattr(module, objectname) if provider_object is None: raise ImportError( "Failed to import provider '{}'".format(provider_name)) if provider_object == IBMQ: # enable IBMQ account preferences = Preferences() enable_ibmq_account(preferences.get_url(), preferences.get_token(), preferences.get_proxies({})) return provider_object
def get_backends_from_provider(provider_name): """ Backends access method. Args: provider_name (str): Fullname of provider instance global property or class Returns: list: backend names Raises: ImportError: Invalid provider name or failed to find provider """ provider_object = _load_provider(provider_name) if provider_object == IBMQ: preferences = Preferences() url = preferences.get_url() token = preferences.get_token() kwargs = {} if url is not None and url != '': kwargs['url'] = url if token is not None and token != '': kwargs['token'] = token return [x.name() for x in provider_object.backends(**kwargs) if x.name() not in _UNSUPPORTED_BACKENDS] try: # try as variable containing provider instance return [x.name() for x in provider_object.backends() if x.name() not in _UNSUPPORTED_BACKENDS] except: # try as provider class then try: provider_instance = provider_object() return [x.name() for x in provider_instance.backends() if x.name() not in _UNSUPPORTED_BACKENDS] except: pass raise ImportError("'Backends not found for provider '{}'".format(provider_object))
def get_backend_from_provider(provider_name, backend_name): """ Backend access method. Args: provider_name (str): Fullname of provider instance global property or class backend_name (str): name of backend for this provider Returns: BaseBackend: backend object Raises: ImportError: Invalid provider name or failed to find provider """ backend = None provider_object = _load_provider(provider_name) if has_ibmq() and isinstance(provider_object, IBMQProvider): from qiskit_aqua_cmd import Preferences preferences = Preferences() url = preferences.get_url() token = preferences.get_token() kwargs = {} if url is not None and url != '': kwargs['url'] = url if token is not None and token != '': kwargs['token'] = token backend = provider_object.get_backend(backend_name, **kwargs) else: try: # try as variable containing provider instance backend = provider_object.get_backend(backend_name) except: # try as provider class then try: provider_instance = provider_object() backend = provider_instance.get_backend(backend_name) except: pass if backend is None: raise ImportError("'{} not found in provider '{}'".format( backend_name, provider_object)) return backend
def register_and_get_operational_backends(): # update registration info using internal methods because: # at this point I don't want to save to or remove credentials from disk # I want to update url, proxies etc without removing token and # re-adding in 2 methods ibmq_backends = [] try: credentials = None preferences = Preferences() url = preferences.get_url() token = preferences.get_token() if url is not None and url != '' and token is not None and token != '': credentials = Credentials(token, url, proxies=preferences.get_proxies({})) if credentials is not None: IBMQ._accounts[credentials.unique_id()] = IBMQSingleProvider( credentials, IBMQ) logger.debug("Registered with Qiskit successfully.") ibmq_backends = [x.name() for x in IBMQ.backends(url=url, token=token)] except Exception as e: logger.debug( "Failed to register with Qiskit: {}".format(str(e))) backends = set() aer_backends = [x.name() for x in Aer.backends()] for aer_backend in aer_backends: backend = aer_backend supported = True for unsupported_backend in QuantumInstance.UNSUPPORTED_BACKENDS: if backend.startswith(unsupported_backend): supported = False break if supported: backends.add(backend) return list(backends) + ibmq_backends