def replace_subcircuit(circuit, subcircuit, replace=None, pos=0):
 if (pos < 0):
  pos = 0
 if isinstance(circuit, Mul):
  circuit = circuit.args
 if isinstance(subcircuit, Mul):
  subcircuit = subcircuit.args
 if isinstance(replace, Mul):
  replace = replace.args
 elif (replace is None):
  replace = ()
 loc = find_subcircuit(circuit, subcircuit, start=pos)
 if (loc > (-1)):
  left = circuit[0:loc]
  right = circuit[(loc + len(subcircuit)):len(circuit)]
  circuit = ((left + replace) + right)
 return circuit

def replace_subcircuit ( circuit , subcircuit , replace = None , pos = 0 ) : if ( pos < 0 ) : pos = 0 if isinstance ( circuit , Mul ) : circuit = circuit . args if isinstance ( subcircuit , Mul ) : subcircuit = subcircuit . args if isinstance ( replace , Mul ) : replace = replace . args elif ( replace is None ) : replace = ( ) loc = find_subcircuit ( circuit , subcircuit , start = pos ) if ( loc > ( - 1 ) ) : left = circuit [ 0 : loc ] right = circuit [ ( loc + len ( subcircuit ) ) : len ( circuit ) ] circuit = ( ( left + replace ) + right ) return circuit
replaces a subcircuit with another subcircuit in circuit .