From 1f1db8de02baed5abced3c850dc262efce9492f6 Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Mon, 24 Oct 2022 11:38:54 +0900 Subject: [PATCH] Update instruction name AreaBarrier -> TimeBlockade --- qiskit/pulse/builder.py | 2 +- qiskit/pulse/instructions/__init__.py | 2 +- qiskit/pulse/instructions/directives.py | 27 +++++++++++++------ qiskit/qpy/type_keys.py | 12 ++++----- ...lder-and-rzx-builder-033ac8ad8ad2a192.yaml | 4 +-- test/python/qpy/test_block_load_from_qpy.py | 8 +++--- 6 files changed, 33 insertions(+), 22 deletions(-) diff --git a/qiskit/pulse/builder.py b/qiskit/pulse/builder.py index 677d688dba20..4656c442dc64 100644 --- a/qiskit/pulse/builder.py +++ b/qiskit/pulse/builder.py @@ -932,7 +932,7 @@ def _naive_typecast_schedule(schedule: Schedule): from qiskit.pulse.transforms import inline_subroutines, flatten, pad preprocessed_schedule = inline_subroutines(flatten(schedule)) - pad(preprocessed_schedule, inplace=True, pad_with=instructions.AreaBarrier) + pad(preprocessed_schedule, inplace=True, pad_with=instructions.TimeBlockade) # default to left alignment, namely ASAP scheduling target_block = ScheduleBlock(name=schedule.name) diff --git a/qiskit/pulse/instructions/__init__.py b/qiskit/pulse/instructions/__init__.py index b79134104738..9fab29a27c87 100644 --- a/qiskit/pulse/instructions/__init__.py +++ b/qiskit/pulse/instructions/__init__.py @@ -57,7 +57,7 @@ """ from .acquire import Acquire from .delay import Delay -from .directives import Directive, RelativeBarrier, AreaBarrier +from .directives import Directive, RelativeBarrier, TimeBlockade from .call import Call from .instruction import Instruction from .frequency import SetFrequency, ShiftFrequency diff --git a/qiskit/pulse/instructions/directives.py b/qiskit/pulse/instructions/directives.py index 66038373db48..187e4f73700d 100644 --- a/qiskit/pulse/instructions/directives.py +++ b/qiskit/pulse/instructions/directives.py @@ -11,7 +11,7 @@ # that they have been altered from the originals. """Directives are hints to the pulse compiler for how to process its input programs.""" - +import numpy as np from abc import ABC from typing import Optional, Tuple @@ -57,13 +57,13 @@ def __eq__(self, other): return isinstance(other, type(self)) and set(self.channels) == set(other.channels) -class AreaBarrier(Directive): - """Pulse ``AreaBarrier`` directive. +class TimeBlockade(Directive): + """Pulse ``TimeBlockade`` directive. This instruction is intended to be used internally within the pulse builder, to naively convert :class:`.Schedule` into :class:`.ScheduleBlock`. - Becasue :class:`.ScheduleBlock` cannot take absolute instruction interval, - this instruction helps the block represetation with finding instruction starting time. + Because :class:`.ScheduleBlock` cannot take absolute instruction time interval, + this instruction helps the block representation with finding instruction starting time. Example: @@ -79,7 +79,7 @@ class AreaBarrier(Directive): .. code-block:: python block = ScheduleBlock() - block.append(AreaBarrier(120, DriveChannel(0))) + block.append(TimeBlockade(120, DriveChannel(0))) block.append(Play(Constant(10, 0.1), DriveChannel(0))) Such conversion may be done by @@ -93,9 +93,9 @@ class AreaBarrier(Directive): .. note:: - The AreaBarrier instruction behaves almost identically + The TimeBlockade instruction behaves almost identically to :class:`~qiskit.pulse.instructions.Delay` instruction. - However, the AreaBarrier is just a compiler directive and must be removed before execution. + However, the TimeBlockade is just a compiler directive and must be removed before execution. This may be done by :func:`~qiskit.pulse.transforms.remove_directives` transform. Once these directives are removed, occupied timeslots are released and user can insert another instruction without timing overlap. @@ -116,6 +116,17 @@ def __init__( """ super().__init__(operands=(duration, channel), name=name) + def _validate(self): + """Called after initialization to validate instruction data. + + Raises: + PulseError: If the input ``duration`` is not integer value. + """ + if not isinstance(self.duration, int): + raise TypeError( + "TimeBlockade duration cannot be parameterized. Specify an integer duration value." + ) + @property def channel(self) -> chans.Channel: """Return the :py:class:`~qiskit.pulse.channels.Channel` that this instruction is diff --git a/qiskit/qpy/type_keys.py b/qiskit/qpy/type_keys.py index d79aee33a4b2..4355f8071615 100644 --- a/qiskit/qpy/type_keys.py +++ b/qiskit/qpy/type_keys.py @@ -44,7 +44,7 @@ SetPhase, ShiftPhase, RelativeBarrier, - AreaBarrier, + TimeBlockade, ) from qiskit.pulse.library import Waveform, SymbolicPulse from qiskit.pulse.schedule import ScheduleBlock @@ -232,7 +232,7 @@ class ScheduleInstruction(TypeKeyBase): SET_PHASE = b"q" SHIFT_PHASE = b"r" BARRIER = b"b" - AREA_BARRIER = b"c" + TimeBlockade = b"t" # 's' is reserved by ScheduleBlock, i.e. block can be nested as an element. # Call instructon is not supported by QPY. @@ -259,8 +259,8 @@ def assign(cls, obj): return cls.SHIFT_PHASE if isinstance(obj, RelativeBarrier): return cls.BARRIER - if isinstance(obj, AreaBarrier): - return cls.AREA_BARRIER + if isinstance(obj, TimeBlockade): + return cls.TimeBlockade raise exceptions.QpyError( f"Object type '{type(obj)}' is not supported in {cls.__name__} namespace." @@ -284,8 +284,8 @@ def retrieve(cls, type_key): return ShiftPhase if type_key == cls.BARRIER: return RelativeBarrier - if type_key == cls.AREA_BARRIER: - return AreaBarrier + if type_key == cls.TimeBlockade: + return TimeBlockade raise exceptions.QpyError( f"A class corresponding to type key '{type_key}' is not found in {cls.__name__} namespace." diff --git a/releasenotes/notes/upgrade-pulse-builder-and-rzx-builder-033ac8ad8ad2a192.yaml b/releasenotes/notes/upgrade-pulse-builder-and-rzx-builder-033ac8ad8ad2a192.yaml index caf8da9fcbb8..1d67f7aa058f 100644 --- a/releasenotes/notes/upgrade-pulse-builder-and-rzx-builder-033ac8ad8ad2a192.yaml +++ b/releasenotes/notes/upgrade-pulse-builder-and-rzx-builder-033ac8ad8ad2a192.yaml @@ -1,7 +1,7 @@ --- features: - | - New pulse directive :class:`~qiskit.pulse.instructions.AreaBarrier` has been added. + New pulse directive :class:`~qiskit.pulse.instructions.TimeBlockade` has been added. This instruction is QPY compatible. This directive behaves almost identically to delay instruction, but will be removed before execution. This directive is intended to be used internally within the pulse builder @@ -9,7 +9,7 @@ features: absolute time intervals. This allows the pulse builder to convert :class:`Schedule` into :class:`ScheduleBlock`, rather than wrapping with call instruction. - | - QPY dump and load now support :class:`~qiskit.pulse.instructions.AreaBarrier` instruction. + QPY dump and load now support :class:`~qiskit.pulse.instructions.TimeBlockade` instruction. upgrade: - | The behavior of the pulse builder when a :class:`.Schedule` is called diff --git a/test/python/qpy/test_block_load_from_qpy.py b/test/python/qpy/test_block_load_from_qpy.py index 0e9f6af7635a..e1ea9de79d8f 100644 --- a/test/python/qpy/test_block_load_from_qpy.py +++ b/test/python/qpy/test_block_load_from_qpy.py @@ -34,7 +34,7 @@ MemorySlot, RegisterSlot, ) -from qiskit.pulse.instructions import Play, AreaBarrier +from qiskit.pulse.instructions import Play, TimeBlockade from qiskit.circuit import Parameter, QuantumCircuit, Gate from qiskit.test import QiskitTestCase from qiskit.qpy import dump, load @@ -155,7 +155,7 @@ def test_barrier(self): def test_area_barrier(self): """Test area barrier.""" with builder.build() as test_sched: - builder.append_instruction(AreaBarrier(10, DriveChannel(0))) + builder.append_instruction(TimeBlockade(10, DriveChannel(0))) self.assert_roundtrip_equal(test_sched) def test_measure(self): @@ -197,8 +197,8 @@ def test_nested_blocks(self): def test_called_schedule(self): """Test referenced pulse Schedule object. - Referened object is naively converted into ScheduleBlock with AreaBarrier instructions. - Thus referenced Schedule is still QPY compatibile. + Referenced object is naively converted into ScheduleBlock with TimeBlockade instructions. + Thus referenced Schedule is still QPY compatible. """ refsched = Schedule() refsched.insert(20, Play(Constant(100, 0.1), DriveChannel(0)))