Lazily perform the dot product between arrays, tensors, and
Using this function, lazy dot products can be computed between two
QNodeCollectionobjects, or a
QNodeCollectionobject and an array/tensor object. In the latter case, only one-dimensional arrays/tensors are supported.
We can create a QNodeCollection using
>>> 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)
costfunction 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
>>> x = qml.init.strong_ent_layers_normal(3, 2) # generate random parameters >>> cost(x) tensor(-0.2183, dtype=torch.float64, grad_fn=<DotBackward>)