Skip to content

Commit e3f5c25

Browse files
mtreinishElePT
andauthored
Deprecate the unit and duration attributes (#13224)
* Deprecate the unit and duration attributes This commit deprecates the unit and duration attributes for QuantumCircuit and Instruction. These attributes will be removed in Qiskit 2.0 as they're not needed anymore and are adding a lot of complexity to the circuit data model as they're mutable state and extra memory slots that we need to keep around. The better model for tracking instruction duration is in the Target as it's inherently a property of the backend running the instructions. For the unittests this commit globally ignores the deprecation warning raised by this commit because internally we access the now deprecated access quite frequently as we need to check it when adding instructions to circuits to populate the rust data model. * Apply suggestions from code review Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> --------- Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
1 parent a3015f7 commit e3f5c25

4 files changed

Lines changed: 73 additions & 6 deletions

File tree

qiskit/circuit/instruction.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def add_decomposition(self, decomposition):
341341
sel.add_equivalence(self, decomposition)
342342

343343
@property
344+
@deprecate_func(since="1.3.0", removal_timeline="in Qiskit 2.0.0", is_property=True)
344345
def duration(self):
345346
"""Get the duration."""
346347
return self._duration
@@ -351,6 +352,7 @@ def duration(self, duration):
351352
self._duration = duration
352353

353354
@property
355+
@deprecate_func(since="1.3.0", removal_timeline="in Qiskit 2.0.0", is_property=True)
354356
def unit(self):
355357
"""Get the time unit of duration."""
356358
return self._unit

qiskit/circuit/quantumcircuit.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,17 +1145,35 @@ def __init__(
11451145
for var, initial in declarations:
11461146
self.add_var(var, initial)
11471147

1148-
self.duration: int | float | None = None
1149-
"""The total duration of the circuit, set by a scheduling transpiler pass. Its unit is
1150-
specified by :attr:`unit`."""
1151-
self.unit = "dt"
1152-
"""The unit that :attr:`duration` is specified in."""
1148+
self._duration = None
1149+
self._unit = "dt"
11531150
self.metadata = {} if metadata is None else metadata
11541151
"""Arbitrary user-defined metadata for the circuit.
1155-
1152+
11561153
Qiskit will not examine the content of this mapping, but it will pass it through the
11571154
transpiler and reattach it to the output, so you can track your own metadata."""
11581155

1156+
@property
1157+
@deprecate_func(since="1.3.0", removal_timeline="in Qiskit 2.0.0", is_property=True)
1158+
def duration(self):
1159+
"""The total duration of the circuit, set by a scheduling transpiler pass. Its unit is
1160+
specified by :attr:`unit`."""
1161+
return self._duration
1162+
1163+
@duration.setter
1164+
def duration(self, value: int | float | None):
1165+
self._duration = value
1166+
1167+
@property
1168+
@deprecate_func(since="1.3.0", removal_timeline="in Qiskit 2.0.0", is_property=True)
1169+
def unit(self):
1170+
"""The unit that :attr:`duration` is specified in."""
1171+
return self._unit
1172+
1173+
@unit.setter
1174+
def unit(self, value):
1175+
self._unit = value
1176+
11591177
@classmethod
11601178
def _from_circuit_data(cls, data: CircuitData, add_regs: bool = False) -> typing.Self:
11611179
"""A private constructor from rust space circuit data."""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
deprecations_circuits:
3+
- |
4+
The :attr:`.QuantumCircuit.unit` and :attr:`.QuantumCircuit.duration`
5+
attributes have been deprecated and will be removed in Qiskit 2.0.0. These
6+
attributes were used to track the estimated duration and unit of that
7+
duration to execute on the circuit. However, the values of these attributes
8+
were always limited, as they would only be properly populated if the
9+
transpiler were run with the correct settings. The duration was also only a
10+
guess based on the longest path on the sum of the duration of
11+
:class:`.DAGCircuit` and wouldn't ever correctly account for control flow
12+
or conditionals in the circuit.
13+
- |
14+
The :attr:`.Instruction.duration` and :attr:`.Instruction.unit` attributes
15+
have been deprecated and will be removed in Qiskit 2.0.0. These attributes
16+
were used to attach a custom execution duration and unit for that duration
17+
to an individual instruction. However, the source of truth of the duration
18+
of a gate is the :class:`.BackendV2` :class:`.Target` which contains
19+
the duration for each instruction supported on the backend. The duration of
20+
an instruction is not something that's typically user adjustable and is
21+
an immutable property of the backend. If you were previously using this
22+
capability to experiment with different durations for gates you can
23+
mutate the :attr:`.InstructionProperties.duration` field in a given
24+
:class:`.Target` to set a custom duration for an instruction on a backend
25+
(the unit is always in seconds in the :class:`.Target`).

test/utils/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ def setUpClass(cls):
156156
module="qiskit_aer",
157157
)
158158

159+
# Remove these four filters in Qiskit 2.0.0 when we remove unit and duration
160+
warnings.filterwarnings(
161+
"ignore",
162+
category=DeprecationWarning,
163+
message=r".*The property.*qiskit.circuit.instruction.Instruction.unit.*",
164+
)
165+
warnings.filterwarnings(
166+
"ignore",
167+
category=DeprecationWarning,
168+
message=r".*The property.*qiskit.circuit.instruction.Instruction.duration.*",
169+
)
170+
warnings.filterwarnings(
171+
"ignore",
172+
category=DeprecationWarning,
173+
message=r".*The property.*qiskit.circuit.quantumcircuit.QuantumCircuit.unit.*",
174+
)
175+
warnings.filterwarnings(
176+
"ignore",
177+
category=DeprecationWarning,
178+
message=r".*The property.*qiskit.circuit.quantumcircuit.QuantumCircuit.duration.*",
179+
)
180+
159181
# Safe to remove once `FakeBackend` is removed (2.0)
160182
warnings.filterwarnings(
161183
"ignore", # If "default", it floods the CI output

0 commit comments

Comments
 (0)