qml.dot¶
-
dot
(coeffs, ops, pauli=False)[source]¶ Returns the dot product between the
coeffs
vector and theops
list 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
coeffs
andops
arguments, respectively.- Parameters
coeffs (Sequence[float, Callable]) – sequence containing the coefficients of the linear combination
ops (Sequence[Operator]) – sequence containing the operators of the linear combination
pauli (bool, optional) – If
True
, aPauliSentence
operator is used to represent the linear combination. If False, aSum
operator is returned. Defaults toFalse
. Note that whenops
consists solely ofPauliWord
andPauliSentence
instances, the function still returns a PennyLane operator whenpauli=False
.
- 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
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=True
can 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=True
and then converting the result to anOperator
is much faster than usingpauli=False
, but it only works for pauli words (seeis_pauli_word()
).If any of the parameters listed in
coeffs
are 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) )