Skip to content

Commit

Permalink
Merge pull request #383 from Quantum-TII/cmaesrename
Browse files Browse the repository at this point in the history
renaming cma function
  • Loading branch information
scarrazza authored Apr 13, 2021
2 parents cbb846e + 78314bc commit db9b794
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/qibo/optimizers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from qibo.parallel import ParallelResources, _executor


def optimize(loss, initial_parameters, args=(), method='Powell',
jac=None, hess=None, hessp=None, bounds=None, constraints=(),
tol=None, callback=None, options=None, compile=False, processes=None):
"""Main optimization method. Selects one of the following optimizers:
- :meth:`qibo.optimizers.cma`
- :meth:`qibo.optimizers.cmaes`
- :meth:`qibo.optimizers.newtonian`
- :meth:`qibo.optimizers.sgd`
Expand Down Expand Up @@ -62,7 +65,7 @@ def myloss(parameters, circuit):
circuit.set_parameters(params)
"""
if method == "cma":
return cma(loss, initial_parameters, args, options)
return cmaes(loss, initial_parameters, args, options)
elif method == "sgd":
return sgd(loss, initial_parameters, args, options, compile)
else:
Expand All @@ -71,7 +74,7 @@ def myloss(parameters, circuit):
callback, options, processes)


def cma(loss, initial_parameters, args=(), options=None):
def cmaes(loss, initial_parameters, args=(), options=None):
"""Genetic optimizer based on `pycma <https://github.com/CMA-ES/pycma>`_.
Args:
Expand All @@ -80,8 +83,8 @@ def cma(loss, initial_parameters, args=(), options=None):
initial_parameters (np.ndarray): Initial guess for the variational
parameters.
args (tuple): optional arguments for the loss function.
options (dict): Dictionary with options accepted by the ``cma``.
optimizer. The user can use ``cma.CMAOptions()`` to view the
options (dict): Dictionary with options accepted by the ``cma``
optimizer. The user can use ``import cma; cma.CMAOptions()`` to view the
available options.
"""
import cma
Expand Down Expand Up @@ -161,14 +164,16 @@ def sgd(loss, initial_parameters, args=(), options=None, compile=False):
a message of the loss function.
"""
# check if gates are using the MatmulEinsum backend
compatible_backends = {"tensorflow_defaulteinsum", "tensorflow_matmuleinsum"}
compatible_backends = {
"tensorflow_defaulteinsum", "tensorflow_matmuleinsum"}
from qibo.core.circuit import Circuit
for argument in args:
if isinstance(argument, Circuit):
from qibo import K
if K.name not in compatible_backends: # pragma: no cover
if K.name not in compatible_backends: # pragma: no cover
from qibo.config import raise_error
raise_error(RuntimeError, "SGD requires native Tensorflow backend.")
raise_error(
RuntimeError, "SGD requires native Tensorflow backend.")

from qibo import K
from qibo.config import log
Expand Down Expand Up @@ -203,8 +208,7 @@ def opt_step():
return loss(vparams, *args).numpy(), vparams.numpy(), sgd_options


from qibo.parallel import ParallelResources, _executor
class ParallelBFGS: # pragma: no cover
class ParallelBFGS: # pragma: no cover
"""Computes the L-BFGS-B using parallel evaluation using multiprocessing.
This implementation here is based on https://doi.org/10.32614/RJ-2019-030.
Expand Down Expand Up @@ -256,7 +260,7 @@ def _eval_approx(eps_at, fun, x, eps):
if eps_at == 0:
x_ = x
else:
x_= x.copy()
x_ = x.copy()
if eps_at <= len(x):
x_[eps_at-1] += eps
else:
Expand All @@ -272,7 +276,8 @@ def evaluate(self, x, eps=1e-8):
self.itertools.repeat(x),
self.itertools.repeat(eps)))
self.function_value = ret[0]
self.jacobian_value = (ret[1:(len(x)+1)] - self.function_value ) / eps
self.jacobian_value = (
ret[1:(len(x)+1)] - self.function_value) / eps

def fun(self, x):
self.evaluate(x)
Expand Down

0 comments on commit db9b794

Please sign in to comment.