def __init__(self, wires, provider=None, backend="ibmq_qasm_simulator", shots=1024, **kwargs): token = os.getenv("IBMQX_TOKEN") or kwargs.get("ibmqx_token", None) url = os.getenv("IBMQX_URL") or kwargs.get("ibmqx_url", None) # Specify a single hub, group and project hub = kwargs.get("hub", "ibm-q") group = kwargs.get("group", "open") project = kwargs.get("project", "main") if token is not None: # token was provided by the user, so attempt to enable an # IBM Q account manually ibmq_kwargs = {"url": url} if url is not None else {} IBMQ.enable_account(token, **ibmq_kwargs) else: # check if an IBM Q account is already active. # # * IBMQ v2 credentials stored in active_account(). # If no accounts are active, it returns None. if IBMQ.active_account() is None: # no active account try: # attempt to load a v2 account stored on disk IBMQ.load_account() except IBMQAccountError: # attempt to enable an account manually using # a provided token raise IBMQAccountError( "No active IBM Q account, and no IBM Q token provided." ) from None # IBM Q account is now enabled # get a provider p = provider or IBMQ.get_provider( hub=hub, group=group, project=project) super().__init__(wires=wires, provider=p, backend=backend, shots=shots, **kwargs)
def _wrapper(self, *args, **kwargs): # type: ignore # The special case of load_accounts is here for backward # compatibility when using v2 credentials. if self._credentials and func.__name__ != 'load_accounts': raise IBMQAccountError( 'IBMQ.{}() is not available when using an IBM Q Experience ' 'v2 account. Please use IBMQ.{}() (note the singular form) ' 'instead.'.format(func.__name__, func.__name__[:-1])) warnings.warn( 'IBMQ.{}() is being deprecated. Please use IBM Q Experience v2 ' 'credentials and IBMQ.{}() (note the singular form) instead. You can ' 'find the instructions to make the updates here: \n' 'https://github.com/Qiskit/qiskit-ibmq-provider#updating-to-the-new-ibm-q-experience' .format(func.__name__, func.__name__[:-1]), DeprecationWarning) return func(self, *args, **kwargs)
def __init__(self, wires, provider=None, backend="ibmq_qasm_simulator", shots=1024, **kwargs): token = os.getenv("IBMQX_TOKEN") or kwargs.get("ibmqx_token", None) url = os.getenv("IBMQX_URL") or kwargs.get("ibmqx_url", None) if token is not None: # token was provided by the user, so attempt to enable an # IBM Q account manually ibmq_kwargs = {"url": url} if url is not None else {} IBMQ.enable_account(token, **ibmq_kwargs) else: # turn off deprecation warnings # TODO: remove IBM Q v1 API calls when fully deprecated with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) # check if an IBM Q account is already active. # # * IBMQ v1 credentials stored in active_accounts(). # If no accounts are active, it returns [] # # * IBMQ v2 credentials stored in active_account(). # If no accounts are active, it returns None. if IBMQ.active_account() is None and not IBMQ.active_accounts(): # no active account try: # attempt to load a v1 account stored on disk IBMQ.load_accounts() except IBMQAccountError: try: # attempt to load a v2 account stored on disk IBMQ.load_account() except IBMQAccountError: # attempt to enable an account manually using # a provided token raise IBMQAccountError( "No active IBM Q account, and no IBM Q token provided." ) from None # IBM Q account is now enabled # get a provider p = provider or IBMQ.get_provider() super().__init__(wires=wires, provider=p, backend=backend, shots=shots, **kwargs)
def connect(kwargs): """Function that allows connection to IBMQ. Args: kwargs(dict): dictionary that contains the token and the url""" token = kwargs.get("ibmqx_token", None) or os.getenv("IBMQX_TOKEN") url = kwargs.get("ibmqx_url", None) or os.getenv("IBMQX_URL") # TODO: remove "no cover" when #173 is resolved if token is not None: # pragma: no cover # token was provided by the user, so attempt to enable an # IBM Q account manually def login(): ibmq_kwargs = {"url": url} if url is not None else {} IBMQ.enable_account(token, **ibmq_kwargs) active_account = IBMQ.active_account() if active_account is None: login() else: # There is already an active account: # If the token is the same, do nothing. # If the token is different, authenticate with the new account. if active_account["token"] != token: IBMQ.disable_account() login() else: # check if an IBM Q account is already active. # # * IBMQ v2 credentials stored in active_account(). # If no accounts are active, it returns None. if IBMQ.active_account() is None: # no active account try: # attempt to load a v2 account stored on disk IBMQ.load_account() except IBMQAccountError: # attempt to enable an account manually using # a provided token raise IBMQAccountError( "No active IBM Q account, and no IBM Q token provided." ) from None