diff --git a/.pylintdict b/.pylintdict index 2c10337fa6..1af77d72ee 100644 --- a/.pylintdict +++ b/.pylintdict @@ -368,6 +368,7 @@ minibatched minibatches minibatching minima +minimizer minimumeigenoptimizationresult mintert minwidth diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 629b5c242e..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,392 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2018, 2020. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -notifications: - email: - if: fork = false AND type = cron - recipients: - - manoel.marques@ibm.com - on_success: never - on_failure: always - -cache: - pip: true - directories: - - .stestr -os: linux -dist: xenial - -language: python -python: 3.7 - -git: - depth: false - -env: - global: - - DEPENDENCY_BRANCH=$(if [[ "$TRAVIS_BRANCH" == stable* ]]; then echo "stable"; else echo "master"; fi) - - INIT_FILE="$TRAVIS_BUILD_DIR/qiskit/__init__.py" - -stage_dependencies: &stage_dependencies - before_install: - - | - if [ -f $INIT_FILE ]; then - # stops travis if __init__.py exists under qiskit - echo "File '$INIT_FILE' found. It should not exist, since this repo extends qiskit namespace."; - travis_terminate 1; - fi - - | - if [ "$INSTALL_PSI4" == "yes" ]; then - # Download and install miniconda psi4 - wget http://vergil.chemistry.gatech.edu/psicode-download/Psi4conda-1.3.2-py37-Linux-x86_64.sh -O miniconda.sh - bash miniconda.sh -b -p $HOME/miniconda - source "$HOME/miniconda/etc/profile.d/conda.sh" - conda activate - fi - - pip install -U 'pip<20.0.0' - - pip install -U setuptools wheel - - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test - - | - if [ "$DEPENDENCY_BRANCH" == "master" ]; then - pip install cython - # Installing qiskit-terra master branch... - pip install https://github.com/Qiskit/qiskit-terra/archive/master.zip --progress-bar off - # Installing qiskit-ignis master branch... - pip install https://github.com/Qiskit/qiskit-ignis/archive/master.zip --progress-bar off - # Installing qiskit-ibmq-provider master branch... - pip install https://github.com/Qiskit/qiskit-ibmq-provider/archive/master.zip --progress-bar off - # install qiskit-aer build dependencies - sudo apt-get -y update - sudo apt-get -y install g++-7 - sudo apt-get -y install libopenblas-dev - # Clone qiskit-aer - git clone --recurse-submodules https://github.com/Qiskit/qiskit-aer.git /tmp/qiskit-aer - cd /tmp/qiskit-aer - # Install qiskit-aer requirements. - pip install -U -c constraints.txt -r requirements-dev.txt --progress-bar off - # Build qiskit-aer with Thrust OpenMP CPU backend - python setup.py bdist_wheel -- -DCMAKE_CXX_COMPILER=g++-7 -DAER_THRUST_BACKEND=OMP -- -j4 - pip install dist/qiskit_aer*whl - # back to current repo directory - cd $TRAVIS_BUILD_DIR - fi - # install Aqua and Aqua dev. requirements - - pip install -e $TRAVIS_BUILD_DIR --progress-bar off - - pip install -U -c constraints.txt -r requirements-dev.txt --progress-bar off - - | - if [ "$DEPENDENCY_BRANCH" != "master" ]; then - pip install qiskit-ibmq-provider - fi - -stage_test_aqua: &stage_test_aqua - <<: *stage_dependencies - install: - - | - cat > selection.txt <1.0.0" - - pip install https://github.com/rpmuller/pyquante2/archive/master.zip --progress-bar off - script: - - pip check - - make spell - - make style - - make lint - # Lint with latest pylint also - - pip install -U pylint - - make lint - - make copyright - - make html SPHINXOPTS=-W - - make doctest - - name: "Lint check Python 3.8" - <<: *stage_dependencies - if: tag IS blank - python: 3.8 - install: - - pip install "cvxpy>1.0.0" - - pip install https://github.com/rpmuller/pyquante2/archive/master.zip --progress-bar off - script: - - make lint - # Lint with latest pylint also - - pip install -U pylint - - make lint - - name: "Mypy check" - <<: *stage_dependencies - if: tag IS blank - python: 3.5 - install: - - pip install cplex - script: - - make mypy - - name: "Test Aqua 1 Python 3.7" - <<: *stage_test_aqua - if: tag IS blank - workspaces: - create: - name: aqua137 - paths: - - aqua1.dat - - aqua137.dep - before_script: - - pip install cplex - - pip install "cvxpy>1.0.0" - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/aqua run --blacklist-file selection.txt 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage aqua1.dat - - python tools/extract_deprecation.py -file out.txt -output aqua137.dep - - name: "Test Aqua 1 Python 3.8" - <<: *stage_test_aqua - if: tag IS blank - workspaces: - create: - name: aqua138 - paths: aqua138.dep - python: 3.8 - before_script: - - pip install "cvxpy>1.0.0" - script: - - stestr --test-path test/aqua run --blacklist-file selection.txt 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output aqua138.dep - - name: "Test Aqua 2 Python 3.7" - <<: *stage_test_aqua - if: tag IS blank - workspaces: - create: - name: aqua237 - paths: - - aqua2.dat - - aqua237.dep - before_script: - - pip install cplex - - pip install "cvxpy>1.0.0" - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/aqua run --whitelist-file selection.txt 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage aqua2.dat - - python tools/extract_deprecation.py -file out.txt -output aqua237.dep - - name: "Test Aqua 2 Python 3.8" - <<: *stage_test_aqua - if: tag IS blank - workspaces: - create: - name: aqua238 - paths: aqua238.dep - python: 3.8 - before_script: - - pip install "cvxpy>1.0.0" - script: - - stestr --test-path test/aqua run --whitelist-file selection.txt 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output aqua238.dep - - name: "Test Chemistry Python 3.7" - <<: *stage_dependencies - if: tag IS blank - env: - - INSTALL_PSI4=yes - - OPENBLAS_NUM_THREADS=1 - workspaces: - create: - name: chemistry37 - paths: - - chemistry.dat - - chemistry37.dep - install: - # install gaussian dependency libgfortran 5 - - sudo apt-get -y update - - sudo apt-get -y install libgfortran5 - # Installing pyquante2 master branch... - - pip install https://github.com/rpmuller/pyquante2/archive/master.zip --progress-bar off - - pip install cplex pyscf - before_script: - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/chemistry run 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage chemistry.dat - - python tools/extract_deprecation.py -file out.txt -output chemistry37.dep - - name: "Test Chemistry Python 3.8" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: chemistry38 - paths: chemistry38.dep - python: 3.8 - env: OPENBLAS_NUM_THREADS=1 - install: - # install gaussian dependency libgfortran 5 - - sudo apt-get -y update - - sudo apt-get -y install libgfortran5 - # Installing pyquante2 master branch... - - pip install https://github.com/rpmuller/pyquante2/archive/master.zip --progress-bar off - - pip install pyscf - script: - - stestr --test-path test/chemistry run 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output chemistry38.dep - - name: "Test Finance Python 3.7" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: finance37 - paths: - - finance.dat - - finance37.dep - before_script: - - pip install cplex - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/finance run 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage finance.dat - - python tools/extract_deprecation.py -file out.txt -output finance37.dep - - name: "Test Finance Python 3.8" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: finance38 - paths: finance38.dep - python: 3.8 - script: - - stestr --test-path test/finance run 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output finance38.dep - - name: "Test Machine Learning Python 3.7" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: ml37 - paths: - - ml.dat - - ml37.dep - before_script: - - pip install cplex - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/ml run 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage ml.dat - - python tools/extract_deprecation.py -file out.txt -output ml37.dep - - name: "Test Machine Learning Python 3.8" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: ml38 - paths: ml38.dep - python: 3.8 - script: - - stestr --test-path test/ml run 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output ml38.dep - - name: "Test Optimization Python 3.7" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: optimization37 - paths: - - optimization.dat - - optimization37.dep - before_script: - - pip install cplex - - export PYTHON="coverage3 run --source qiskit/aqua,qiskit/chemistry,qiskit/finance,qiskit/ml,qiskit/optimization --omit */gauopen/* --parallel-mode" - script: - - stestr --test-path test/optimization run 2> >(tee /dev/stderr out.txt > /dev/null) - - coverage3 combine - - mv .coverage optimization.dat - - python tools/extract_deprecation.py -file out.txt -output optimization37.dep - - name: "Test Optimization Python 3.8" - <<: *stage_dependencies - if: tag IS blank - workspaces: - create: - name: optimization38 - paths: optimization38.dep - python: 3.8 - script: - - stestr --test-path test/optimization run 2> >(tee /dev/stderr out.txt > /dev/null) - - python tools/extract_deprecation.py -file out.txt -output optimization38.dep - - stage: Deprecation Messages and Coverage - name: "Print deprecation msgs, combine all coverages and upload to Coveralls" - if: tag IS blank - workspaces: - use: - - aqua137 - - aqua138 - - aqua237 - - aqua238 - - chemistry37 - - chemistry38 - - finance37 - - finance38 - - ml37 - - ml38 - - optimization37 - - optimization38 - install: - - pip install -U coverage coveralls diff-cover - script: - - sort -f -u aqua137.dep aqua138.dep aqua237.dep aqua238.dep chemistry37.dep chemistry38.dep finance37.dep finance38.dep ml37.dep ml38.dep optimization37.dep optimization38.dep || true - - coverage3 combine aqua1.dat aqua2.dat chemistry.dat finance.dat ml.dat optimization.dat - - coveralls || true - - coverage3 xml - - diff-cover --compare-branch master coverage.xml || true - - stage: Deploy - name: "Deploy to Pypi" - if: tag IS present - python: 3.7 - env: - - TWINE_USERNAME=qiskit - install: - - pip install -U twine pip setuptools virtualenv wheel - script: - - python3 setup.py sdist bdist_wheel - - twine upload dist/qiskit* diff --git a/qiskit/aqua/components/optimizers/__init__.py b/qiskit/aqua/components/optimizers/__init__.py index e448c7d4f0..612f0f3cd5 100644 --- a/qiskit/aqua/components/optimizers/__init__.py +++ b/qiskit/aqua/components/optimizers/__init__.py @@ -36,6 +36,7 @@ :toctree: ../stubs/ :nosignatures: + OptimizerSupportLevel Optimizer Local Optimizers @@ -83,7 +84,7 @@ """ -from .optimizer import Optimizer +from .optimizer import OptimizerSupportLevel, Optimizer from .adam_amsgrad import ADAM from .cg import CG from .cobyla import COBYLA @@ -104,6 +105,7 @@ from .nlopts.isres import ISRES __all__ = ['Optimizer', + 'OptimizerSupportLevel', 'ADAM', 'AQGD', 'CG', diff --git a/qiskit/aqua/components/optimizers/adam_amsgrad.py b/qiskit/aqua/components/optimizers/adam_amsgrad.py index e4c24da1cd..e08725a072 100644 --- a/qiskit/aqua/components/optimizers/adam_amsgrad.py +++ b/qiskit/aqua/components/optimizers/adam_amsgrad.py @@ -22,7 +22,7 @@ import csv import numpy as np from qiskit.aqua import aqua_globals -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -112,9 +112,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.supported + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.supported } def save_params(self, snapshot_dir: str) -> None: diff --git a/qiskit/aqua/components/optimizers/aqgd.py b/qiskit/aqua/components/optimizers/aqgd.py index b5d3714490..18b23486d9 100644 --- a/qiskit/aqua/components/optimizers/aqgd.py +++ b/qiskit/aqua/components/optimizers/aqgd.py @@ -18,12 +18,10 @@ from copy import deepcopy from numpy import pi, absolute, array, zeros from qiskit.aqua.utils.validation import validate_range_exclusive_max -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) -# pylint: disable=invalid-name - class AQGD(Optimizer): """Analytic Quantum Gradient Descent (AQGD) optimizer. @@ -78,9 +76,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def deriv(self, j, params, obj): @@ -160,17 +158,17 @@ def optimize(self, num_vars, objective_function, gradient_function=None, variable_bounds, initial_point) params = array(initial_point) - it = 0 + iter_count = 0 momentum = zeros(shape=(num_vars,)) objval = objective_function(params) if self._disp: - print("Iteration: " + str(it) + " \t| Energy: " + str(objval)) + print("Iteration: " + str(iter_count) + " \t| Energy: " + str(objval)) minobj = objval minparams = params - while it < self._maxiter and not self.converged(objval): + while iter_count < self._maxiter and not self.converged(objval): for j in range(num_vars): # update parameters in order based on quantum gradient derivative = self.deriv(j, params, objective_function) @@ -185,8 +183,8 @@ def optimize(self, num_vars, objective_function, gradient_function=None, minparams = params # update the iteration count - it += 1 + iter_count += 1 if self._disp: - print("Iteration: " + str(it) + " \t| Energy: " + str(objval)) + print("Iteration: " + str(iter_count) + " \t| Energy: " + str(objval)) - return minparams, minobj, it + return minparams, minobj, iter_count diff --git a/qiskit/aqua/components/optimizers/cg.py b/qiskit/aqua/components/optimizers/cg.py index 5c1fbb520e..d5f7e95175 100644 --- a/qiskit/aqua/components/optimizers/cg.py +++ b/qiskit/aqua/components/optimizers/cg.py @@ -18,7 +18,7 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -64,9 +64,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/cobyla.py b/qiskit/aqua/components/optimizers/cobyla.py index 015f2e26e7..58b1757a32 100644 --- a/qiskit/aqua/components/optimizers/cobyla.py +++ b/qiskit/aqua/components/optimizers/cobyla.py @@ -18,7 +18,7 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -60,9 +60,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/gsls.py b/qiskit/aqua/components/optimizers/gsls.py index 972cbc7a6a..f2f3ae79fc 100644 --- a/qiskit/aqua/components/optimizers/gsls.py +++ b/qiskit/aqua/components/optimizers/gsls.py @@ -20,7 +20,7 @@ import numpy as np from qiskit.aqua import aqua_globals -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -89,9 +89,9 @@ def get_support_level(self) -> Dict[str, int]: A dictionary containing the support levels for different options. """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars: int, diff --git a/qiskit/aqua/components/optimizers/l_bfgs_b.py b/qiskit/aqua/components/optimizers/l_bfgs_b.py index 19aea7341c..fd626fd3a2 100644 --- a/qiskit/aqua/components/optimizers/l_bfgs_b.py +++ b/qiskit/aqua/components/optimizers/l_bfgs_b.py @@ -17,14 +17,12 @@ import logging from scipy import optimize as sciopt -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) -# pylint: disable=invalid-name - -class L_BFGS_B(Optimizer): +class L_BFGS_B(Optimizer): # pylint: disable=invalid-name """ Limited-memory BFGS Bound optimizer. @@ -85,9 +83,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/nelder_mead.py b/qiskit/aqua/components/optimizers/nelder_mead.py index 7b9dd8198b..a894a9c55d 100644 --- a/qiskit/aqua/components/optimizers/nelder_mead.py +++ b/qiskit/aqua/components/optimizers/nelder_mead.py @@ -18,14 +18,12 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) -# pylint: disable=invalid-name - -class NELDER_MEAD(Optimizer): +class NELDER_MEAD(Optimizer): # pylint: disable=invalid-name """ Nelder-Mead optimizer. @@ -76,9 +74,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/nft.py b/qiskit/aqua/components/optimizers/nft.py index 6068e12702..b885be6ab6 100644 --- a/qiskit/aqua/components/optimizers/nft.py +++ b/qiskit/aqua/components/optimizers/nft.py @@ -20,7 +20,7 @@ import numpy as np from scipy.optimize import minimize from scipy.optimize import OptimizeResult -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -69,9 +69,9 @@ def __init__(self, def get_support_level(self): """ return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/nlopts/nloptimizer.py b/qiskit/aqua/components/optimizers/nlopts/nloptimizer.py index 3c681281e7..a3e8a1df1e 100644 --- a/qiskit/aqua/components/optimizers/nlopts/nloptimizer.py +++ b/qiskit/aqua/components/optimizers/nlopts/nloptimizer.py @@ -20,7 +20,7 @@ import logging import numpy as np from qiskit.aqua import MissingOptionalLibraryError -from qiskit.aqua.components.optimizers import Optimizer +from ..optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -86,9 +86,9 @@ def get_nlopt_optimizer(self) -> NLoptOptimizerType: def get_support_level(self): """ return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/optimizer.py b/qiskit/aqua/components/optimizers/optimizer.py index 50f6d91d1a..18ca95d241 100644 --- a/qiskit/aqua/components/optimizers/optimizer.py +++ b/qiskit/aqua/components/optimizers/optimizer.py @@ -21,19 +21,18 @@ logger = logging.getLogger(__name__) -# pylint: disable=invalid-name + +class OptimizerSupportLevel(IntEnum): + """ Support Level enum for features such as bounds, gradient and initial point """ + not_supported = 0 # Does not support the corresponding parameter in optimize() + ignored = 1 # Feature can be passed as non None but will be ignored + supported = 2 # Feature is supported + required = 3 # Feature is required and must be given, None is invalid class Optimizer(ABC): """Base class for optimization algorithm.""" - class SupportLevel(IntEnum): - """ Support Level enum """ - not_supported = 0 # Does not support the corresponding parameter in optimize() - ignored = 1 # Feature can be passed as non None but will be ignored - supported = 2 # Feature is supported - required = 3 # Feature is required and must be given, None is invalid - @abstractmethod def __init__(self): """ @@ -68,6 +67,7 @@ def set_options(self, **kwargs): self._options[name] = value logger.debug('options: %s', self._options) + # pylint: disable=invalid-name @staticmethod def gradient_num_diff(x_center, f, epsilon, max_evals_grouped=1): """ @@ -211,17 +211,17 @@ def gradient_support_level(self): @property def is_gradient_ignored(self): """ Returns is gradient ignored """ - return self._gradient_support_level == self.SupportLevel.ignored + return self._gradient_support_level == OptimizerSupportLevel.ignored @property def is_gradient_supported(self): """ Returns is gradient supported """ - return self._gradient_support_level != self.SupportLevel.not_supported + return self._gradient_support_level != OptimizerSupportLevel.not_supported @property def is_gradient_required(self): """ Returns is gradient required """ - return self._gradient_support_level == self.SupportLevel.required + return self._gradient_support_level == OptimizerSupportLevel.required @property def bounds_support_level(self): @@ -231,17 +231,17 @@ def bounds_support_level(self): @property def is_bounds_ignored(self): """ Returns is bounds ignored """ - return self._bounds_support_level == self.SupportLevel.ignored + return self._bounds_support_level == OptimizerSupportLevel.ignored @property def is_bounds_supported(self): """ Returns is bounds supported """ - return self._bounds_support_level != self.SupportLevel.not_supported + return self._bounds_support_level != OptimizerSupportLevel.not_supported @property def is_bounds_required(self): """ Returns is bounds required """ - return self._bounds_support_level == self.SupportLevel.required + return self._bounds_support_level == OptimizerSupportLevel.required @property def initial_point_support_level(self): @@ -251,17 +251,17 @@ def initial_point_support_level(self): @property def is_initial_point_ignored(self): """ Returns is initial point ignored """ - return self._initial_point_support_level == self.SupportLevel.ignored + return self._initial_point_support_level == OptimizerSupportLevel.ignored @property def is_initial_point_supported(self): """ Returns is initial point supported """ - return self._initial_point_support_level != self.SupportLevel.not_supported + return self._initial_point_support_level != OptimizerSupportLevel.not_supported @property def is_initial_point_required(self): """ Returns is initial point required """ - return self._initial_point_support_level == self.SupportLevel.required + return self._initial_point_support_level == OptimizerSupportLevel.required def print_options(self): """Print algorithm-specific options.""" diff --git a/qiskit/aqua/components/optimizers/p_bfgs.py b/qiskit/aqua/components/optimizers/p_bfgs.py index 856ca51152..85c3cd597c 100644 --- a/qiskit/aqua/components/optimizers/p_bfgs.py +++ b/qiskit/aqua/components/optimizers/p_bfgs.py @@ -24,14 +24,12 @@ from qiskit.aqua import aqua_globals from qiskit.aqua.utils.validation import validate_min -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) -# pylint: disable=invalid-name - -class P_BFGS(Optimizer): +class P_BFGS(Optimizer): # pylint: disable=invalid-name """ Parallelized Limited-memory BFGS optimizer. @@ -82,9 +80,9 @@ def __init__(self, def get_support_level(self): """ return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, @@ -126,9 +124,9 @@ def optimize_runner(_queue, _i_pt): # Multi-process sampling processes = [] for _ in range(num_procs): i_pt = aqua_globals.random.uniform(low, high) # Another random point in bounds - p = multiprocessing.Process(target=optimize_runner, args=(queue, i_pt)) - processes.append(p) - p.start() + proc = multiprocessing.Process(target=optimize_runner, args=(queue, i_pt)) + processes.append(proc) + proc.start() # While the one _optimize in this process below runs the other processes will # be running to. This one runs @@ -136,10 +134,10 @@ def optimize_runner(_queue, _i_pt): # Multi-process sampling sol, opt, nfev = self._optimize(num_vars, objective_function, gradient_function, variable_bounds, initial_point) - for p in processes: + for proc in processes: # For each other process we wait now for it to finish and see if it has # a better result than above - p.join() + proc.join() p_sol, p_opt, p_nfev = queue.get() if p_opt < opt: sol, opt = p_sol, p_opt diff --git a/qiskit/aqua/components/optimizers/powell.py b/qiskit/aqua/components/optimizers/powell.py index 6c4e6bb1c8..1481337459 100644 --- a/qiskit/aqua/components/optimizers/powell.py +++ b/qiskit/aqua/components/optimizers/powell.py @@ -18,7 +18,7 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -66,9 +66,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/slsqp.py b/qiskit/aqua/components/optimizers/slsqp.py index cc021ea765..6968cf701c 100644 --- a/qiskit/aqua/components/optimizers/slsqp.py +++ b/qiskit/aqua/components/optimizers/slsqp.py @@ -18,7 +18,7 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -66,9 +66,9 @@ def __init__(self, def get_support_level(self): """ Return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/spsa.py b/qiskit/aqua/components/optimizers/spsa.py index af99bbcb44..3dcde511fa 100644 --- a/qiskit/aqua/components/optimizers/spsa.py +++ b/qiskit/aqua/components/optimizers/spsa.py @@ -22,7 +22,7 @@ from qiskit.aqua import aqua_globals from qiskit.aqua.utils.validation import validate_min -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -107,9 +107,9 @@ def __init__(self, def get_support_level(self): """ return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.ignored, - 'bounds': Optimizer.SupportLevel.ignored, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.ignored, + 'bounds': OptimizerSupportLevel.ignored, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/qiskit/aqua/components/optimizers/tnc.py b/qiskit/aqua/components/optimizers/tnc.py index db7b01d8e1..d9e7bd773b 100644 --- a/qiskit/aqua/components/optimizers/tnc.py +++ b/qiskit/aqua/components/optimizers/tnc.py @@ -18,7 +18,7 @@ import logging from scipy.optimize import minimize -from .optimizer import Optimizer +from .optimizer import Optimizer, OptimizerSupportLevel logger = logging.getLogger(__name__) @@ -76,9 +76,9 @@ def __init__(self, def get_support_level(self): """ return support level dictionary """ return { - 'gradient': Optimizer.SupportLevel.supported, - 'bounds': Optimizer.SupportLevel.supported, - 'initial_point': Optimizer.SupportLevel.required + 'gradient': OptimizerSupportLevel.supported, + 'bounds': OptimizerSupportLevel.supported, + 'initial_point': OptimizerSupportLevel.required } def optimize(self, num_vars, objective_function, gradient_function=None, diff --git a/releasenotes/notes/optimization-supportlevel-d18e8dbced7172e0.yaml b/releasenotes/notes/optimization-supportlevel-d18e8dbced7172e0.yaml new file mode 100644 index 0000000000..7c29a5e0c8 --- /dev/null +++ b/releasenotes/notes/optimization-supportlevel-d18e8dbced7172e0.yaml @@ -0,0 +1,6 @@ +--- +deprecations: + - | + ``Optimizer.SupportLevel`` nested enum is replaced by ``OptimizerSupportLevel`` + and ``Optimizer.SupportLevel`` was removed. Use, for example, + ``OptimizerSupportLevel.required`` instead of ``Optimizer.SupportLevel.required``. diff --git a/requirements-dev.txt b/requirements-dev.txt index dc35f3ea92..36427e11db 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,7 +6,7 @@ pylintfileheader>=0.0.2 stestr>=2.0.0 ddt>=1.2.0,!=1.4.0 reno>=3.1.0;python_version>'3.5' -Sphinx>=1.8.3,<3.1.0 +Sphinx>=1.8.3,!=3.1.0 sphinx-rtd-theme>=0.4.0 sphinx-tabs>=1.1.11 sphinx-autodoc-typehints