Currently the __iadd__ function in QCircuit directly appends gates without copying. This means that when adding the same circuit twice, the same gate instances are added twice, instead of two copies.
This leads to unexpected problems, e.g. the following code doesn't work:
import tequila as tq
U = tq.QCircuit()
gate = tq.gates.X(target=0)
U += gate
U += gate
U.add_controls(control=1)
This is because add_controls tries to add a control to the gate twice, but during the second time fails because the gate already has the control.
Interestingly, since __add__ is implemented differently, this works:
import tequila as tq
U = tq.QCircuit()
gate = tq.gates.X(target=0)
U = U + gate
U = U + gate
U.add_controls(control=1)
I'm not sure what the intended behavior is, but my example can be fixed by appending a deepcopy of the gates instead.
Currently the
__iadd__
function inQCircuit
directly appends gates without copying. This means that when adding the same circuit twice, the same gate instances are added twice, instead of two copies. This leads to unexpected problems, e.g. the following code doesn't work:This is because
add_controls
tries to add a control to the gate twice, but during the second time fails because the gate already has the control.Interestingly, since
__add__
is implemented differently, this works:I'm not sure what the intended behavior is, but my example can be fixed by appending a deepcopy of the gates instead.