From b7bc6a8abcbfbbadc1d921c69799f8260e50fe27 Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Tue, 2 Aug 2022 09:57:02 +0200 Subject: [PATCH 1/2] Fix code-block:: python --- qiskit/algorithms/optimizers/spsa.py | 64 +++++++++---------- qiskit/extensions/unitary.py | 4 +- .../passes/scheduling/padding/pad_delay.py | 2 +- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/qiskit/algorithms/optimizers/spsa.py b/qiskit/algorithms/optimizers/spsa.py index 6634c98dfdcb..707409efe4cf 100644 --- a/qiskit/algorithms/optimizers/spsa.py +++ b/qiskit/algorithms/optimizers/spsa.py @@ -110,6 +110,38 @@ def loss(x): two_spsa = SPSA(maxiter=300, second_order=True) result = two_spsa.optimize(ansatz.num_parameters, loss, initial_point=initial_point) + The `termination_checker` can be used to implement a custom termination criterion. + + .. code-block:: python + + import numpy as np + from qiskit.algorithms.optimizers import SPSA + + def objective(x): + return np.linalg.norm(x) + .04*np.random.rand(1) + + class TerminationChecker: + + def __init__(self, N : int): + self.N = N + self.values = [] + + def __call__(self, nfev, parameters, value, stepsize, accepted) -> bool: + self.values.append(value) + + if len(self.values) > self.N: + last_values = self.values[-self.N:] + pp = np.polyfit(range(self.N), last_values, 1) + slope = pp[0] / self.N + + if slope > 0: + return True + return False + + spsa = SPSA(maxiter=200, termination_checker=TerminationChecker(10)) + parameters, value, niter = spsa.optimize(2, objective, initial_point=[0.5, 0.5]) + print(f'SPSA completed after {niter} iterations') + References: @@ -206,38 +238,6 @@ def __init__( ValueError: If ``learning_rate`` or ``perturbation`` is an array with less elements than the number of iterations. - Example: - .. code-block::python - - import numpy as np - from qiskit.algorithms.optimizers import SPSA - - def objective(x): - return np.linalg.norm(x) + .04*np.random.rand(1) - - class TerminationChecker: - - def __init__(self, N : int): - self.N = N - self.values = [] - - def __call__(self, nfev, parameters, value, stepsize, accepted) -> bool: - self.values.append(value) - - if len(self.values) > self.N: - last_values = self.values[-self.N:] - pp = np.polyfit(range(self.N), last_values, 1) - slope = pp[0] / self.N - - if slope > 0: - return True - return False - - spsa = SPSA(maxiter=200, termination_checker=TerminationChecker(10)) - parameters, value, niter = spsa.optimize(2, objective, initial_point=[0.5, 0.5]) - print(f'SPSA completed after {niter} iterations') - - """ super().__init__() diff --git a/qiskit/extensions/unitary.py b/qiskit/extensions/unitary.py index a1bd2d2551aa..ff166044e82a 100644 --- a/qiskit/extensions/unitary.py +++ b/qiskit/extensions/unitary.py @@ -43,7 +43,7 @@ class UnitaryGate(Gate): to a quantum circuit. The matrix can also be directly applied to the quantum circuit, see :meth:`~qiskit.QuantumCircuit.unitary`. - .. code-block::python + .. code-block:: python from qiskit import QuantumCircuit from qiskit.extensions import UnitaryGate @@ -230,7 +230,7 @@ def unitary(self, obj, qubits, label=None): Apply a gate specified by a unitary matrix to a quantum circuit - .. code-block::python + .. code-block:: python from qiskit import QuantumCircuit matrix = [[0, 0, 0, 1], diff --git a/qiskit/transpiler/passes/scheduling/padding/pad_delay.py b/qiskit/transpiler/passes/scheduling/padding/pad_delay.py index c188247e2b26..c0a12267211a 100644 --- a/qiskit/transpiler/passes/scheduling/padding/pad_delay.py +++ b/qiskit/transpiler/passes/scheduling/padding/pad_delay.py @@ -24,7 +24,7 @@ class PadDelay(BasePadding): Consecutive delays will be merged in the output of this pass. - .. code-block::python + .. code-block:: python durations = InstructionDurations([("x", None, 160), ("cx", None, 800)]) From 61f9f59cd0cd14a5580e151acf2a8cc26b5c6415 Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Wed, 3 Aug 2022 09:52:06 +0200 Subject: [PATCH 2/2] Follow-up on code-blocks:: python --- qiskit/algorithms/minimum_eigen_solvers/vqe.py | 4 ++-- qiskit/algorithms/optimizers/gradient_descent.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/qiskit/algorithms/minimum_eigen_solvers/vqe.py b/qiskit/algorithms/minimum_eigen_solvers/vqe.py index 29d10b7be31e..87ab8707b0f1 100755 --- a/qiskit/algorithms/minimum_eigen_solvers/vqe.py +++ b/qiskit/algorithms/minimum_eigen_solvers/vqe.py @@ -92,7 +92,7 @@ class VQE(VariationalAlgorithm, MinimumEigensolver): The callable _must_ have the argument names ``fun, x0, jac, bounds`` as indicated in the following code block. - .. code-block::python + .. code-block:: python from qiskit.algorithms.optimizers import OptimizerResult @@ -111,7 +111,7 @@ def my_minimizer(fun, x0, jac=None, bounds=None) -> OptimizerResult: The above signature also allows to directly pass any SciPy minimizer, for instance as - .. code-block::python + .. code-block:: python from functools import partial from scipy.optimize import minimize diff --git a/qiskit/algorithms/optimizers/gradient_descent.py b/qiskit/algorithms/optimizers/gradient_descent.py index 5ca2e0f8cad8..38ed55048623 100644 --- a/qiskit/algorithms/optimizers/gradient_descent.py +++ b/qiskit/algorithms/optimizers/gradient_descent.py @@ -50,7 +50,7 @@ class GradientDescent(Optimizer): A minimum example that will use finite difference gradients with a default perturbation of 0.01 and a default learning rate of 0.01. - .. code-block::python + .. code-block:: python from qiskit.algorithms.optimizers import GradientDescent @@ -70,7 +70,7 @@ def f(x): Note how much faster this convergences (i.e. less ``nfevs``) compared to the previous example. - .. code-block::python + .. code-block:: python from qiskit.algorithms.optimizers import GradientDescent