Skip to content

Removed a bug in QuantumCircuit.toffoli - #21

Open
Biswayan375 wants to merge 4 commits into
LegacYFTw:mainfrom
Biswayan375:main
Open

Removed a bug in QuantumCircuit.toffoli#21
Biswayan375 wants to merge 4 commits into
LegacYFTw:mainfrom
Biswayan375:main

Conversation

@Biswayan375

Copy link
Copy Markdown
Contributor

When the QuantumCircuit.toffoli function is called inside a loop and with a variable array for control variables, the bug is spotted. The scenario is like

n_qudits = 3
qc = QuantumCircuit(qregs=[4 for i in range(n_qudits)])
controls, target = [], n_qudits-1
for i in range(n_qudits-1):
    controls.append(i)
    qc.toffoli((controls, target), 1)
qc.measure_all()
print(qc)

The output should be

(4)|0>--*----*----M--
(4)|0>--|----*----M--
(4)|0>-X(1)-X(1)--M--

depth: 2, width: 3

Rather I got

(4)|0>--*----*----M--
(4)|0>--*----*----M--
(4)|0>-X(1)-X(1)--M--

depth: 2, width: 3

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 parameter qreg, inside which the controls array is. I was passing the array and inside QuantumCircuit.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

class A:
    def __init__(self, x: tuple[list, int]):
        self.x = x
l1 = ([0, 1, 2], 2)
a = A(l1)
l1[0].append("bummer")
print(a.x)

which outputs

([0, 1, 2, 'bummer'], 2)

Comment thread src/qudiet/core/quantum_circuit.py
@LegacYFTw
LegacYFTw requested a review from arnavdas88 August 1, 2024 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant