quantumlib / Qualtran

Qᴜᴀʟᴛʀᴀɴ is a Python library for expressing and analyzing Fault Tolerant Quantum algorithms.
https://qualtran.readthedocs.io/en/latest/
Apache License 2.0
173 stars 40 forks source link

Fix CNOT.get_ctrl_system transposition error #1066

Closed mpharrigan closed 3 months ago

mpharrigan commented 3 months ago

Fixes #1065

get_ctrl_system is responsible for tracking which are the new soquets introduced to control the bloq and which are the existing ones. Since CNOT already has a control bit, we weave in the new control bit into the two bits in the ctrl register in Toffoli. Seemingly, at the beginning of the method I wanted to plug the new control bit into the second entry in the ctrl register but at the end of the method I thought I used the first entry in the ctrl register.

This is not an invalid thing to do for a general CompositeBloq but it is wrong for wiring up a Controlled(CNOT()). I added a unit test. The first assertion always passed. The second two previously failed. One needs to exercise Controlled([cbloq containing CNOT]).decompose_bloq() to see the bad behavior.