qml.dot¶
- dot(coeffs, ops, pauli=False, grouping_type=None, method='lf')[source]¶
Returns the dot product between the
coeffsvector and theopslist of operators.This function returns the following linear combination: \(\sum_{k} c_k O_k\), where \(c_k\) and \(O_k\) are the elements inside the
coeffsandopsarguments, respectively.- Parameters:
coeffs (Sequence[float, Callable]) – sequence containing the coefficients of the linear combination
ops (Sequence[Operator, PauliWord, PauliSentence]) – sequence containing the operators of the linear combination. Can also be
PauliWordorPauliSentenceinstances.pauli (bool, optional) – If
True, aPauliSentenceoperator is used to represent the linear combination. If False, aSumoperator is returned. Defaults toFalse. Note that whenopsconsists solely ofPauliWordandPauliSentenceinstances, the function still returns a PennyLane operator whenpauli=False.grouping_type (str) – The type of binary relation between Pauli words used to compute the grouping. Can be
'qwc','commuting', or'anticommuting'. Note that ifpauli=True, the grouping will be ignored.method (str) – The graph colouring heuristic to use in solving minimum clique cover for grouping, which can be
'lf'(Largest First),'rlf'(Recursive Largest First),'dsatur'(Degree of Saturation), or'gis'(Greedy Independent Set). This keyword argument is ignored ifgrouping_typeisNone. Defaults to'lf'if no method is provided.
- Raises:
ValueError – if the number of coefficients and operators does not match or if they are empty
- Returns:
operator describing the linear combination
- Return type:
Note
If grouping is requested, the computed groupings are stored as a list of list of indices in
Sum.grouping_indices. The indices refer to the operators and coefficients returned bySum.terms(), notSum.operands, as these are not guaranteed to be equivalent.Example
>>> coeffs = np.array([1.1, 2.2]) >>> ops = [qml.X(0), qml.Y(0)] >>> qml.dot(coeffs, ops) 1.1 * X(0) + 2.2 * Y(0) >>> qml.dot(coeffs, ops, pauli=True) 1.1 * X(0) + 2.2 * Y(0)
Note that additions of the same operator are not executed by default.
>>> qml.dot([1., 1.], [qml.X(0), qml.X(0)]) X(0) + X(0)
You can obtain a cleaner version by simplifying the resulting expression.
>>> qml.dot([1., 1.], [qml.X(0), qml.X(0)]).simplify() 2.0 * X(0)
pauli=Truecan be used to construct a more efficient, simplified version of the operator. Note that it returns aPauliSentence, which is not anOperator. This specialized representation can be converted to an operator:>>> qml.dot([1, 2], [qml.X(0), qml.X(0)], pauli=True).operation() 3.0 * X(0)
Using
pauli=Trueand then converting the result to anOperatoris much faster than usingpauli=False, but it only works for pauli words (seeis_pauli_word()).If any of the parameters listed in
coeffsare callables, the resulting dot product will be aParametrizedHamiltonian:>>> coeffs = [lambda p, t: p * jnp.sin(t) for _ in range(2)] >>> ops = [qml.X(0), qml.Y(0)] >>> qml.dot(coeffs, ops) ( <lambda>(params_0, t) * X(0) + <lambda>(params_1, t) * Y(0) )
Grouping
Grouping information can be collected during construction using the
grouping_typeandmethodkeyword arguments. For example:import pennylane as qml a = qml.X(0) b = qml.prod(qml.X(0), qml.X(1)) c = qml.Z(0) obs = [a, b, c] coeffs = [1.0, 2.0, 3.0] op = qml.dot(coeffs, obs, grouping_type="qwc")
>>> op.grouping_indices ((0, 1), (2,))
grouping_typecan be"qwc"(qubit-wise commuting),"commuting", or"anticommuting", andmethodcan be'lf'(Largest First),'rlf'(Recursive Largest First),'dsatur'(Degree of Saturation), or'gis'(Greedy Independent Set). To see more details about how these affect grouping, see Pauli Graph Colouring andcompute_partition_indices().