qml.dot

dot(x, y)[source]

Lazily perform the dot product between arrays, tensors, and QNodeCollection.

Using this function, lazy dot products can be computed between two QNodeCollection objects, or a QNodeCollection object and an array/tensor object. In the latter case, only one-dimensional arrays/tensors are supported.

Parameters
  • x (array or tensor or QNodeCollection) – A QNode collection of independent QNodes, or an array/tensor object.

  • y (array or tensor or QNodeCollection) – A QNode collection of independent QNodes, or an array/tensor object.

See also

apply(), sum()

Example:

We can create a QNodeCollection using map():

>>> dev = qml.device("default.qubit", wires=2)
>>> obs_list = [qml.PauliX(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliZ(1)]
>>> qnodes = qml.map(qml.templates.StronglyEntanglingLayers, obs_list, dev, interface="torch")

The returned QNodeCollection contains 2 QNodes, as we mapped the StronglyEntanglingLayers() over a list of two observables:

>>> len(qnodes)
2

For the cost function, we now perform the dot product between a vector of coefficients and the QNodeCollection:

>>> coeffs = torch.tensor([0.32, -0.2], dtype=torch.double)
>>> cost = qml.dot(coeffs, qnodes)

Note

The cost function is equivalent to computing \(\langle 0 | U(\theta)^\dagger H U(\theta) | 0\rangle\) where

  • \(U(\theta)\) is the unitary applied by the strongly entangling layers, and

  • \(H = 0.32 X\otimes Z - 0.2 Z \otimes Z\).

This is a lazy dot product — no QNode evaluation has yet occured. Evaluation only occurs when the returned function cost is evaluated:

>>> x = qml.init.strong_ent_layers_normal(3, 2) # generate random parameters
>>> cost(x)
tensor(-0.2183, dtype=torch.float64, grad_fn=<DotBackward>)