Deepcopy gates in __iadd__
of QCircuit
#360
Merged
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.
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.