Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 1 addition & 48 deletions qiskit_ibm_runtime/accounts/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
)
_QISKITRC_CONFIG_FILE = os.path.join(os.path.expanduser("~"), ".qiskit", "qiskitrc")
_DEFAULT_ACCOUNT_NAME = "default"
_DEFAULT_ACCOUNT_NAME_LEGACY = "default-legacy"
_DEFAULT_ACCOUNT_NAME_CLOUD = "default-cloud"
_DEFAULT_ACCOUNT_NAME_IBM_QUANTUM = "default-ibm-quantum"
_DEFAULT_ACCOUNT_NAME_IBM_CLOUD = "default-ibm-cloud"
_DEFAULT_CHANNEL_TYPE: ChannelType = "ibm_cloud"
Expand All @@ -52,7 +50,6 @@ def save(
channel_strategy: Optional[str] = None,
) -> None:
"""Save account on disk."""
cls.migrate(filename=filename)
channel = channel or os.getenv("QISKIT_IBM_CHANNEL") or _DEFAULT_CHANNEL_TYPE
name = name or cls._get_default_account_name(channel)
filename = filename if filename else _DEFAULT_ACCOUNT_CONFIG_JSON_FILE
Expand Down Expand Up @@ -84,7 +81,6 @@ def list(
"""List all accounts in a given filename, or in the default account file."""
filename = filename if filename else _DEFAULT_ACCOUNT_CONFIG_JSON_FILE
filename = os.path.expanduser(filename)
AccountManager.migrate(filename)

def _matching_name(account_name: str) -> bool:
return name is None or name == account_name
Expand Down Expand Up @@ -139,7 +135,7 @@ def get(

Args:
filename: Full path of the file from which to get the account.
name: Account name. Takes precedence if `auth` is also specified.
name: Account name.
channel: Channel type.

Returns:
Expand All @@ -150,7 +146,6 @@ def get(
"""
filename = filename if filename else _DEFAULT_ACCOUNT_CONFIG_JSON_FILE
filename = os.path.expanduser(filename)
cls.migrate(filename)
if name:
saved_account = read_config(filename=filename, name=name)
if not saved_account:
Expand Down Expand Up @@ -194,54 +189,12 @@ def delete(
"""Delete account from disk."""
filename = filename if filename else _DEFAULT_ACCOUNT_CONFIG_JSON_FILE
filename = os.path.expanduser(filename)
cls.migrate(filename=filename)
name = name or cls._get_default_account_name(channel)
return delete_config(
filename=filename,
name=name,
)

@classmethod
def migrate(cls, filename: Optional[str] = None) -> None:
"""Migrate accounts on disk by removing `auth` and adding `channel`."""
filename = filename if filename else _DEFAULT_ACCOUNT_CONFIG_JSON_FILE
filename = os.path.expanduser(filename)
data = read_config(filename=filename)
for key, value in data.items():
if key == _DEFAULT_ACCOUNT_NAME_CLOUD:
value.pop("auth", None)
value.update(channel="ibm_cloud")
delete_config(filename=filename, name=key)
save_config(
filename=filename,
name=_DEFAULT_ACCOUNT_NAME_IBM_CLOUD,
config=value,
overwrite=False,
)
elif key == _DEFAULT_ACCOUNT_NAME_LEGACY:
value.pop("auth", None)
value.update(channel="ibm_quantum")
delete_config(filename=filename, name=key)
save_config(
filename=filename,
name=_DEFAULT_ACCOUNT_NAME_IBM_QUANTUM,
config=value,
overwrite=False,
)
else:
if isinstance(value, dict) and "auth" in value:
if value["auth"] == "cloud":
value.update(channel="ibm_cloud")
elif value["auth"] == "legacy":
value.update(channel="ibm_quantum")
value.pop("auth", None)
save_config(
filename=filename,
name=key,
config=value,
overwrite=True,
)

@classmethod
def _from_env_variables(cls, channel: Optional[ChannelType]) -> Optional[Account]:
"""Read account from environment variable."""
Expand Down
34 changes: 7 additions & 27 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from qiskit_ibm_provider.utils.backend_decoder import configuration_from_server_data
from qiskit_ibm_runtime import ibm_backend

from .accounts import AccountManager, Account, AccountType, ChannelType
from .accounts import AccountManager, Account, ChannelType
from .api.clients import AuthClient, VersionClient
from .api.clients.runtime import RuntimeClient
from .api.exceptions import RequestsApiError
Expand Down Expand Up @@ -230,7 +230,6 @@ def _discover_account(
url: Optional[str] = None,
instance: Optional[str] = None,
channel: Optional[ChannelType] = None,
auth: Optional[AccountType] = None,
filename: Optional[str] = None,
name: Optional[str] = None,
proxies: Optional[ProxyConfiguration] = None,
Expand All @@ -250,27 +249,24 @@ def _discover_account(
)
if name:
if filename:
if any([auth, channel, token, url]):
if any([channel, token, url]):
logger.warning(
"Loading account from file %s with name %s. Any input 'auth', "
"Loading account from file %s with name %s. Any input "
"'channel', 'token' or 'url' are ignored.",
filename,
name,
)
else:
if any([auth, channel, token, url]):
if any([channel, token, url]):
logger.warning(
"Loading account with name %s. Any input 'auth', "
"Loading account with name %s. Any input "
"'channel', 'token' or 'url' are ignored.",
name,
)
account = AccountManager.get(filename=filename, name=name)
elif auth or channel:
if auth and auth not in ["legacy", "cloud"]:
raise ValueError("'auth' can only be 'cloud' or 'legacy'")
elif channel:
if channel and channel not in ["ibm_cloud", "ibm_quantum"]:
raise ValueError("'channel' can only be 'ibm_cloud' or 'ibm_quantum'")
channel = channel or self._get_channel_for_auth(auth=auth)
if token:
account = Account(
channel=channel,
Expand All @@ -288,7 +284,7 @@ def _discover_account(
elif any([token, url]):
# Let's not infer based on these attributes as they may change in the future.
raise ValueError(
"'channel' or 'auth' is required if 'token', or 'url' is specified but 'name' is not."
"'channel' is required if 'token', or 'url' is specified but 'name' is not."
)

if account is None:
Expand Down Expand Up @@ -681,13 +677,6 @@ def delete_account(
"""
return AccountManager.delete(filename=filename, name=name, channel=channel)

@staticmethod
def _get_channel_for_auth(auth: str) -> str:
"""Returns channel type based on auth"""
if auth == "legacy":
return "ibm_quantum"
return "ibm_cloud"

@staticmethod
def save_account(
token: Optional[str] = None,
Expand Down Expand Up @@ -1510,15 +1499,6 @@ def instances(self) -> List[str]:
return list(self._hgps.keys())
return []

@property
def auth(self) -> str:
"""Return the authentication type used.

Returns:
The authentication type used.
"""
return "cloud" if self._channel == "ibm_cloud" else "legacy"

@property
def channel(self) -> str:
"""Return the channel type used.
Expand Down
Loading