code: def q_retrace(R, D, q_i, v, rho_i, nenvs, nsteps, gamma):
      """
      Calculates q_retrace targets
  
      :param R: Rewards
      :param D: Dones
      :param q_i: Q values for actions taken
      :param v: V values
      :param rho_i: Importance weight for each action
      :return: Q_retrace values
      """
      rho_bar = batch_to_seq(
          tf.minimum(1.0, rho_i), nenvs, nsteps, True
      )  # list of len steps, shape [nenvs]
      rs = batch_to_seq(
          R, nenvs, nsteps, True
      )  # list of len steps, shape [nenvs]
      ds = batch_to_seq(
          D, nenvs, nsteps, True
      )  # list of len steps, shape [nenvs]
      q_is = batch_to_seq(q_i, nenvs, nsteps, True)
      vs = batch_to_seq(v, nenvs, nsteps + 1, True)
      v_final = vs[-1]
      qret = v_final
      qrets = []
      for i in range(nsteps - 1, -1, -1):
          check_shape(
              [qret, ds[i], rs[i], rho_bar[i], q_is[i], vs[i]],
              [[nenvs]] * 6,
          )
          qret = rs[i] + gamma * qret * (1.0 - ds[i])
          qrets.append(qret)
          qret = (rho_bar[i] * (qret - q_is[i])) + vs[i]
      qrets = qrets[::-1]
      qret = seq_to_batch(qrets, flat=True)
      return qret
  
imports: import time
  import functools
  import numpy
  import tensorflow
  from baselines import logger
  from baselines.common import set_global_seeds
  from baselines.common.policies import build_policy
  from baselines.common.tf_util import get_session, save_variables, load_variables
  from baselines.common.vec_env.vec_frame_stack import VecFrameStack
  from baselines.a2c.utils import batch_to_seq, seq_to_batch
  from baselines.a2c.utils import cat_entropy_softmax
  from baselines.a2c.utils import Scheduler, find_trainable_variables
  from baselines.a2c.utils import EpisodeStats
  from baselines.a2c.utils import get_by_index, check_shape, avg_norm, gradient_add, q_explained_variance
  from baselines.acer.buffer import Buffer
  from baselines.acer.runner import Runner
label_desc: Use image node_id 2 for calling a global function with example usage: q_retrace(R, D, q_i, v, rho_i, nenvs, nsteps, gamma) and returns: qret