Skip to content

Commit

Permalink
Fix annotated operation is modified during parameter assignment (#12869)
Browse files Browse the repository at this point in the history
(cherry picked from commit f508ac7)
  • Loading branch information
Cryoris authored and mergify[bot] committed Jul 31, 2024
1 parent 5b2d9e7 commit 64a7d90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/circuit/annotated_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __eq__(self, other) -> bool:

def copy(self) -> "AnnotatedOperation":
"""Return a copy of the :class:`~.AnnotatedOperation`."""
return AnnotatedOperation(base_op=self.base_op, modifiers=self.modifiers.copy())
return AnnotatedOperation(base_op=self.base_op.copy(), modifiers=self.modifiers.copy())

def to_matrix(self):
"""Return a matrix representation (allowing to construct Operator)."""
Expand Down
11 changes: 11 additions & 0 deletions test/python/circuit/test_annotated_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def test_invalid_params_access(self):
with self.assertRaises(AttributeError):
_ = annotated.validate_parameter(1.2)

def test_invariant_under_assign(self):
"""Test the annotated operation is not changed by assigning."""
p = Parameter("p")
annotated = RXGate(p).control(2, annotated=True)
circuit = QuantumCircuit(annotated.num_qubits)
circuit.append(annotated, circuit.qubits)
bound = circuit.assign_parameters([1.23])

self.assertEqual(list(circuit.parameters), [p])
self.assertEqual(len(bound.parameters), 0)


if __name__ == "__main__":
unittest.main()

0 comments on commit 64a7d90

Please sign in to comment.