Opimizer class and its methods:

tqix.pis.optimizers.gradient_num_diff(x_center, f, epsilon, max_evals_grouped=1)
Parameters:
  • x_center (ndarray) – point around which we compute the gradient

  • f (func) – the function of which the gradient is to be computed.

  • epsilon (float) – the epsilon used in the numeric differentiation.

  • max_evals_grouped (int, optional) – max evals grouped, defaults to 1

Returns:

the gradient computed

Return type:

ndarray

class tqix.pis.optimizers.GD(lr: float, eps: float, maxiter: int, tol: float = 1e-06, use_qng=False, route=None, N=None, theta=None)

Gradient descent optimizer class (ref. qiskit)

tqix.pis.optimizers.GD.calc_fubini_tensor(self, params)

We calculate fubini tensor (note currently only apply for OAT,TNT,TAT gates)

Parameters:

params (List) – list of parameters

Returns:

G - fubini tensor

Return type:

ndarray, torch, sparse

tqix.pis.optimizers.GD.optimize(self, num_vars: int, objective_function: Callable[[ndarray], float], gradient_function: Optional[Callable[[ndarray], float]] = None, initial_point: Optional[ndarray] = None, return_loss_hist=None, loss_break=None, return_time_iters=None) Tuple[ndarray, float, int]
Parameters:
  • num_vars (int) – Number of parameters to be optimized.

  • objective_function (func) – Handle to a function that computes the objective function.

  • gradient_function (func, optional) – Handle to a function that computes the gradient of the objective function., defaults to None

  • initial_point (Optional[np.ndarray], optional) – The initial point for the optimization., defaults to None

  • return_loss_hist (bool, optional) – return history of loss values, defaults to None

  • loss_break (bool, optional) – early stopping, defaults to None

  • return_time_iters (bool, optional) – return time each iterations, defaults to None

Returns:

output

Return type:

Tuple[np.ndarray, float, int]

tqix.pis.optimizers.GD.minimize(self, objective_function: Callable[[ndarray], float], initial_point: ndarray, gradient_function: Callable[[ndarray], float], return_loss_hist: bool, return_time_iters: bool, loss_break: bool) Tuple[ndarray, float, int]

Run the minimization.

Parameters:
  • objective_function (func) – A function handle to the objective function.

  • initial_point (np.ndarray) – The initial iteration point.

  • gradient_function (func) – A function handle to the gradient of the objective function.

  • return_loss_hist (bool) – return loss history

  • return_time_iters (bool) – return time each iterations

  • loss_break (bool) – early stopping

Returns:

A tuple of (optimal parameters, optimal value, number of iterations).

Return type:

Tuple[np.ndarray, float, int]

class tqix.pis.optimizers.ADAM(maxiter: int = 10000, tol: float = 1e-06, lr: float = 0.001, beta_1: float = 0.9, beta_2: float = 0.99, noise_factor: float = 1e-08, eps: float = 1e-10, amsgrad: bool = False, snapshot_dir: Optional[str] = None)

Adam and AMSGRAD optimizers. (ref. qiskit)

tqix.pis.optimizers.ADAM.save_params(self, snapshot_dir: str) None

Save the current iteration parameters to a file called adam_params.csv.

Note:

The current parameters are appended to the file, if it exists already.

The file is not overwritten.

Parameters:

snapshot_dir (str) – The directory to store the file in.

tqix.pis.optimizers.ADAM.load_params(self, load_dir: str) None

Load iteration parameters for a file called adam_params.csv.

Parameters:

load_dir (str) – The directory containing adam_params.csv

tqix.pis.optimizers.ADAM.minimize(self, objective_function: Callable[[ndarray], float], initial_point: ndarray, gradient_function: Callable[[ndarray], float], return_loss_hist: bool, return_time_iters: bool, loss_break: int) Tuple[ndarray, float, int]
Parameters:
  • objective_function (func) – A function handle to the objective function.

  • initial_point (np.ndarray) – The initial iteration point.

  • gradient_function (func) – A function handle to the gradient of the objective function.

  • return_loss_hist (bool) – return history of losses

  • return_time_iters (bool) – return time each iterations

  • loss_break (int) – early stopping

Returns:

output

Return type:

Tuple[np.ndarray, float, int]

tqix.pis.optimizers.ADAM.optimize(self, num_vars: int, objective_function: Callable[[ndarray], float], gradient_function: Optional[Callable[[ndarray], float]] = None, initial_point: Optional[ndarray] = None, return_loss_hist=False, loss_break=None, return_time_iters=None) Tuple[ndarray, float, int]

Perform optimization.

Parameters:
  • num_vars (int) – Number of parameters to be optimized.

  • objective_function (func) – Handle to a function that computes the objective function.

  • gradient_function (func, optional) – Handle to a function that computes the gradient of the objective function, defaults to None

  • initial_point (Optional[np.ndarray], optional) – The initial point for the optimization, defaults to None

  • return_loss_hist (bool, optional) – return history of loss values, defaults to False

  • loss_break (bool, optional) – early stopping, defaults to None

  • return_time_iters (bool, optional) – return time each iterations, defaults to None

Returns:

output

Return type:

Tuple[np.ndarray, float, int]