code: def compute_gradient(
      x, interface, circuit, return_type, complex=False
  ):
      """Return an interface-specific gradient or jacobian using the
      provided parameters:
  
      * input weights,
      * interface,
      * the QNode to execute,
      * return type and
      * whether output is complex
  
      For the StateCost and DensityMatrix return types, this computes the
      gradient of a scalar cost function dependent on the state instead of
      the jacobian of the state directly. The latter is tested by the
      StateVector return type.
      """
      if return_type == "StateCost":
          cost_fn = get_state_cost_fn(circuit)
      elif return_type == "DensityMatrix":
          cost_fn = get_density_matrix_cost_fn(circuit)
      else:
          cost_fn = circuit
  
      if interface == "autograd":
          if return_type in grad_return_cases:
              return qml.grad(cost_fn)(x)
          return qml.jacobian(cost_fn)(x)
      if interface == "jax":
          if return_type in grad_return_cases:
              return jax.grad(cost_fn)(x)
          return jax.jacrev(cost_fn, holomorphic=complex)(x)
      if interface == "tf":
          with tf.GradientTape(persistent=True) as tape:
              out = cost_fn(x)
  
          if return_type in grad_return_cases:
              return tape.gradient(out, [x])
          return tape.jacobian(out, [x], experimental_use_pfor=False)
      if interface == "torch":
          if return_type in grad_return_cases:
              res = cost_fn(x)
              res.backward()
              return x.grad
          return F.jacobian(circuit, (x,))
      return None
  
imports: import pytest
  import pennylane
  from pennylane import numpy
  from pennylane import QuantumFunctionError
  from pennylane.measurements import Probability, Expectation, Variance, Sample, VnEntropy, MutualInfo
  from pennylane.measurements.measurements import ObservableReturnTypes
label_desc: Use image node_id 5 for calling a global function with example usage: compute_gradient(x, interface, circuit, return_type, complex) and returns: None, qml, jax, tape, F, qml, jax, tape, x