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)

    # Create the random integer generator with the seed
    randrange = _randrange(seed)

    # Look for an identity in the circuit
    while ids:
        i = randrange(len(ids))
        id = ids.pop(i)
        if find_subcircuit(circuit, id) != -1:
            break
    else:
        # no identity was found
        return circuit

    # return circuit with the identity removed
    return replace_subcircuit(circuit, id)
Shorten the length of a quantum circuit.
dataset/ETHPy150Open sympy/sympy/sympy/physics/quantum/circuitutils.py/random_reduce