diff --git a/azure-pipelines.yml b/azure-pipelines.yml index fd2fd1ee6c63..7e1209b6755d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -129,6 +129,8 @@ stages: matrix: Python37: python.version: '3.7' + variables: + QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y steps: - task: UsePythonVersion@0 inputs: @@ -235,6 +237,8 @@ stages: matrix: Python37: python.version: '3.7' + variables: + QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y steps: - task: UsePythonVersion@0 inputs: @@ -295,6 +299,8 @@ stages: python.version: '3.7' Python38: python.version: '3.8' + variables: + QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y steps: - task: UsePythonVersion@0 inputs: @@ -350,6 +356,8 @@ stages: python.version: '3.5' Python38: python.version: '3.8' + variables: + QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y steps: - task: UsePythonVersion@0 inputs: @@ -406,6 +414,8 @@ stages: python.version: '3.5' Python38: python.version: '3.8' + variables: + QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y steps: - task: UsePythonVersion@0 inputs: diff --git a/qiskit/__init__.py b/qiskit/__init__.py index dd12947b90da..37dad50e5a19 100644 --- a/qiskit/__init__.py +++ b/qiskit/__init__.py @@ -12,7 +12,7 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. -# pylint: disable=wrong-import-order +# pylint: disable=wrong-import-order,invalid-name,wrong-import-position """Main Qiskit public functionality.""" @@ -20,6 +20,7 @@ import pkgutil import sys import warnings +import os # First, check for required Python and API version from . import util @@ -32,6 +33,9 @@ from qiskit.circuit import QuantumRegister from qiskit.circuit import QuantumCircuit +# user config +from qiskit import user_config as _user_config + # The qiskit.extensions.x imports needs to be placed here due to the # mechanism for adding gates dynamically. import qiskit.extensions @@ -46,29 +50,37 @@ # Please note these are global instances, not modules. from qiskit.providers.basicaer import BasicAer +_config = _user_config.get_config() + # Try to import the Aer provider if installed. try: from qiskit.providers.aer import Aer except ImportError: - warnings.warn('Could not import the Aer provider from the qiskit-aer ' - 'package. Install qiskit-aer or check your installation.', - RuntimeWarning) + suppress_warnings = os.environ.get('QISKIT_SUPPRESS_PACKAGING_WARNINGS', '') + if suppress_warnings.upper() != 'Y': + if not _config.get('suppress_packaging_warnings') or suppress_warnings.upper() == 'N': + warnings.warn('Could not import the Aer provider from the qiskit-aer ' + 'package. Install qiskit-aer or check your installation.', + RuntimeWarning) # Try to import the IBMQ provider if installed. try: from qiskit.providers.ibmq import IBMQ except ImportError: - warnings.warn('Could not import the IBMQ provider from the ' - 'qiskit-ibmq-provider package. Install qiskit-ibmq-provider ' - 'or check your installation.', - RuntimeWarning) + suppress_warnings = os.environ.get('QISKIT_SUPPRESS_PACKAGING_WARNINGS', '') + if suppress_warnings.upper() != 'Y': + if not _config.get('suppress_packaging_warnings') or suppress_warnings.upper() == 'N': + warnings.warn('Could not import the IBMQ provider from the ' + 'qiskit-ibmq-provider package. Install ' + 'qiskit-ibmq-provider or check your installation.', + RuntimeWarning) # Moved to after IBMQ and Aer imports due to import issues # with other modules that check for IBMQ (tools) -from qiskit.execute import execute -from qiskit.compiler import transpile, assemble, schedule +from qiskit.execute import execute # noqa +from qiskit.compiler import transpile, assemble, schedule # noqa -from .version import __version__ -from .version import _get_qiskit_versions +from .version import __version__ # noqa +from .version import _get_qiskit_versions # noqa if sys.version_info[0] == 3 and sys.version_info[1] == 5: diff --git a/qiskit/user_config.py b/qiskit/user_config.py index e0f9822ad28e..25a39ed88afc 100644 --- a/qiskit/user_config.py +++ b/qiskit/user_config.py @@ -88,6 +88,11 @@ def read_config_file(self): "0, 1, 2, or 3.") self.settings['transpile_optimization_level'] = ( transpile_optimization_level) + # Parse package warnings + package_warnings = self.config_parser.getboolean( + 'default', 'suppress_packaging_warnings', fallback=False) + if package_warnings: + self.settings['suppress_packaging_warnings'] = package_warnings def get_config(): diff --git a/releasenotes/notes/suppress-packaging-warnings-396b38feb6b3d52f.yaml b/releasenotes/notes/suppress-packaging-warnings-396b38feb6b3d52f.yaml new file mode 100644 index 000000000000..3ee97941cbe2 --- /dev/null +++ b/releasenotes/notes/suppress-packaging-warnings-396b38feb6b3d52f.yaml @@ -0,0 +1,25 @@ +--- +features: + - | + A new environment variable ``QISKIT_SUPPRESS_PACKAGING_WARNINGS`` can be + set to ``Y`` or ``y`` which will suppress the warnings about + ``qiskit-aer`` and ``qiskit-ibmq-provider`` not being installed at import + time. This is useful for users who are only running qiskit-terra (or just + not qiskit-aer and/or qiskit-ibmq-provider) and the warnings are not an + indication of a potential packaging problem. You can set the environment + variable to ``N`` or ``n`` to ensure that warnings are always enabled + even if the user config file is set to disable them. + - | + A new user config file option, ``suppress_packaging_warnings`` has been + added. When set to ``true`` in your user config file like:: + + [default] + suppress_packaging_warnings = true + + it will suppress the warnings about ``qiskit-aer`` and + ``qiskit-ibmq-provider`` not being installed at import time. This is useful + for users who are only running qiskit-terra (or just not qiskit-aer and/or + qiskit-ibmq-provider) and the warnings are not an indication of a potential + packaging problem. If the user config file is set to disable the warnings + this can be overriden by setting the ``QISKIT_SUPPRESS_PACKAGING_WARNINGS`` + to ``N`` or ``n`` diff --git a/test/python/test_user_config.py b/test/python/test_user_config.py index 13b8e2acea22..e87c4a5a82a6 100644 --- a/test/python/test_user_config.py +++ b/test/python/test_user_config.py @@ -32,6 +32,18 @@ def test_empty_file_read(self): config.read_config_file() self.assertEqual({}, config.settings) + def test_invalid_suppress_packaging_warnings(self): + test_config = """ + [default] + suppress_packaging_warnings = 76 + """ + self.addCleanup(os.remove, self.file_path) + with open(self.file_path, 'w') as file: + file.write(test_config) + file.flush() + config = user_config.UserConfig(self.file_path) + self.assertRaises(ValueError, config.read_config_file) + def test_invalid_optimization_level(self): test_config = """ [default] @@ -88,12 +100,41 @@ def test_optimization_level_valid(self): self.assertEqual({'transpile_optimization_level': 1}, config.settings) + def test_valid_suppress_packaging_warnings_false(self): + test_config = """ + [default] + suppress_packaging_warnings = false + """ + self.addCleanup(os.remove, self.file_path) + with open(self.file_path, 'w') as file: + file.write(test_config) + file.flush() + config = user_config.UserConfig(self.file_path) + config.read_config_file() + self.assertEqual({}, + config.settings) + + def test_valid_suppress_packaging_warnings_true(self): + test_config = """ + [default] + suppress_packaging_warnings = true + """ + self.addCleanup(os.remove, self.file_path) + with open(self.file_path, 'w') as file: + file.write(test_config) + file.flush() + config = user_config.UserConfig(self.file_path) + config.read_config_file() + self.assertEqual({'suppress_packaging_warnings': True}, + config.settings) + def test_all_options_valid(self): test_config = """ [default] circuit_drawer = latex circuit_mpl_style = default transpile_optimization_level = 3 + suppress_packaging_warnings = true """ self.addCleanup(os.remove, self.file_path) with open(self.file_path, 'w') as file: @@ -103,4 +144,6 @@ def test_all_options_valid(self): config.read_config_file() self.assertEqual({'circuit_drawer': 'latex', 'circuit_mpl_style': 'default', - 'transpile_optimization_level': 3}, config.settings) + 'transpile_optimization_level': 3, + 'suppress_packaging_warnings': True}, + config.settings) diff --git a/tox.ini b/tox.ini index 7e4a6bde2cd7..84148ae59c8a 100644 --- a/tox.ini +++ b/tox.ini @@ -11,6 +11,7 @@ setenv = LANGUAGE=en_US LC_ALL=en_US.utf-8 ARGS="-V" + QISKIT_SUPRESS_PACKAGING_WARNINGS=Y deps = -r{toxinidir}/requirements.txt -r{toxinidir}/requirements-dev.txt commands =