squlearn.qnn.loss.SquaredLoss

class squlearn.qnn.loss.SquaredLoss

Squared loss for regression.

gradient(value_dict: dict, **kwargs) ndarray | tuple[ndarray, ndarray]

Returns the gradient of the squared loss.

This function calculates the gradient of the squared loss between the values in value_dict and ground_truth as

\[2\sum_j \sum_i w_i \left(f\left(x_i\right)-f_ref\left(x_i\right)\right) \frac{\partial f(x_i)}{\partial p_j}\]
Parameters:
  • value_dict (dict) – Contains calculated values of the model

  • ground_truth (np.ndarray) – The true values \(f_ref\left(x_i\right)\)

  • weights (np.ndarray) – Weight for each data point, if None all data points count the same

  • multiple_output (bool) – True if the QNN has multiple outputs

Returns:

Gradient values

set_opt_param_op(opt_param_op: bool = True)

Sets the opt_param_op flag.

Parameters:

opt_param_op (bool) – True, if operator has trainable parameters

value(value_dict: dict, **kwargs) float

Calculates the squared loss.

This function calculates the squared loss between the values in value_dict and ground_truth as

\[\sum_i w_i \left|f\left(x_i\right)-f_ref\left(x_i\right)\right|^2\]
Parameters:
  • value_dict (dict) – Contains calculated values of the model

  • ground_truth (np.ndarray) – The true values \(f_ref\left(x_i\right)\)

  • weights (np.ndarray) – Weight for each data point, if None all data points count the

  • same

Returns:

Loss value

variance(value_dict: dict, **kwargs) float

Calculates the approximated variance of the squared loss.

This function calculates the approximated variance of the squared loss

\[4\sum_i w_i \left|f\left(x_i\right)-f_ref\left(x_i\right)\right|^2 \sigma_f^2(x_i)\]
Parameters:
  • value_dict (dict) – Contains calculated values of the model

  • ground_truth (np.ndarray) – The true values \(f_ref\left(x_i\right)\)

  • weights (np.ndarray) – Weight for each data point, if None all data points count the same

Returns:

Loss value