def random_reduce(circuit, gate_ids, seed=None):
 from sympy.utilities.randtest import _randrange
 if (not gate_ids):
  return circuit
 if isinstance(circuit, Mul):
  circuit = circuit.args
 ids = flatten_ids(gate_ids)
 randrange = _randrange(seed)
 while ids:
  i = randrange(len(ids))
  id = ids.pop(i)
  if (find_subcircuit(circuit, id) != (-1)):
   break
 else:
  return circuit
 return replace_subcircuit(circuit, id)

def random_reduce ( circuit , gate_ids , seed = None ) : from sympy . utilities . randtest import _randrange if ( not gate_ids ) : return circuit if isinstance ( circuit , Mul ) : circuit = circuit . args ids = flatten_ids ( gate_ids ) randrange = _randrange ( seed ) while ids : i = randrange ( len ( ids ) ) id = ids . pop ( i ) if ( find_subcircuit ( circuit , id ) != ( - 1 ) ) : break else : return circuit return replace_subcircuit ( circuit , id )
shorten the length of a quantum circuit .