from qiskit import QuantumCircuit, transpile

qc = QuantumCircuit(2)
qc.reset(0)
qc.x(0)
qc.cx(0, 1)
qc.reset(0)
qc.reset(0)
qc.h(0)
qc.draw(output='mpl')

qc_basic_transpile = transpile(qc, optimization_level=3)
qc_basic_transpile.draw(output='mpl', style='iqp')

from qiskit.transpiler.passes.optimization.remove_reset_in_zero_state import RemoveResetInZeroState
from qiskit.converters import circuit_to_dag, dag_to_circuit

remove_reset = RemoveResetInZeroState()
remove_reset(qc).draw('mpl')

from dynacir.dynacir_passes import CollectResets

collect_resets = CollectResets()

circuit_with_less_resets = collect_resets(qc)
circuit_with_less_resets.draw(output='mpl')

!pip install git+https://github.com/derek-wang-ibm/dynacir.git

from qiskit.transpiler.preset_passmanagers.plugin import list_stage_plugins
print(list_stage_plugins("optimization"))

from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

pm = generate_preset_pass_manager(optimization_level=3, optimization_method="dynacir"
)

qc_dynacir = pm.run(qc)
qc_dynacir.draw(output='mpl', style='iqp')