def METHOD_NAME(self, dag):
    """Run the CollectLinearFunctions pass on `dag`.
    Args:
        dag (DAGCircuit): the DAG to be optimized.
    Returns:
        DAGCircuit: the optimized DAG.
    """
    # If the option commutative_analysis is set, construct DAGDependency from the given DAGCircuit.
    if self.do_commutative_analysis:
        dag = dag_to_dagdependency(dag)
    # call collect_function to collect blocks from DAG
    blocks = self.collect_function(dag)
    # call collapse_function to collapse each block in the DAG
    self.collapse_function(dag, blocks)
    # If the option commutative_analysis is set, construct back DAGCircuit from DAGDependency.
    if self.do_commutative_analysis:
        dag = dagdependency_to_dag(dag)
    return dag