I'm reading about surface code theory recently ([Phys. Rev. A 86, 032324](https://arxiv.org/ftp/arxiv/papers/1208/1208.0928.pdf)). When I come across the magic state distillation part, it is difficult for me to understand the circuits. In addition, I have read Bravyi and Kitaev's paper about magic state distillation([Phys. Rev. A 71, 022316](https://arxiv.org/pdf/quant-ph/0403025.pdf)). But when back to the circuits below, I still can not figure out the process how it work. [![Distillation circuit for the |YL> ancilla state][1]][1] [![Distillation circuit for the |AL> ancilla state][2]][2] [1]: https://i.sstatic.net/B4cTo.png [2]: https://i.sstatic.net/twjaW.png