Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AnnotatedOperation being modified during parameter assignment (backport #12869) #12872

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading