code: def test_graph_replace_advanced(self):
      x = MyVariable("x")
      y = MyVariable("y")
      z = MyVariable("z")
      w = MyVariable("w")
      z2 = MyOp("zop")(z)
      x2 = MyOp("xop")(x, w)
      x2.name = "x2"
      y2 = MyOp("yop")(y)
      y2.name = "y2"
      o = MyOp("xyop")(x2, y2)
      new_x = x.clone(name="x_new")
      new_y2 = y2.clone(name="y2_new")
      new_y21 = MyOp("ny2op")(new_y2)
      # now yet another replacement that could only appear after new_y2: z
      # show we can do that after the prev clone
      # the case where new variable is referenced during the replacements
      new_y21 = MyOp("ny2op")(new_y2)
      # the reference new_y2: z2 is not a part of the original graph so the replacement is unsafe
      oc = graph_replace([o], {x: new_x, y2: new_y21})
      oc = graph_replace(oc, {new_y2: z2})[0]
      assert oc.owner.inputs[1].owner.inputs[0] is z2
      assert oc.owner.inputs[0].owner.inputs[0] is new_x
      # the old reference is still kept
      assert oc.owner.inputs[0].owner.inputs[1] is w
  
      new_z = z.clone(name="z_new")
      oc = graph_replace([oc], {z: new_z})[0]
      # new reference appear
      assert oc.owner.inputs[1].owner.inputs[0] is not z2
      assert oc.owner.inputs[1].owner.inputs[0].owner.inputs[0] is new_z
      # the old reference is still kept
      assert oc.owner.inputs[0].owner.inputs[0] is new_x
      assert oc.owner.inputs[0].owner.inputs[1] is w
  
imports: import numpy
  import pytest
  import scipy.special
  import pytensor.tensor
  from pytensor import config, function, shared
  from pytensor.graph.basic import equal_computations, graph_inputs
  from pytensor.graph.replace import clone_replace, graph_replace, vectorize_graph, vectorize_node
  from pytensor.tensor import dvector, fvector, vector
  from tests import unittest_tools
  from tests.graph.utils import MyOp, MyVariable, op_multiple_outputs
label_desc: Use image node_id 3 for calling the TestGraphReplace obj's underlying member method code with example usage: obj.test_graph_replace_advanced() without return types