Butterflies don't evolve from caterpillars; butterflies develop from
caterpillars. How it happens is a problem in developmental biology,
not evolutionary biology. It is akin to the problem of how adult
humans develop from embryos. It happens every day, so it obviously is
not a theoretical difficulty.
Fruit flies go through the same developmental stages as caterpillars
and butterflies, and the research on fruit fly genetics is very
extensive. Anyone who is interested in how butterflies develop is
advised to look in that research.