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
  • Loading branch information
Cryoris authored Jul 31, 2024
1 parent 4b056a7 commit f508ac7
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 f508ac7

Please sign in to comment.