squlearn.encoding_circuit.EncodingCircuitDerivatives

class squlearn.encoding_circuit.EncodingCircuitDerivatives(encoding_circuit: EncodingCircuitBase, optree_caching: bool = True)

Class for automatic differentiation of encoding circuits.

This class allows to compute derivatives of a encoding circuit with respect to its parameters by utilizing the parameter-shift rule. The derivatives can be obtained by the method get_derivative(). The type of derivative can be specified by either a string (see table below) or a ParameterVector or (a list) of ParameterElements that can be accessed via feature_vector() or parameter_vector(), respectively.

Strings that are recognized by the get_derivative() method

String

Derivative

"I"

Identity Operation (returns the encoding circuit circuit)

"dx"

Gradient with respect to feature \(x\): \(\nabla_x = \big( \frac{\partial}{\partial x_1},\ldots, \frac{\partial}{\partial x_n} \big)\)

"dp"

Gradient with respect to parameter \(p\): \(\nabla_p = \big( \frac{\partial}{\partial p_1},\ldots, \frac{\partial}{\partial p_m} \big)\)

"dxdx"

Hessian with respect to feature \(x\): \(H^x_{ij} = \frac{\partial^2}{\partial x_i \partial x_j}\)

"dpdxdx"

Derivative of the feature Hessian with respect to parameter \(p\): \(\nabla_p H^x_{ij} = \big( \frac{\partial H^x_{ij}}{\partial p_1},\ldots, \frac{\partial H^x_{ij}}{\partial p_m} \big)\)

laplace

Laplace operator with respect to \(x\): \(\Delta = \nabla^2 = \sum_i \frac{\partial^2}{\partial x^2_i}\)

laplace_dp

Derivative of the Laplacian with respect to parameter \(p\): \(\nabla_p \circ \Delta = \big( \frac{\partial }{\partial p_1}\Delta,\ldots, \frac{\partial}{\partial p_m} \Delta \big)\)

"dpdp"

Hessian with respect to parameter \(p\): \(H^p_{ij} = \frac{\partial^2}{\partial p_i \partial p_j}\)

"dxdp" (or "dxdp")

Mixed Hessian with respect to feature \(x\) and parameter \(p\): \(H^{xp}_{ij} = \frac{\partial^2}{\partial x_i \partial p_j}\)

Example: Encoding Circuit gradient with respect to the trainable parameters

from squlearn.encoding_circuit import HubregtsenEncodingCircuit
from squlearn.encoding_circuit.encoding_circuit_derivatives import EncodingCircuitDerivatives
fm = HubregtsenEncodingCircuit(num_qubits=2, num_features=2, num_layers=2)
fm_deriv = EncodingCircuitDerivatives(fm)
grad = fm_deriv.get_derivative("dp")

Example: Derivative with respect to only the first trainable parameter

from squlearn.encoding_circuit import HubregtsenEncodingCircuit
from squlearn.encoding_circuit.encoding_circuit_derivatives import EncodingCircuitDerivatives
fm = HubregtsenEncodingCircuit(num_qubits=2, num_features=2, num_layers=2)
fm_deriv = EncodingCircuitDerivatives(fm)
dp0 = fm_deriv.get_derivative((fm_deriv.parameter_vector[0],))
Parameters:
  • encoding_circuit (EncodingCircuitBase) – Encoding circuit to differentiate

  • optree_caching (bool) – If True, the OpTree expressions are cached for faster evaluation. (default: True)

assign_parameters(optree: OpTreeElementBase, features: ndarray, parameters: ndarray) OpTreeElementBase

Assigns numerical values to the ParameterVector elements of the encoding circuit circuit.

Parameters:
  • optree (OperatorBase) – OpTree object to be assigned.

  • features (np.ndarray) – Numerical values of the feature vector.

  • parameters (np.ndarray) – Numerical values of the parameter vector.

Returns:

OpTree object with assigned numerical values.

get_derivative(derivative: str | tuple | list) OpTreeElementBase

Determine the derivative of the encoding circuit circuit.

Parameters:

derivative (str or tuple) – String or tuple of parameters for specifying the derivation.

Returns:

Derivative circuit in OpTree format.