From 64a7d90036c07aacb58b3df7fb32de3e0af72d96 Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Wed, 31 Jul 2024 18:15:31 +0200 Subject: [PATCH] Fix annotated operation is modified during parameter assignment (#12869) (cherry picked from commit f508ac783c8d9be2ed3c5927c6839551bcb451c1) --- qiskit/circuit/annotated_operation.py | 2 +- test/python/circuit/test_annotated_operation.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/qiskit/circuit/annotated_operation.py b/qiskit/circuit/annotated_operation.py index 6006e68f58df..c1093d8fc5bc 100644 --- a/qiskit/circuit/annotated_operation.py +++ b/qiskit/circuit/annotated_operation.py @@ -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).""" diff --git a/test/python/circuit/test_annotated_operation.py b/test/python/circuit/test_annotated_operation.py index e2ca9f4af9a4..375c19fcf3a6 100644 --- a/test/python/circuit/test_annotated_operation.py +++ b/test/python/circuit/test_annotated_operation.py @@ -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()