tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
362 stars 101 forks source link

Deepcopy gates in `__iadd__` of `QCircuit` #360

Closed ohuettenhofer closed 3 weeks ago

ohuettenhofer commented 4 weeks ago

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.

kottmanj commented 3 weeks ago

Thanks! Good catch.