squlearn.qnn.loss
.ParameterRegularizationLoss
- class squlearn.qnn.loss.ParameterRegularizationLoss(alpha: float | Callable[[int], float] = 0.005, mode: str = 'L2', parameter_list: list | None = None, parameter_operator_list: list | None = [])
Loss for parameter regularization.
Possible implementations:
"L1"
: \(L=\alpha \sum_i \left|p_i\right|\)"L2"
: \(L=\alpha \sum_i p_i^2\)
- Parameters:
alpha (float, Callable[[int], float]) – Weight value \(\alpha\)
mode (str) – Type of regularization, either ‘L1’ or ‘L2’ (default: ‘L2’).
parameter_list (list) – List of parameters to regularize, None: all (default: None).
parameter_operator_list (list) – List of operator parameters to regularize, None: all (default: []).
- gradient(value_dict: dict, **kwargs) ndarray | tuple[ndarray, ndarray]
Returns the gradient of the variance.
This function calculates the gradient of the variance values in value_dict.
- Parameters:
value_dict (dict) – Contains calculated values of the model
iteration (int) – iteration number, if variance_factor is a function
- 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
Returns the variance.
This function returns the weighted variance as
\[L_\text{var} = \alpha \sum_i \var_i\]- Parameters:
value_dict (dict) – Contains calculated values of the model
iteration (int) – iteration number, if alpha is a callable function
- Returns:
Loss value
- variance(value_dict: dict, **kwargs) float
Returns 0 since the variance is equal to zero.