squlearn.observables.observable_derivatives
.ObservableDerivatives
- class squlearn.observables.observable_derivatives.ObservableDerivatives(observable: ObservableBase | list, optree_caching=True, split_paulis=False)
Class for calculating derivatives of observables.
The derivatives are calculated by automatic differentiation of parameter in the expectation operator. Also, squaring of the operator is implemented. The class can either applied on a single operator, or on a list of operators. results are cached for faster evaluation.
- Parameters:
observable (Union[ObservableBase, list]) – Expectation operator or list of observables from which the derivatives are obtained.
optree_caching (bool) – If True, the optree structure of the observable is cached
String
Derivative
"O"
Expectation operator \(\hat{O}\)
"OO"
Squared observable \(\hat{O}^2\)
"dop"
or"Odop"
First-order derivative of the observable: \(\frac{d}{dp}\hat{O}(p)\)
"dopdop"
or"Odopdop"
Second-order derivative of the observable: \(\frac{d^2}{dp^2}\hat{O}(p)\)
"OOdop"
First-order derivative of the squared observable: \(\frac{d}{dp}\hat{O}^2(p)\)
"OOdopdop"
Second-order derivative of the squared observable: \(\frac{d^2}{dp^2}\hat{O}^2(p)\)
"I"
Returns a identity operator with the same number of qubits as the provided observable
Example: first-order derivative of the Ising Hamiltonian
from squlearn.observables import IsingHamiltonian from squlearn.observables.observable_derivatives import ObservableDerivatives op = IsingHamiltonian(num_qubits=3) print(ObservableDerivatives(op).get_derivative("dop"))
[1.0*SparsePauliOp(['III'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['IIZ'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['IZI'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['ZII'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['IZZ'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['ZIZ'], coeffs=[1.+0.j]), 1.0*SparsePauliOp(['ZZI'], coeffs=[1.+0.j])]
Example: Squared summed Pauli Operator
from squlearn.observables import SummedPaulis from squlearn.observables.observable_derivatives import ObservableDerivatives op = SummedPaulis(num_qubits=3) print(ObservableDerivatives(op).get_operator_squared())
SparsePauliOp(['III', 'IIZ', 'IZI', 'ZII', 'IZZ', 'ZIZ', 'ZZI'], coeffs=[ParameterExpression(1.0*p_op[0]**2 + 1.0*p_op[1]**2 + 1.0*p_op[2]**2 + 1.0*p_op[3]**2), ParameterExpression(2.0*p_op[0]*p_op[1]), ParameterExpression(2.0*p_op[0]*p_op[2]), ParameterExpression(2.0*p_op[0]*p_op[3]), ParameterExpression(2.0*p_op[1]*p_op[2]), ParameterExpression(2.0*p_op[1]*p_op[3]), ParameterExpression(2.0*p_op[2]*p_op[3])])
Attributes:
- parameter_vector
Parameter vector used in the observable
- Type:
ParameterVector
- num_parameters
Total number of trainable parameters in the observable
- Type:
int
- num_operators
Number operators in case of multiple observables
- Type:
int
Methods:
- assign_parameters(operator: OpTreeElementBase, parameters: ndarray) OpTreeElementBase
Assign parameters to a derivative that is obtained from this class.
- Parameters:
operator (OperatorBase) – Operator to which the parameters are assigned
parameters (np.ndarray) – Parameters values that replace the parameters in the operator
- Returns:
Operator with assigned parameters
- get_derivative(derivative: str | tuple | list) OpTreeElementBase
Determine the derivative of the observable.
- Parameters:
derivative (str or tuple) – String or tuple of parameters for specifying the derivation. See
ObservableDerivatives
for more information.- Returns:
Differentiated observable in OpTree format
- get_operator()
Returns the observable operator
- get_operator_squared()
Returns the squared form of the observable OO=O^2