I'm reading about surface code theory recently (Phys. Rev. A 86, 032324). 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). But when back to the circuits below, I still can not figure out the process how it work.
The key property these circuits have to satisfy is that, if there is a small number of Z or X errors next to the key gate (the T gate or the S gate in this case), the measurements that you perform will detect those errors. Additionally, they have to have the property that in the noiseless case they output the state you want. Put those two together, and you have a distillation circuit.
The 15-to-1 T state distillation circuit is an example circuit in Quirk. You can confirm there that the circuit meets both properties. It outputs a T state by default, and if you put Z errors next to the T gates, the post-selection present in the circuit filters out cases with at most two errors.