Skip to content

Commit

Permalink
Log calibrated learning rate and perturbation in (QN-)SPSA (Qiskit#7109)
Browse files Browse the repository at this point in the history
* log calibrated learning rate and perturbation

* Fix formatting

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
Cryoris and jakelishman authored Oct 8, 2021
1 parent 9a376a5 commit dce862f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions qiskit/algorithms/optimizers/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def calibrate(
tuple(generator, generator): A tuple of powerseries generators, the first one for the
learning rate and the second one for the perturbation.
"""
logger.info("SPSA: Starting calibration of learning rate and perturbation.")
if target_magnitude is None:
target_magnitude = 2 * np.pi / 10

Expand Down Expand Up @@ -347,6 +348,15 @@ def calibrate(
warnings.warn(f"Calibration failed, using {target_magnitude} for `a`")
a = target_magnitude

logger.info("Finished calibration:")
logger.info(
" -- Learning rate: a / ((A + n) ^ alpha) with a = %s, A = %s, alpha = %s",
a,
stability_constant,
alpha,
)
logger.info(" -- Perturbation: c / (n ^ gamma) with c = %s, gamma = %s", c, gamma)

# set up the powerseries
def learning_rate():
return powerseries(a, alpha, stability_constant)
Expand Down Expand Up @@ -529,8 +539,7 @@ def minimize(
fun, x, max_evals_grouped=self._max_evals_grouped
)

logger.info("=" * 30)
logger.info("Starting SPSA optimization")
logger.info("SPSA: Starting optimization.")
start = time()

# keep track of the last few steps to return their average
Expand Down Expand Up @@ -611,8 +620,7 @@ def minimize(
logger.info("terminated optimization at {k}/{self.maxiter} iterations")
break

logger.info("SPSA finished in %s", time() - start)
logger.info("=" * 30)
logger.info("SPSA: Finished in %s", time() - start)

if self.last_avg > 1:
x = np.mean(last_steps, axis=0)
Expand Down

0 comments on commit dce862f

Please sign in to comment.