From 46286a65e644916cec07d2b07c35c43a1a0449dc Mon Sep 17 00:00:00 2001 From: "Diego M. Rodriguez" Date: Fri, 20 Jul 2018 11:17:17 +0200 Subject: [PATCH] Use full class name for account name, spelling Fixes after review: * use the full class name (module + class) for the account name, in order to ensure it is unique, moving the calculation to its own function for reusability. * spelling and other fixes. --- README.md | 2 +- doc/install.rst | 2 +- qiskit/wrapper/_wrapper.py | 3 ++- qiskit/wrapper/credentials/__init__.py | 1 + qiskit/wrapper/credentials/_configrc.py | 12 ++++++++---- qiskit/wrapper/credentials/_environ.py | 4 ++-- qiskit/wrapper/credentials/_qconfig.py | 3 ++- qiskit/wrapper/credentials/_utils.py | 25 +++++++++++++++++++++++++ test/python/common.py | 10 ++++++---- test/python/test_registration.py | 17 +++++++++++------ 10 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 qiskit/wrapper/credentials/_utils.py diff --git a/README.md b/README.md index c5606bf93128..992bf10ca1aa 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ your IBM Q Experience account: page to `store_credentials`. After calling `store_credentials()`, your credentials will be stored into disk. -Once they are stored, you can automatically load and use them in your program +Once they are stored, Qiskit will automatically load and use them in your program via: ```python diff --git a/doc/install.rst b/doc/install.rst index c02623664e6a..c5969ba79156 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -143,7 +143,7 @@ following contents: And customize the following lines: * copy/paste your API token into the space between the quotation marks on the - first line (``APItoken = 'PUT_YOUR_API_TOKEN_HERE``). + first line (``APItoken = 'PUT_YOUR_API_TOKEN_HERE'``). * if you have access to the IBM Q features, you also need to setup the values for your url, hub, group, and project. You can do so by filling the ``config`` variable with the values you can find on your IBM Q account diff --git a/qiskit/wrapper/_wrapper.py b/qiskit/wrapper/_wrapper.py index 954637f9809a..78fdcb21268a 100644 --- a/qiskit/wrapper/_wrapper.py +++ b/qiskit/wrapper/_wrapper.py @@ -63,7 +63,8 @@ def register(*args, provider_class=IBMQProvider, **kwargs): """ # Try to autodiscover credentials if not passed. if not args and not kwargs and provider_class == IBMQProvider: - kwargs = credentials.discover_credentials().get(IBMQProvider.__name__) or {} + kwargs = credentials.discover_credentials().get( + credentials.get_account_name(IBMQProvider)) or {} if not kwargs: raise QISKitError( 'No IBMQ credentials found. Please pass them explicitly or ' diff --git a/qiskit/wrapper/credentials/__init__.py b/qiskit/wrapper/credentials/__init__.py index ca205e43d804..3028bcc673e4 100644 --- a/qiskit/wrapper/credentials/__init__.py +++ b/qiskit/wrapper/credentials/__init__.py @@ -14,6 +14,7 @@ from ._configrc import read_credentials_from_qiskitrc, store_credentials from ._environ import read_credentials_from_environ from ._qconfig import read_credentials_from_qconfig +from ._utils import get_account_name logger = logging.getLogger(__name__) diff --git a/qiskit/wrapper/credentials/_configrc.py b/qiskit/wrapper/credentials/_configrc.py index ea1b68fd973b..4427124f2829 100644 --- a/qiskit/wrapper/credentials/_configrc.py +++ b/qiskit/wrapper/credentials/_configrc.py @@ -15,6 +15,7 @@ from qiskit import QISKitError from qiskit.backends.ibmq import IBMQProvider +from ._utils import get_account_name DEFAULT_QISKITRC_FILE = os.path.join(os.path.expanduser("~"), @@ -51,11 +52,14 @@ def read_credentials_from_qiskitrc(filename=None): credentials_dict = {} for name in config_parser.sections(): single_credentials = dict(config_parser.items(name)) - # TODO: 'proxies' is the only value that is a dict. Consider moving to - # json configuration or splitting into single keys manually. + # Individually convert keys to their right types. + # TODO: consider generalizing, moving to json configuration or a more + # robust alternative. if 'proxies' in single_credentials.keys(): single_credentials['proxies'] = literal_eval( single_credentials['proxies']) + if 'verify' in single_credentials.keys(): + single_credentials['verify'] = bool(single_credentials['verify']) credentials_dict[name] = single_credentials return credentials_dict @@ -100,7 +104,7 @@ def store_credentials(provider_class=IBMQProvider, overwrite=False, the account_name could not be assigned. """ # Set the name of the Provider from the class. - account_name = provider_class.__name__ + account_name = get_account_name(provider_class) # Read the current providers stored in the configuration file. filename = filename or DEFAULT_QISKITRC_FILE credentials = read_credentials_from_qiskitrc(filename) @@ -127,7 +131,7 @@ def remove_credentials(provider_class=IBMQProvider, filename=None): file. """ # Set the name of the Provider from the class. - account_name = provider_class.__name__ + account_name = get_account_name(provider_class) credentials = read_credentials_from_qiskitrc(filename) try: diff --git a/qiskit/wrapper/credentials/_environ.py b/qiskit/wrapper/credentials/_environ.py index c344f3d21445..9f22e32ac093 100644 --- a/qiskit/wrapper/credentials/_environ.py +++ b/qiskit/wrapper/credentials/_environ.py @@ -12,7 +12,7 @@ import os from qiskit.backends.ibmq import IBMQProvider - +from ._utils import get_account_name # Dictionary that maps `ENV_VARIABLE_NAME` to credential parameter. VARIABLES_MAP = { @@ -44,4 +44,4 @@ def read_credentials_from_environ(): if os.getenv(envar_name): credentials[credential_key] = os.getenv(envar_name) - return {IBMQProvider.__name__: credentials} + return {get_account_name(IBMQProvider): credentials} diff --git a/qiskit/wrapper/credentials/_qconfig.py b/qiskit/wrapper/credentials/_qconfig.py index 6e67c4163466..0951529d0a59 100644 --- a/qiskit/wrapper/credentials/_qconfig.py +++ b/qiskit/wrapper/credentials/_qconfig.py @@ -14,6 +14,7 @@ from qiskit import QISKitError from qiskit.backends.ibmq import IBMQProvider +from ._utils import get_account_name DEFAULT_QCONFIG_FILE = 'Qconfig.py' @@ -59,4 +60,4 @@ def read_credentials_from_qconfig(): # pylint: disable=broad-except raise QISKitError('Error loading Qconfig.py: %s' % str(ex)) - return {IBMQProvider.__name__: credentials} + return {get_account_name(IBMQProvider): credentials} diff --git a/qiskit/wrapper/credentials/_utils.py b/qiskit/wrapper/credentials/_utils.py new file mode 100644 index 000000000000..17ad2d84b461 --- /dev/null +++ b/qiskit/wrapper/credentials/_utils.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018, IBM. +# +# This source code is licensed under the Apache License, Version 2.0 found in +# the LICENSE.txt file in the root directory of this source tree. + +""" +Utilities for credentials. +""" + + +def get_account_name(provider_class): + """ + Return the account name for a particular provider. This name is used by + Qiskit internally and in the configuration file and uniquely identifies + a provider. + + Args: + provider_class (class): class for the account. + + Returns: + str: the account name. + """ + return '{}.{}'.format(provider_class.__module__, provider_class.__name__) diff --git a/test/python/common.py b/test/python/common.py index 60f8c7fa4fdc..cd6b8d746204 100644 --- a/test/python/common.py +++ b/test/python/common.py @@ -15,7 +15,8 @@ import unittest from unittest.util import safe_repr from qiskit import __path__ as qiskit_path -from qiskit.wrapper.credentials import discover_credentials +from qiskit.backends.ibmq import IBMQProvider +from qiskit.wrapper.credentials import discover_credentials, get_account_name from qiskit.wrapper.defaultqiskitprovider import DefaultQISKitProvider @@ -249,9 +250,10 @@ def _(*args, **kwargs): _wrapper._DEFAULT_PROVIDER = DefaultQISKitProvider() # Attempt to read the standard credentials. + account_name = get_account_name(IBMQProvider) discovered_credentials = discover_credentials() - try: - credentials = next(iter(discovered_credentials.values())) + if account_name in discovered_credentials.keys(): + credentials = discovered_credentials[account_name] kwargs.update({ 'QE_TOKEN': credentials.get('token'), 'QE_URL': credentials.get('url'), @@ -259,7 +261,7 @@ def _(*args, **kwargs): 'group': credentials.get('group'), 'project': credentials.get('project'), }) - except StopIteration: + else: raise Exception('Could not locate valid credentials') return func(*args, **kwargs) diff --git a/test/python/test_registration.py b/test/python/test_registration.py index 8b597bc139bb..c121111677f9 100644 --- a/test/python/test_registration.py +++ b/test/python/test_registration.py @@ -19,15 +19,20 @@ import qiskit from qiskit import QISKitError from qiskit.backends.ibmq import IBMQProvider -from qiskit.wrapper.credentials import _configrc, _qconfig, discover_credentials +from qiskit.wrapper.credentials import (_configrc, _qconfig, + discover_credentials, get_account_name) from qiskit.wrapper.credentials._environ import VARIABLES_MAP from .common import QiskitTestCase # TODO: NamedTemporaryFiles do not support name in Windows @skipIf(os.name == 'nt', 'Test not supported in Windows') -class TestWrapperCredentuals(QiskitTestCase): +class TestWrapperCredentials(QiskitTestCase): """Wrapper autoregistration and credentials test case.""" + def setUp(self): + super(TestWrapperCredentials, self).setUp() + self.ibmq_account_name = get_account_name(IBMQProvider) + def test_autoregister_no_credentials(self): """Test register() with no credentials available.""" with no_file('Qconfig.py'), no_file(_configrc.DEFAULT_QISKITRC_FILE), no_envs(): @@ -73,8 +78,8 @@ def test_environ_over_qiskitrc(self): with no_file('Qconfig.py'), custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN'}): credentials = discover_credentials() - self.assertIn('IBMQProvider', credentials) - self.assertEqual(credentials['IBMQProvider']['token'], 'ENVIRON_TOKEN') + self.assertIn(self.ibmq_account_name, credentials) + self.assertEqual(credentials[self.ibmq_account_name]['token'], 'ENVIRON_TOKEN') def test_qconfig_over_all(self): """Test order, with qconfig""" @@ -85,8 +90,8 @@ def test_qconfig_over_all(self): custom_envs({'QE_TOKEN': 'ENVIRON_TOKEN'}): credentials = discover_credentials() - self.assertIn('IBMQProvider', credentials) - self.assertEqual(credentials['IBMQProvider']['token'], 'QCONFIG_TOKEN') + self.assertIn(self.ibmq_account_name, credentials) + self.assertEqual(credentials[self.ibmq_account_name]['token'], 'QCONFIG_TOKEN') # Context managers