code: def backward_pass(
      trajectory, states_dict, leaf_q_value: torch.Tensor
  ):
      """Backward pass of the montecarlo algorithm"""
      reward = 0
      for idx, (state, action_idx) in enumerate(reversed(trajectory)):
          if action_idx is None:  # leaf node
              reward += leaf_q_value
          else:
              (
                  _,
                  old_idx_to_new_idx,
                  _,
                  N_s_a,
                  q_values,
                  _,
              ) = states_dict[state]
              if isinstance(reward, torch.Tensor):
                  reward = reward.to(q_values.device)
              action_idx = int(action_idx)
              if action_idx in old_idx_to_new_idx:
                  not_dupl_index = old_idx_to_new_idx[int(action_idx)]
              else:
                  not_dupl_index = action_idx
              reward -= 1
              q_values[:, not_dupl_index] = (
                  N_s_a[:, not_dupl_index] * q_values[:, not_dupl_index]
                  + reward
              ) / (N_s_a[:, not_dupl_index] + 1)
              N_s_a[:, not_dupl_index] += 1
  
file_path: nebuly/optimization/open_alpha_tensor/open_alpha_tensor/core/actors/stage.py
imports: from typing import Dict, List
  import torch
  from open_alpha_tensor.core.data.utils import get_scalars, map_action_to_triplet
  from open_alpha_tensor.core.modules.alpha_tensor import AlphaTensorModel
label_desc: Use image node_id 12 for calling a global function with example usage: backward_pass(trajectory, states_dict, leaf_q_value) without return types