qiskit.aqua.operators.gradients.Gradient
class Gradient(grad_method='param_shift', **kwargs)
Convert an operator expression to the first-order gradient.
Parameters
- grad_method (
Union
[str
,CircuitGradient
]) – The method used to compute the state/probability gradient. Can be either'param_shift'
or'lin_comb'
or'fin_diff'
. Ignored for gradients w.r.t observable parameters. - kwargs (dict) – Optional parameters for a CircuitGradient
Raises
ValueError – If method != fin_diff
and epsilon
is not None.
__init__
__init__(grad_method='param_shift', **kwargs)
Parameters
- grad_method (
Union
[str
,CircuitGradient
]) – The method used to compute the state/probability gradient. Can be either'param_shift'
or'lin_comb'
or'fin_diff'
. Ignored for gradients w.r.t observable parameters. - kwargs (dict) – Optional parameters for a CircuitGradient
Raises
ValueError – If method != fin_diff
and epsilon
is not None.
Methods
__init__ ([grad_method]) | type grad_methodUnion [str , CircuitGradient ] |
convert (operator[, params]) | type operatorOperatorBase |
get_gradient (operator, params) | Get the gradient for the given operator w.r.t. |
gradient_wrapper (operator, bind_params[, …]) | Get a callable function which provides the respective gradient, Hessian or QFI for given parameter values. |
parameter_expression_grad (param_expr, param) | Get the derivative of a parameter expression w.r.t. |
Attributes
grad_method | Returns CircuitGradient . |
convert
convert(operator, params=None)
Parameters
- operator (
OperatorBase
) – The operator we are taking the gradient of. - params (
Union
[ParameterVector
,ParameterExpression
,List
[ParameterExpression
],None
]) – params: The parameters we are taking the gradient with respect to.
Return type
OperatorBase
Returns
An operator whose evaluation yields the Gradient.
Raises
ValueError – If params
contains a parameter not present in operator
.
get_gradient
get_gradient(operator, params)
Get the gradient for the given operator w.r.t. the given parameters
Parameters
- operator (
OperatorBase
) – Operator w.r.t. which we take the gradient. - params (
Union
[ParameterExpression
,ParameterVector
,List
[ParameterExpression
]]) – Parameters w.r.t. which we compute the gradient.
Return type
OperatorBase
Returns
Operator which represents the gradient w.r.t. the given params.
Raises
- ValueError – If
params
contains a parameter not present inoperator
. - AquaError – If the coefficient of the operator could not be reduced to 1.
- AquaError – If the differentiation of a combo_fn requires JAX but the package is not installed.
- TypeError – If the operator does not include a StateFn given by a quantum circuit
- Exception – Unintended code is reached
grad_method
Returns CircuitGradient
.
Return type
CircuitGradient
Returns
CircuitGradient
.
gradient_wrapper
gradient_wrapper(operator, bind_params, grad_params=None, backend=None)
Get a callable function which provides the respective gradient, Hessian or QFI for given parameter values. This callable can be used as gradient function for optimizers.
Parameters
- operator (
OperatorBase
) – The operator for which we want to get the gradient, Hessian or QFI. - bind_params (
Union
[ParameterExpression
,ParameterVector
,List
[ParameterExpression
]]) – The operator parameters to which the parameter values are assigned. - grad_params (
Union
[ParameterExpression
,ParameterVector
,List
[ParameterExpression
],Tuple
[ParameterExpression
,ParameterExpression
],List
[Tuple
[ParameterExpression
,ParameterExpression
]],None
]) – The parameters with respect to which we are taking the gradient, Hessian or QFI. If grad_params = None, then grad_params = bind_params - backend (
Union
[BaseBackend
,QuantumInstance
,None
]) – The quantum backend or QuantumInstance to use to evaluate the gradient, Hessian or QFI.
Returns
Function to compute a gradient, Hessian or QFI. The function takes an iterable as argument which holds the parameter values.
Return type
callable(param_values)
parameter_expression_grad
static parameter_expression_grad(param_expr, param)
Get the derivative of a parameter expression w.r.t. the given parameter.
Parameters
- param_expr (
ParameterExpression
) – The Parameter Expression for which we compute the derivative - param (
ParameterExpression
) – Parameter w.r.t. which we want to take the derivative
Return type
Union
[ParameterExpression
, float
]
Returns
ParameterExpression representing the gradient of param_expr w.r.t. param