<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def state_dict(self) -> dict: """ Calculate hidden state dictionary """

        hidden_state = {}

        if self.optimizer is not None:
            hidden_state['optimizer'] = self.optimizer.state_dict()

        for callback in self.callbacks:
            callback.write_state_dict(self.training_info, hidden_state)

        return hidden_state