From 91c5a58935816c4b2af93eb269690ba5d62a1466 Mon Sep 17 00:00:00 2001 From: merav-aharoni Date: Tue, 19 Dec 2023 01:07:50 +0200 Subject: [PATCH] Removed support for qiskitrc file (#1121) * Removed support for qiskitrc file * lint --------- Co-authored-by: Kevin Tian --- qiskit_ibm_runtime/accounts/management.py | 41 +---------------------- qiskit_ibm_runtime/accounts/storage.py | 11 ------ test/account.py | 25 +------------- test/unit/test_account.py | 23 ------------- 4 files changed, 2 insertions(+), 98 deletions(-) diff --git a/qiskit_ibm_runtime/accounts/management.py b/qiskit_ibm_runtime/accounts/management.py index f230e40a5..4992998a6 100644 --- a/qiskit_ibm_runtime/accounts/management.py +++ b/qiskit_ibm_runtime/accounts/management.py @@ -13,20 +13,17 @@ """Account management related classes and functions.""" import os -import ast from typing import Optional, Dict from qiskit_ibm_provider.proxies import ProxyConfiguration -from qiskit_ibm_runtime.utils.deprecation import issue_deprecation_msg from .exceptions import AccountNotFoundError from .account import Account, ChannelType -from .storage import save_config, read_config, delete_config, read_qiskitrc +from .storage import save_config, read_config, delete_config _DEFAULT_ACCOUNT_CONFIG_JSON_FILE = os.path.join( os.path.expanduser("~"), ".qiskit", "qiskit-ibm.json" ) -_QISKITRC_CONFIG_FILE = os.path.join(os.path.expanduser("~"), ".qiskit", "qiskitrc") _DEFAULT_ACCOUNT_NAME = "default" _DEFAULT_ACCOUNT_NAME_IBM_QUANTUM = "default-ibm-quantum" _DEFAULT_ACCOUNT_NAME_IBM_CLOUD = "default-ibm-cloud" @@ -196,15 +193,6 @@ def get( if account_name in all_config: return Account.from_saved_format(all_config[account_name]) - if os.path.isfile(_QISKITRC_CONFIG_FILE): - issue_deprecation_msg( - msg="Use of the ~/.qiskit/qiskitrc.json file is deprecated.", - version="0.15.0", - remedy="Please use the ~/.qiskit/qiskit-ibm.json file instead.", - period="1 month", - ) - return cls._from_qiskitrc_file() - raise AccountNotFoundError("Unable to find account.") @classmethod @@ -272,30 +260,3 @@ def _get_default_account_name(cls, channel: ChannelType) -> str: if channel == "ibm_quantum" else _DEFAULT_ACCOUNT_NAME_IBM_CLOUD ) - - @classmethod - def _from_qiskitrc_file(cls) -> Optional[Account]: - """Read account from qiskitrc file.""" - qiskitrc_data = read_qiskitrc(_QISKITRC_CONFIG_FILE) - proxies = ( - ProxyConfiguration(ast.literal_eval(qiskitrc_data["proxies"])) - if "proxies" in qiskitrc_data - else None - ) - save_config( - filename=_DEFAULT_ACCOUNT_CONFIG_JSON_FILE, - name=_DEFAULT_ACCOUNT_NAME_IBM_QUANTUM, - overwrite=False, - config=Account.create_account( - token=qiskitrc_data.get("token", None), - url=qiskitrc_data.get("url", None), - instance=qiskitrc_data.get("default_provider", None), - verify=bool(qiskitrc_data.get("verify", None)), - proxies=proxies, - channel="ibm_quantum", - ) - .validate() - .to_saved_format(), - ) - default_config = read_config(filename=_DEFAULT_ACCOUNT_CONFIG_JSON_FILE) - return Account.from_saved_format(default_config[_DEFAULT_ACCOUNT_NAME_IBM_QUANTUM]) diff --git a/qiskit_ibm_runtime/accounts/storage.py b/qiskit_ibm_runtime/accounts/storage.py index 256432997..127d92a81 100644 --- a/qiskit_ibm_runtime/accounts/storage.py +++ b/qiskit_ibm_runtime/accounts/storage.py @@ -16,7 +16,6 @@ import logging import os from typing import Optional, Dict -from configparser import ConfigParser from .exceptions import AccountAlreadyExistsError logger = logging.getLogger(__name__) @@ -58,16 +57,6 @@ def save_config( json.dump(data, json_out, sort_keys=True, indent=4) -def read_qiskitrc(qiskitrc_config_file: str) -> Dict[str, str]: - """Read credentials from a qiskitrc config and return as a dictionary.""" - config_parser = ConfigParser() - config_parser.read(qiskitrc_config_file) - account_data = {} - for name in config_parser.sections(): - account_data = dict(config_parser.items(name)) - return account_data - - def read_config( filename: str, name: Optional[str] = None, diff --git a/test/account.py b/test/account.py index 65da49c73..65a5c5453 100644 --- a/test/account.py +++ b/test/account.py @@ -121,29 +121,6 @@ def __exit__(self, *exc): management._DEFAULT_ACCOUNT_CONFIG_JSON_FILE = self.account_config_json_backup -class custom_qiskitrc(ContextDecorator): - """Context manager that uses a temporary qiskitrc.""" - - # pylint: disable=invalid-name - - def __init__(self, contents=b""): - # Create a temporary file with the contents. - self.tmp_file = NamedTemporaryFile() - self.tmp_file.write(contents) - self.tmp_file.flush() - self.default_qiskitrc_file_original = management._QISKITRC_CONFIG_FILE - - def __enter__(self): - # Temporarily modify the default location of the qiskitrc file. - management._QISKITRC_CONFIG_FILE = self.tmp_file.name - return self - - def __exit__(self, *exc): - # Delete the temporary file and restore the default location. - self.tmp_file.close() - management._QISKITRC_CONFIG_FILE = self.default_qiskitrc_file_original - - def get_account_config_contents( name=None, channel="ibm_cloud", @@ -154,7 +131,7 @@ def get_account_config_contents( proxies=None, set_default=None, ): - """Generate qiskitrc content""" + """Generate account config file content""" if instance is None: instance = "some_instance" if channel == "ibm_cloud" else "hub/group/project" token = token or uuid.uuid4().hex diff --git a/test/unit/test_account.py b/test/unit/test_account.py index 90605a88f..5c0494784 100644 --- a/test/unit/test_account.py +++ b/test/unit/test_account.py @@ -18,7 +18,6 @@ import uuid from typing import Any from unittest import skipIf -import warnings from qiskit_ibm_provider.proxies import ProxyConfiguration from qiskit_ibm_runtime.accounts import ( @@ -38,7 +37,6 @@ from ..account import ( get_account_config_contents, temporary_account_config_file, - custom_qiskitrc, no_envs, custom_envs, ) @@ -845,27 +843,6 @@ def test_enable_account_by_name_input_instance(self): self.assertTrue(service._account) self.assertEqual(service._account.instance, instance) - @no_envs(["QISKIT_IBM_TOKEN"]) - def test_enable_account_by_qiskitrc(self): - """Test initializing account by a qiskitrc file.""" - token = "token-x" - proxies = {"urls": {"https": "localhost:8080"}} - str_contents = f""" - [ibmq] - token = {token} - url = https://auth.quantum-computing.ibm.com/api - verify = True - default_provider = ibm-q/open/main - proxies = {proxies} - """ - with custom_qiskitrc(contents=str.encode(str_contents)): - with temporary_account_config_file(contents={}): - with warnings.catch_warnings(record=True) as warn: - service = FakeRuntimeService() - self.assertIn("Use of the ~/.qiskit/qiskitrc.json file is deprecated", str(warn[0].message)) - self.assertTrue(service._account) - self.assertEqual(service._account.token, token) - def test_enable_account_by_channel_input_instance(self): """Test initializing account by channel and input instance.""" instance = uuid.uuid4().hex