qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Improve support for Python 3.13 `copy.replace`

Open jakelishman opened this issue 1 year ago • 0 comments

What should we add?

We have some classes that act like immutable record types. These often define a custom replace method to allow constructing a new version of the class while making limited changes to a subset of fields. These classes tend now to be the ones defined in Rust space (CircuitInstruction is a prime example, but there may be more).

Python 3.13 formalised this idea into a part of the copy module / pickle protocol, with the user-facing copy.replace and the protocol-level object.__replace__. We should make sure that Qiskit objects that define a replace function with the same semantics expose the same behaviour as __replace__ to improve interoperability with later language features.

At the time of writing, I'm only aware of CircuitInstruction that does this verbatim, but it's possible that we have other classes that are dataclass-like (for example DataBin) that might want to begin supporting the behaviour.

jakelishman avatar Oct 07 '24 10:10 jakelishman