From f40808e2c10a9c79b2fa03f14d2e2125ee5a8c37 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Tue, 9 Jan 2024 11:30:03 +0000 Subject: [PATCH 01/12] Enabled support for BackendV2 in method from_backend of class InstructionDurations --- qiskit/transpiler/instruction_durations.py | 69 ++++++++++++++++------ 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/qiskit/transpiler/instruction_durations.py b/qiskit/transpiler/instruction_durations.py index fc9186a8790a..2c512afc5134 100644 --- a/qiskit/transpiler/instruction_durations.py +++ b/qiskit/transpiler/instruction_durations.py @@ -12,16 +12,19 @@ """Durations of instructions, one of transpiler configurations.""" from __future__ import annotations +import logging from typing import Optional, List, Tuple, Union, Iterable import qiskit.circuit from qiskit.circuit import Barrier, Delay from qiskit.circuit import Instruction, ParameterExpression from qiskit.circuit.duration import duration_in_dt -from qiskit.providers import Backend +from qiskit.providers import Backend, BackendV1, BackendV2 from qiskit.transpiler.exceptions import TranspilerError from qiskit.utils.units import apply_prefix +logger = logging.getLogger(__name__) + class InstructionDurations: """Helper class to provide durations of instructions for scheduling. @@ -75,26 +78,54 @@ def from_backend(cls, backend: Backend): Raises: TranspilerError: If dt and dtm is different in the backend. """ + # All durations in seconds in gate_length instruction_durations = [] - backend_properties = backend.properties() - if hasattr(backend_properties, "_gates"): - for gate, insts in backend_properties._gates.items(): - for qubits, props in insts.items(): - if "gate_length" in props: - gate_length = props["gate_length"][0] # Throw away datetime at index 1 - instruction_durations.append((gate, qubits, gate_length, "s")) - for q, props in backend.properties()._qubits.items(): - if "readout_length" in props: - readout_length = props["readout_length"][0] # Throw away datetime at index 1 - instruction_durations.append(("measure", [q], readout_length, "s")) - - try: - dt = backend.configuration().dt - except AttributeError: - dt = None - - return InstructionDurations(instruction_durations, dt=dt) + dt = None + + # Logic to handle if backend is sub-class of old BackendV1 + if isinstance(backend, BackendV1): + backend_properties = backend.properties() + if hasattr(backend_properties, "_gates"): + for gate, insts in backend_properties._gates.items(): + for qubits, props in insts.items(): + if "gate_length" in props: + gate_length = props["gate_length"][0] # Ignore datetime at index 1 + instruction_durations.append((gate, qubits, gate_length, "s")) + for q, props in backend.properties()._qubits.items(): + if "readout_length" in props: + readout_length = props["readout_length"][0] # Ignore datetime at index 1 + instruction_durations.append(("measure", [q], readout_length, "s")) + + try: + dt = backend.configuration().dt + except AttributeError: + pass + + # Logic to handle if backend is sub-class is BackendV2 + elif isinstance(backend, BackendV2): + target = backend.target + inst_with_no_props = {"delay"} + inst_duration = None + for name in target.operation_names: + for qubits, inst_props in target._gate_map[name].items(): + if name in inst_with_no_props: + # Setting the duration for 'delay' to zero. + inst_duration = 0.0 + else: + try: + inst_duration = inst_props.duration + except AttributeError: + logger.info("%s on %s did not report any duration", name, qubits) + continue + instruction_durations.append((name, qubits, inst_duration, "s")) + + try: + dt = target.dt + except AttributeError: + logger.info("Backend Target didn't report any dt") + + return cls(instruction_durations, dt=dt) def update(self, inst_durations: "InstructionDurationsType" | None, dt: float = None): """Update self with inst_durations (inst_durations overwrite self). From 1c736b062801302c870910b1bfb1625890bb441b Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Tue, 9 Jan 2024 16:49:36 +0000 Subject: [PATCH 02/12] added, modified tests --- .../transpiler/test_instruction_durations.py | 124 +++++++++++++----- 1 file changed, 92 insertions(+), 32 deletions(-) diff --git a/test/python/transpiler/test_instruction_durations.py b/test/python/transpiler/test_instruction_durations.py index 4c5a5959a799..4c4344105e5a 100644 --- a/test/python/transpiler/test_instruction_durations.py +++ b/test/python/transpiler/test_instruction_durations.py @@ -13,17 +13,17 @@ # pylint: disable=missing-function-docstring """Test InstructionDurations class.""" - +from copy import deepcopy from qiskit.circuit import Delay, Parameter -from qiskit.providers.fake_provider import FakeParis, FakeTokyo +from qiskit.providers.fake_provider import FakeParis, FakePerth from qiskit.transpiler.exceptions import TranspilerError from qiskit.transpiler.instruction_durations import InstructionDurations from qiskit.test.base import QiskitTestCase -class TestInstructionDurationsClass(QiskitTestCase): - """Test Test InstructionDurations class.""" +class TestInstructionDurations(QiskitTestCase): + """Test InstructionDurations class.""" def test_empty(self): durations = InstructionDurations() @@ -36,41 +36,13 @@ def test_fail_if_invalid_dict_is_supplied_when_construction(self): with self.assertRaises(TranspilerError): InstructionDurations(invalid_dic) - def test_from_backend_for_backend_with_dt(self): - backend = FakeParis() - gate = self._find_gate_with_length(backend) - durations = InstructionDurations.from_backend(backend) - self.assertGreater(durations.dt, 0) - self.assertGreater(durations.get(gate, 0), 0) - - def test_from_backend_for_backend_without_dt(self): - backend = FakeTokyo() - gate = self._find_gate_with_length(backend) - durations = InstructionDurations.from_backend(backend) - self.assertIsNone(durations.dt) - self.assertGreater(durations.get(gate, 0, "s"), 0) - with self.assertRaises(TranspilerError): - durations.get(gate, 0) - def test_update_with_parameters(self): durations = InstructionDurations( [("rzx", (0, 1), 150, (0.5,)), ("rzx", (0, 1), 300, (1.0,))] ) - self.assertEqual(durations.get("rzx", [0, 1], parameters=[0.5]), 150) self.assertEqual(durations.get("rzx", [0, 1], parameters=[1.0]), 300) - def _find_gate_with_length(self, backend): - """Find a gate that has gate length.""" - props = backend.properties() - for gate in props.gates: - try: - if props.gate_length(gate.gate, 0): - return gate.gate - except Exception: # pylint: disable=broad-except - pass - raise ValueError("Unable to find a gate with gate length.") - def test_can_get_unbounded_duration_without_unit_conversion(self): param = Parameter("t") parameterized_delay = Delay(param, "dt") @@ -88,3 +60,91 @@ def test_fail_if_get_unbounded_duration_with_unit_conversion_when_dt_is_not_prov parameterized_delay = Delay(param, "s") with self.assertRaises(TranspilerError): InstructionDurations().get(parameterized_delay, 0) + + +class TestInstrctionDurationsFromBackendV1(QiskitTestCase): + """Test :meth:`~.from_backend` of :class:`.InstructionDurations` with + :class:`.BackendV1`""" + + def setUp(self): + super().setUp() + + self.backend = FakeParis() + self.backend_config = self.backend.configuration() + self.backend_props = self.backend.properties() + self.example_qubit = (0,) + self.example_gate = "x" + + # Setting dt for the copy of backend to be None + self.backend_cpy = deepcopy(self.backend) + self.backend_cpy.configuration().dt = None + + def test_backend_dt_equals_inst_dur_dt(self): + durations = InstructionDurations.from_backend(self.backend) + self.assertEqual(durations.dt, self.backend_config.dt) + + def test_backend_gate_length_equals_inst_dur(self): + durations = InstructionDurations.from_backend(self.backend) + inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0]) + backend_inst_dur = self.backend_props.gate_length( + gate=self.example_gate, qubits=self.example_qubit + ) + self.assertEqual(inst_dur_duration, backend_inst_dur) + + def test_backend_without_dt_sets_inst_dur_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + self.assertIsNone(durations.dt) + + def test_get_dur_s_with_dt_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + self.assertEqual( + durations.get(self.example_gate, self.example_qubit[0], "s"), 3.5555555555555554e-08 + ) + + def test_raise_dur_get_dt_with_backend_dt_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + with self.assertRaises(TranspilerError): + durations.get(self.example_gate, self.example_qubit[0]) + + +class TestInstrctionDurationsFromBackendV2(QiskitTestCase): + """Test :meth:`~.from_backend` of :class:`.InstructionDurations` with + :class:`.BackendV2`""" + + def setUp(self): + super().setUp() + + self.backend = FakePerth() + self.example_gate = "x" + self.example_qubit = (0,) + + # Setting dt for the copy for BackendV2 to None + self.backend_cpy = deepcopy(self.backend) + self.backend_cpy.target.dt = None + + def test_backend_dt_equals_inst_dur_dt(self): + durations = InstructionDurations.from_backend(self.backend) + self.assertEqual(durations.dt, self.backend.dt) + + def test_backend_gate_length_equals_inst_dur(self): + durations = InstructionDurations.from_backend(self.backend) + inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0], "s") + backend_inst_dur = self.backend.target._gate_map[self.example_gate][ + self.example_qubit + ].duration + self.assertEqual(inst_dur_duration, backend_inst_dur) + + def test_backend_without_dt_sets_inst_dur_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + self.assertIsNone(durations.dt) + + def test_get_dur_s_with_dt_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + self.assertEqual( + durations.get(self.example_gate, self.example_qubit[0], "s"), 3.5555555555555554e-08 + ) + + def test_raise_dur_get_dt_with_backend_dt_None(self): + durations = InstructionDurations.from_backend(self.backend_cpy) + with self.assertRaises(TranspilerError): + durations.get(self.example_gate, self.example_qubit[0]) From cf7fbd9fde51af9a73b3a9afd78db07bcb873426 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Tue, 9 Jan 2024 17:17:05 +0000 Subject: [PATCH 03/12] added release note --- ...urations_from_backend-9ce722879c870377.yaml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml new file mode 100644 index 000000000000..11a8d74c7ba9 --- /dev/null +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -0,0 +1,18 @@ +--- +features: + - | + This enables the support for :class:`.BackendV2` in :meth:`.from_backend` + of :class:`.InstructionDurations`. + + Users can have an object of :class:`.InstructionDurations` using :class:`.BackendV2` + with followig code. + + .. code-block:: python + from qiskit.providers.fake_provder import FakePerth + from qiskit.transpiler import InstructionDurations + backendV2 = FakePerth() + + inst_dur = InstructionDurations.from_backend(backendV2) + + + From 7f3f763f00a7e46180ceb02fd38be35b89f22de6 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Tue, 9 Jan 2024 18:54:02 +0000 Subject: [PATCH 04/12] added suggestion, lint --- qiskit/transpiler/instruction_durations.py | 30 ++++--------------- .../transpiler/test_instruction_durations.py | 6 ++-- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/qiskit/transpiler/instruction_durations.py b/qiskit/transpiler/instruction_durations.py index 2c1e1ae113f2..5014d4dad231 100644 --- a/qiskit/transpiler/instruction_durations.py +++ b/qiskit/transpiler/instruction_durations.py @@ -81,7 +81,7 @@ def from_backend(cls, backend: Backend): # All durations in seconds in gate_length instruction_durations = [] - dt = None + return_durations = None # Logic to handle if backend is sub-class of old BackendV1 if isinstance(backend, BackendV1): @@ -100,33 +100,15 @@ def from_backend(cls, backend: Backend): try: dt = backend.configuration().dt except AttributeError: - pass + dt = None + + return_durations = cls(instruction_durations, dt=dt) # Logic to handle if backend is sub-class is BackendV2 elif isinstance(backend, BackendV2): - target = backend.target - inst_with_no_props = {"delay"} - inst_duration = None - for name in target.operation_names: - for qubits, inst_props in target._gate_map[name].items(): - if name in inst_with_no_props: - # Setting the duration for 'delay' to zero. - inst_duration = 0.0 - else: - try: - inst_duration = inst_props.duration - except AttributeError: - logger.info("%s on %s did not report any duration", name, qubits) - continue - instruction_durations.append((name, qubits, inst_duration, "s")) - - try: - dt = target.dt - except AttributeError: - logger.info("Backend Target didn't report any dt") - + return_durations = backend.target.durations() - return cls(instruction_durations, dt=dt) + return return_durations def update(self, inst_durations: "InstructionDurationsType" | None, dt: float = None): """Update self with inst_durations (inst_durations overwrite self). diff --git a/test/python/transpiler/test_instruction_durations.py b/test/python/transpiler/test_instruction_durations.py index 4c4344105e5a..7fac3f802496 100644 --- a/test/python/transpiler/test_instruction_durations.py +++ b/test/python/transpiler/test_instruction_durations.py @@ -85,7 +85,7 @@ def test_backend_dt_equals_inst_dur_dt(self): def test_backend_gate_length_equals_inst_dur(self): durations = InstructionDurations.from_backend(self.backend) - inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0]) + inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0], "s") backend_inst_dur = self.backend_props.gate_length( gate=self.example_gate, qubits=self.example_qubit ) @@ -128,7 +128,9 @@ def test_backend_dt_equals_inst_dur_dt(self): def test_backend_gate_length_equals_inst_dur(self): durations = InstructionDurations.from_backend(self.backend) - inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0], "s") + inst_dur_duration = durations.get( + inst=self.example_gate, qubits=self.example_qubit[0], unit="s" + ) backend_inst_dur = self.backend.target._gate_map[self.example_gate][ self.example_qubit ].duration From 2868b410622eab1ada9f4b67ba7684855ad806b2 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Tue, 9 Jan 2024 19:22:50 +0000 Subject: [PATCH 05/12] fixed lint --- qiskit/transpiler/instruction_durations.py | 3 --- ...in_instruction_durations_from_backend-9ce722879c870377.yaml | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/qiskit/transpiler/instruction_durations.py b/qiskit/transpiler/instruction_durations.py index 5014d4dad231..7d579443d6ee 100644 --- a/qiskit/transpiler/instruction_durations.py +++ b/qiskit/transpiler/instruction_durations.py @@ -12,7 +12,6 @@ """Durations of instructions, one of transpiler configurations.""" from __future__ import annotations -import logging from typing import Optional, List, Tuple, Union, Iterable import qiskit.circuit @@ -23,8 +22,6 @@ from qiskit.transpiler.exceptions import TranspilerError from qiskit.utils.units import apply_prefix -logger = logging.getLogger(__name__) - class InstructionDurations: """Helper class to provide durations of instructions for scheduling. diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index 11a8d74c7ba9..d5806c03aa5b 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -8,7 +8,8 @@ features: with followig code. .. code-block:: python - from qiskit.providers.fake_provder import FakePerth + + from qiskit.providers.fake_provider import FakePerth from qiskit.transpiler import InstructionDurations backendV2 = FakePerth() From 78289301d42189b0913bfa8fd143a123a8cb49cf Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Wed, 10 Jan 2024 09:40:34 +0000 Subject: [PATCH 06/12] altered docstrings; added test; altered release note --- qiskit/transpiler/instruction_durations.py | 3 --- ...rations_from_backend-9ce722879c870377.yaml | 7 +++++-- .../transpiler/test_instruction_durations.py | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/qiskit/transpiler/instruction_durations.py b/qiskit/transpiler/instruction_durations.py index 7d579443d6ee..2a3bd802ee9a 100644 --- a/qiskit/transpiler/instruction_durations.py +++ b/qiskit/transpiler/instruction_durations.py @@ -71,9 +71,6 @@ def from_backend(cls, backend: Backend): Returns: InstructionDurations: The InstructionDurations constructed from backend. - - Raises: - TranspilerError: If dt and dtm is different in the backend. """ # All durations in seconds in gate_length diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index d5806c03aa5b..78e670f38c90 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -5,7 +5,7 @@ features: of :class:`.InstructionDurations`. Users can have an object of :class:`.InstructionDurations` using :class:`.BackendV2` - with followig code. + from :meth:`.from_backend` with followig code. .. code-block:: python @@ -15,5 +15,8 @@ features: inst_dur = InstructionDurations.from_backend(backendV2) - +upgrade: + - | + Given :code:`dt` and :code:`dtm` for a :class:`.BackendV1` or :class:`.BackendV2` is different, + :meth:`~.InstructionDurations.from_backend` does not raise any error. diff --git a/test/python/transpiler/test_instruction_durations.py b/test/python/transpiler/test_instruction_durations.py index 7fac3f802496..4b509da0b351 100644 --- a/test/python/transpiler/test_instruction_durations.py +++ b/test/python/transpiler/test_instruction_durations.py @@ -106,6 +106,16 @@ def test_raise_dur_get_dt_with_backend_dt_None(self): with self.assertRaises(TranspilerError): durations.get(self.example_gate, self.example_qubit[0]) + def test_works_unequal_dt_dtm(self): + self.backend_cpy.configuration().dt = 1.0 + + # This is expcted to fail + InstructionDurations.from_backend(self.backend_cpy) + + self.backend_cpy.configuration().dt = None # Resetting to None + # Check if dt and dtm were indeed unequal + self.assertNotEqual(self.backend_cpy.configuration().dtm, 1.0) + class TestInstrctionDurationsFromBackendV2(QiskitTestCase): """Test :meth:`~.from_backend` of :class:`.InstructionDurations` with @@ -150,3 +160,13 @@ def test_raise_dur_get_dt_with_backend_dt_None(self): durations = InstructionDurations.from_backend(self.backend_cpy) with self.assertRaises(TranspilerError): durations.get(self.example_gate, self.example_qubit[0]) + + def test_works_unequal_dt_dtm(self): + self.backend_cpy.target.dt = 1.0 + + # This is expcted to fail + InstructionDurations.from_backend(self.backend_cpy) + + self.backend_cpy.target.dt = None # Resetting to None + # Check if dt and dtm were indeed unequal + self.assertNotEqual(self.backend_cpy.dtm, 1.0) From 8dd3fb4648ba45ea39883fa2a5379a95d012e520 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Wed, 10 Jan 2024 10:30:52 +0000 Subject: [PATCH 07/12] fixed typo --- ..._in_instruction_durations_from_backend-9ce722879c870377.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index 78e670f38c90..e7d5a9d1596c 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -17,6 +17,6 @@ features: upgrade: - | - Given :code:`dt` and :code:`dtm` for a :class:`.BackendV1` or :class:`.BackendV2` is different, + Given :code:`dt` and :code:`dtm` for a :class:`.BackendV1` or :class:`.BackendV2` are unequal, :meth:`~.InstructionDurations.from_backend` does not raise any error. From 795bf616f07f6a359863ca342579cce5d13eb355 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Thu, 18 Jan 2024 16:37:20 +0000 Subject: [PATCH 08/12] altered release note --- ..._instruction_durations_from_backend-9ce722879c870377.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index e7d5a9d1596c..0a9986450e32 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -1,9 +1,8 @@ --- features: - | - This enables the support for :class:`.BackendV2` in :meth:`.from_backend` - of :class:`.InstructionDurations`. - + Support for :class:`.BackendV2` in :meth:`.InstructionDurations.from_backend` is added. + Users can have an object of :class:`.InstructionDurations` using :class:`.BackendV2` from :meth:`.from_backend` with followig code. From a02ad0cde0daaccb2bf9ff7806cb808ff13d77cc Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Thu, 15 Feb 2024 08:45:34 +0000 Subject: [PATCH 09/12] added suggestions --- ...struction_durations_from_backend-9ce722879c870377.yaml | 5 ----- test/python/transpiler/test_instruction_durations.py | 8 ++------ 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index 0a9986450e32..d7d0638ee976 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -14,8 +14,3 @@ features: inst_dur = InstructionDurations.from_backend(backendV2) -upgrade: - - | - Given :code:`dt` and :code:`dtm` for a :class:`.BackendV1` or :class:`.BackendV2` are unequal, - :meth:`~.InstructionDurations.from_backend` does not raise any error. - diff --git a/test/python/transpiler/test_instruction_durations.py b/test/python/transpiler/test_instruction_durations.py index 4b509da0b351..e1a6e4a59be3 100644 --- a/test/python/transpiler/test_instruction_durations.py +++ b/test/python/transpiler/test_instruction_durations.py @@ -70,8 +70,6 @@ def setUp(self): super().setUp() self.backend = FakeParis() - self.backend_config = self.backend.configuration() - self.backend_props = self.backend.properties() self.example_qubit = (0,) self.example_gate = "x" @@ -81,12 +79,12 @@ def setUp(self): def test_backend_dt_equals_inst_dur_dt(self): durations = InstructionDurations.from_backend(self.backend) - self.assertEqual(durations.dt, self.backend_config.dt) + self.assertEqual(durations.dt, self.backend.configuration().dt) def test_backend_gate_length_equals_inst_dur(self): durations = InstructionDurations.from_backend(self.backend) inst_dur_duration = durations.get(self.example_gate, self.example_qubit[0], "s") - backend_inst_dur = self.backend_props.gate_length( + backend_inst_dur = self.backend.properties().gate_length( gate=self.example_gate, qubits=self.example_qubit ) self.assertEqual(inst_dur_duration, backend_inst_dur) @@ -112,7 +110,6 @@ def test_works_unequal_dt_dtm(self): # This is expcted to fail InstructionDurations.from_backend(self.backend_cpy) - self.backend_cpy.configuration().dt = None # Resetting to None # Check if dt and dtm were indeed unequal self.assertNotEqual(self.backend_cpy.configuration().dtm, 1.0) @@ -167,6 +164,5 @@ def test_works_unequal_dt_dtm(self): # This is expcted to fail InstructionDurations.from_backend(self.backend_cpy) - self.backend_cpy.target.dt = None # Resetting to None # Check if dt and dtm were indeed unequal self.assertNotEqual(self.backend_cpy.dtm, 1.0) From 37229f8eb78b13d5f28d77105463aaccfe12dfd2 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Fri, 16 Feb 2024 13:59:04 +0000 Subject: [PATCH 10/12] added Generic Backends for tests --- .../transpiler/test_instruction_durations.py | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/test/python/transpiler/test_instruction_durations.py b/test/python/transpiler/test_instruction_durations.py index a397dbe939a9..5260aac032d4 100644 --- a/test/python/transpiler/test_instruction_durations.py +++ b/test/python/transpiler/test_instruction_durations.py @@ -15,7 +15,7 @@ """Test InstructionDurations class.""" from copy import deepcopy from qiskit.circuit import Delay, Parameter -from qiskit.providers.fake_provider import Fake27QPulseV1 +from qiskit.providers.fake_provider import Fake7QPulseV1, GenericBackendV2 from qiskit.transpiler.exceptions import TranspilerError from qiskit.transpiler.instruction_durations import InstructionDurations from test import QiskitTestCase # pylint: disable=wrong-import-order @@ -35,23 +35,6 @@ def test_fail_if_invalid_dict_is_supplied_when_construction(self): with self.assertRaises(TranspilerError): InstructionDurations(invalid_dic) - def test_from_backend_for_backend_with_dt(self): - backend = Fake27QPulseV1() - gate = self._find_gate_with_length(backend) - durations = InstructionDurations.from_backend(backend) - self.assertGreater(durations.dt, 0) - self.assertGreater(durations.get(gate, 0), 0) - - def test_from_backend_for_backend_without_dt(self): - backend = Fake27QPulseV1() - delattr(backend.configuration(), "dt") - gate = self._find_gate_with_length(backend) - durations = InstructionDurations.from_backend(backend) - self.assertIsNone(durations.dt) - self.assertGreater(durations.get(gate, 0, "s"), 0) - with self.assertRaises(TranspilerError): - durations.get(gate, 0) - def test_update_with_parameters(self): durations = InstructionDurations( [("rzx", (0, 1), 150, (0.5,)), ("rzx", (0, 1), 300, (1.0,))] @@ -85,7 +68,7 @@ class TestInstrctionDurationsFromBackendV1(QiskitTestCase): def setUp(self): super().setUp() - self.backend = FakeParis() + self.backend = Fake7QPulseV1() self.example_qubit = (0,) self.example_gate = "x" @@ -137,7 +120,7 @@ class TestInstrctionDurationsFromBackendV2(QiskitTestCase): def setUp(self): super().setUp() - self.backend = FakePerth() + self.backend = GenericBackendV2(num_qubits=7, calibrate_instructions=True, seed=1450) self.example_gate = "x" self.example_qubit = (0,) @@ -166,7 +149,7 @@ def test_backend_without_dt_sets_inst_dur_None(self): def test_get_dur_s_with_dt_None(self): durations = InstructionDurations.from_backend(self.backend_cpy) self.assertEqual( - durations.get(self.example_gate, self.example_qubit[0], "s"), 3.5555555555555554e-08 + durations.get(self.example_gate, self.example_qubit[0], "s"), 4.0147038772116484e-08 ) def test_raise_dur_get_dt_with_backend_dt_None(self): From 55aca1c6e7b2f4152da01076dbdd3386903c38a5 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Fri, 16 Feb 2024 15:00:23 +0000 Subject: [PATCH 11/12] fix docs --- ...2_in_instruction_durations_from_backend-9ce722879c870377.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index d7d0638ee976..98cda3361a8d 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -13,4 +13,3 @@ features: backendV2 = FakePerth() inst_dur = InstructionDurations.from_backend(backendV2) - From bdbe82d470fc0ce8f9f243ee79f5cdaf6e311f59 Mon Sep 17 00:00:00 2001 From: MozammilQ Date: Wed, 21 Feb 2024 00:41:09 +0000 Subject: [PATCH 12/12] fix docs --- ..._in_instruction_durations_from_backend-9ce722879c870377.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml index 98cda3361a8d..36f0f44f07c5 100644 --- a/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml +++ b/releasenotes/notes/Add_support_for_BackendV2_in_instruction_durations_from_backend-9ce722879c870377.yaml @@ -4,7 +4,7 @@ features: Support for :class:`.BackendV2` in :meth:`.InstructionDurations.from_backend` is added. Users can have an object of :class:`.InstructionDurations` using :class:`.BackendV2` - from :meth:`.from_backend` with followig code. + from :meth:`.InstructionDurations.from_backend` with followig code. .. code-block:: python