qml.gradients.compute_vjp_multi

compute_vjp_multi(dy, jac, num=None)[source]

Convenience function to compute the vector-Jacobian product for a given vector of gradient outputs and a Jacobian for a tape with multiple measurements.

Parameters
  • dy (tensor_like) – vector of gradient outputs

  • jac (tensor_like, tuple) – Jacobian matrix

  • num (int) – The length of the flattened dy argument. This is an optional argument, but can be useful to provide if dy potentially has no shape (for example, due to tracing or just-in-time compilation).

Returns

the vector-Jacobian product

Return type

tensor_like

Examples

  1. For a single parameter and multiple measurement (one without shape and one with shape, e.g. expval and probs):

>>> jac = tuple([np.array(0.1), np.array([0.3, 0.4])])
>>> dy = tuple([np.array(1.0), np.array([1.0, 2.0])])
>>> compute_vjp_multi(dy, jac)
array([1.2])

2. For multiple parameters (in this case 2 parameters) and multiple measurement (one without shape and one with shape, e.g. expval and probs):

>>> jac = ((np.array(0.1), np.array(0.2)), (np.array([0.3, 0.4]), np.array([0.5, 0.6])))
>>> dy = tuple([np.array(1.0), np.array([1.0, 2.0])])
>>> compute_vjp_multi(dy, jac)
array([1.2, 1.9])