qml.gradients.compute_vjp_single

compute_vjp_single(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 single measurement tape.

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 a single measurement without shape (e.g. expval, var):

>>> jac = np.array(0.1)
>>> dy = np.array(2)
>>> compute_vjp_single(dy, jac)
array([0.2])
  1. For a single parameter and a single measurement with shape (e.g. probs):

>>> jac = np.array([0.1, 0.2])
>>> dy = np.array([1.0, 1.0])
>>> compute_vjp_single(dy, jac)
array([0.3])
  1. For multiple parameters (in this case 2 parameters) and a single measurement without shape (e.g. expval, var):

>>> jac = tuple([np.array(0.1), np.array(0.2)])
>>> dy = np.array(2)
>>> compute_vjp_single(dy, jac)
array([0.2, 0.4])
  1. For multiple parameters (in this case 2 parameters) and a single measurement with shape (e.g. probs):

>>> jac = tuple([np.array([0.1, 0.2]), np.array([0.3, 0.4])])
>>> dy = np.array([1.0, 2.0])
>>> compute_vjp_single(dy, jac)
array([0.5, 1.1])