code: def _compute_cumulative_quantities(
      rewards: types.NestedArray,
      discounts: types.NestedArray,
      additional_discount: float,
  ):
      """Stolen from TransitionAdder."""
  
      # Give the same tree structure to the n-step return accumulator,
      # n-step discount accumulator, and self.discount, so that they can be
      # iterated in parallel using tree.map_structure.
      rewards, discounts = tree_utils.broadcast_structures(
          rewards, discounts
      )
      flat_rewards = tree.flatten(rewards)
      flat_discounts = tree.flatten(discounts)
      n_step = tf.shape(flat_rewards[0])[0]
      # Initialize flat output containers.
      flat_total_discounts = []
      flat_n_step_returns = []
  
      def scan_body(
          state: types.NestedTensor,
          discount_and_reward: types.NestedTensor,
      ) -> types.NestedTensor:
          compound_discount, discounted_return = state
          discount, reward = discount_and_reward
          return (
              additional_discount * discount * compound_discount,
              discounted_return
              + additional_discount * compound_discount * reward,
          )
  
      for reward, discount in zip(flat_rewards, flat_discounts):
          shape = tf.broadcast_static_shape(
              tf.TensorShape(reward[0].shape),
              tf.TensorShape(discount[0].shape),
          )
          total_discount = discount[0]
          n_step_return = tf.broadcast_to(reward[0], shape)
  
          if n_step > 1:
              # NOTE: total_discount will have one less additional_discount applied to
              # it (compared to flat_discount). This is so that when the learner/update
              # uses an additional discount we don't apply it twice. Inside the
              # following loop we will apply this right before summing up the
              # n_step_return.
              total_discount, n_step_return = tf.scan(
                  scan_body,
                  (discount[1:], reward[1:]),
                  (total_discount, n_step_return),
              )
  
              # Add the last return and discount of the scan, which correspond to the
              # n-step return and environment discount.
              n_step_return = n_step_return[-1]
              total_discount = total_discount[-1]
  
          flat_n_step_returns.append(n_step_return)
          flat_total_discounts.append(total_discount)
  
      n_step_return = tree.unflatten_as(rewards, flat_n_step_returns)
      total_discount = tree.unflatten_as(rewards, flat_total_discounts)
      return n_step_return, total_discount
  
imports: import itertools
  import time
  from typing import Callable, List, Optional, Sequence, Sized
  from absl import logging
  from acme import specs
  from acme import types
  from acme.adders import base
  from acme.adders.reverb import base
  from acme.adders.reverb import sequence
  from acme.utils import tree_utils
  import dm_env
  import numpy
  import reverb
  from reverb import structured_writer
  import tensorflow
  import tree
label_desc: Use image node_id 6 for calling a global function with example usage: _compute_cumulative_quantities(rewards, discounts, additional_discount) and returns: n_step_return,  total_discount, unknown,  unknown