Removed a bug in QuantumCircuit.toffoli - #21
Open
Biswayan375 wants to merge 4 commits into
Open
Conversation
…Not sure in how many places it is still a problem
Biswayan375
commented
Feb 20, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the
QuantumCircuit.toffolifunction is called inside a loop and with a variable array for control variables, the bug is spotted. The scenario is likeThe output should be
Rather I got
The problem was not with loops, it was with using a variable array for the controls. First I thought the issue was with the
QuantumCircuit.__str__I recently wrote. But this function was dependent on the operator flow itself. So, I found the problem with the parameterqreg, inside which thecontrolsarray is. I was passing the array and insideQuantumCircuit.toffoli, it is directly used instead of its copy. So, whenever I change (append a new value) in the array, it gets updated in the Toffoli object as well. I just made it use a copy instead and the problem is solved for now. I have not studied the entire codebase so I don't know how many places it needs to use a copy instead of the original object. It is solved now for the Toffoli only.I reproduced the issue to get the bug with the following
which outputs