Skip to content

Commit

Permalink
Fix code-block:: python (Qiskit#8434)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Cryoris and mergify[bot] authored Aug 2, 2022
1 parent 028ba2e commit 1abec69
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions qiskit/algorithms/optimizers/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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__()
Expand Down

0 comments on commit 1abec69

Please sign in to comment.