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

ScheduleBlock 3/4 - add ScheduleBlock #5854

Merged
merged 209 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
09fc52f
remove timeslot dependency from instruction
nkanazawa1989 Jan 22, 2021
09fed68
update type hint
nkanazawa1989 Jan 22, 2021
01b7e3e
update unittest
nkanazawa1989 Jan 22, 2021
609481c
update validation logic
nkanazawa1989 Jan 22, 2021
e838c7d
fix lint and test
nkanazawa1989 Jan 22, 2021
50a3fb7
Update qiskit/pulse/utils.py
nkanazawa1989 Jan 28, 2021
1c7c3fd
Update qiskit/pulse/instructions/instruction.py
nkanazawa1989 Jan 28, 2021
73ff507
Update qiskit/pulse/schedule.py
nkanazawa1989 Jan 28, 2021
85b79e8
minor updates
nkanazawa1989 Jan 28, 2021
cb8d11c
Merge branch 'issue-5679-1_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Jan 28, 2021
e888cea
add reno
nkanazawa1989 Jan 28, 2021
ceefcb4
Merge branch 'issue-5679-1_schedule_block' into issue-5679-2_schedule…
nkanazawa1989 Jan 28, 2021
42cb633
wip add call instruction
nkanazawa1989 Jan 28, 2021
20b2548
update flatten method
nkanazawa1989 Jan 28, 2021
8939e25
wip
nkanazawa1989 Jan 29, 2021
15e302f
deprecate flatten method
nkanazawa1989 Jan 29, 2021
cb53c12
cyclic import fix
nkanazawa1989 Feb 1, 2021
df858fb
fix recursion bug
nkanazawa1989 Feb 2, 2021
33d68b2
split subroutine removal function
nkanazawa1989 Feb 2, 2021
7cf36b8
update parameter table for call
nkanazawa1989 Feb 2, 2021
e7fa08c
update builder logic and relevant unittests
nkanazawa1989 Feb 3, 2021
bd17f6e
add reno
nkanazawa1989 Feb 3, 2021
5d63b7d
update docstring
nkanazawa1989 Feb 3, 2021
64ee355
Merge branch 'master' of github.com:Qiskit/qiskit-terra into issue-56…
nkanazawa1989 Feb 3, 2021
d1cdd5c
update docstring
nkanazawa1989 Feb 3, 2021
e1bfdc6
revert unittest
nkanazawa1989 Feb 3, 2021
c67fb64
update reno for deprecation
nkanazawa1989 Feb 3, 2021
d7936cd
fix empty subroutine bug
nkanazawa1989 Feb 3, 2021
7e63c0a
update docstring
nkanazawa1989 Feb 3, 2021
00d062b
update docstring
nkanazawa1989 Feb 3, 2021
35285ca
remove whitespace
nkanazawa1989 Feb 4, 2021
312a4c4
feedback from Lauren
nkanazawa1989 Feb 4, 2021
88654ff
lint
nkanazawa1989 Feb 4, 2021
da3d039
Update qiskit/pulse/builder.py
nkanazawa1989 Feb 8, 2021
a29857e
Update qiskit/pulse/builder.py
nkanazawa1989 Feb 8, 2021
2b2cded
Update releasenotes/notes/add-call-instruction-6f4fc37994090c2f.yaml
nkanazawa1989 Feb 8, 2021
fe90614
Update releasenotes/notes/add-call-instruction-6f4fc37994090c2f.yaml
nkanazawa1989 Feb 8, 2021
7817f27
Update qiskit/pulse/transforms.py
nkanazawa1989 Feb 8, 2021
3a28c91
update function name
nkanazawa1989 Feb 8, 2021
450971e
update Call instruction
nkanazawa1989 Feb 8, 2021
0c71ce2
update logic of inline function
nkanazawa1989 Feb 8, 2021
28f8f1e
update call function
nkanazawa1989 Feb 8, 2021
a6d7901
update test and fix bugs
nkanazawa1989 Feb 8, 2021
9430307
add test for call instruction
nkanazawa1989 Feb 8, 2021
3f21951
fix unittest name overlap
nkanazawa1989 Feb 8, 2021
bab92c4
update table initialization
nkanazawa1989 Feb 8, 2021
e3257d1
lint
nkanazawa1989 Feb 8, 2021
a99ebfc
update docstring
nkanazawa1989 Feb 8, 2021
6a9d86f
Merge branch 'master' into issue-5679-2_schedule_block
nkanazawa1989 Feb 9, 2021
ff11ba3
name update and type update
nkanazawa1989 Feb 9, 2021
a4c5c8a
update type hints
nkanazawa1989 Feb 9, 2021
4b035c6
Merge branch 'issue-5679-2_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Feb 9, 2021
e582a4b
Merge branch 'master' into issue-5679-2_schedule_block
nkanazawa1989 Feb 10, 2021
9897c80
feedback from Thomas
nkanazawa1989 Feb 14, 2021
4b601fd
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Feb 14, 2021
fae10af
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Feb 14, 2021
28d1497
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Feb 14, 2021
e933698
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Feb 14, 2021
d97eb2e
Update qiskit/pulse/transforms.py
nkanazawa1989 Feb 14, 2021
c7a45df
Merge branch 'issue-5679-2_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Feb 14, 2021
644dfff
Update qiskit/pulse/builder.py
nkanazawa1989 Feb 14, 2021
90df0d0
feedback from Daniel
nkanazawa1989 Feb 14, 2021
3816f25
Merge branch 'issue-5679-2_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Feb 14, 2021
d7e98af
Merge branch 'master' of github.com:Qiskit/qiskit-terra into issue-56…
nkanazawa1989 Feb 14, 2021
9954dcd
update test
nkanazawa1989 Feb 14, 2021
c582067
add value dict to call constructor
nkanazawa1989 Feb 14, 2021
abdaaa2
more test
nkanazawa1989 Feb 14, 2021
a8cae98
support calling subroutine with new parameters
nkanazawa1989 Feb 14, 2021
bfc706a
fix parameter assign bug
nkanazawa1989 Feb 14, 2021
31e2152
lint
nkanazawa1989 Feb 14, 2021
6289969
fix type hint
nkanazawa1989 Feb 14, 2021
8de880c
lint
nkanazawa1989 Feb 14, 2021
22eb83f
forward reference
nkanazawa1989 Feb 14, 2021
7886cb3
ignore lint
nkanazawa1989 Feb 14, 2021
110f546
wip - implementing ScheduleBlock
nkanazawa1989 Feb 16, 2021
1a186e2
wip - implementing ScheduleBlock
nkanazawa1989 Feb 17, 2021
7222ff6
remove some method support
nkanazawa1989 Feb 17, 2021
43e2f4f
code cleanup
nkanazawa1989 Feb 17, 2021
6babb4c
update filter logic
nkanazawa1989 Feb 18, 2021
c29d174
fix filter bug
nkanazawa1989 Feb 19, 2021
a5444f5
more docstring
nkanazawa1989 Feb 20, 2021
d5e315a
wip unittest
nkanazawa1989 Feb 24, 2021
55c3a96
unittest for basic functionality
nkanazawa1989 Mar 4, 2021
81dca9f
add complete unittest for block
nkanazawa1989 Mar 7, 2021
8e37d37
lint
nkanazawa1989 Mar 7, 2021
efc21ee
Merge branch 'master' of github.com:Qiskit/qiskit-terra into issue-56…
nkanazawa1989 Mar 7, 2021
903adda
update docstring
nkanazawa1989 Mar 7, 2021
4160f2c
add reno
nkanazawa1989 Mar 7, 2021
e59446f
lint
nkanazawa1989 Mar 8, 2021
46308aa
fix block to dag
nkanazawa1989 Mar 8, 2021
86a9f62
fix transformation enum
nkanazawa1989 Mar 8, 2021
a435a03
type hint
nkanazawa1989 Mar 8, 2021
c503df6
fix docstring
nkanazawa1989 Mar 8, 2021
104dae7
Update releasenotes/notes/add-schedule-block-c37527f3205b7b62.yaml
nkanazawa1989 Mar 9, 2021
588b11d
Update releasenotes/notes/add-schedule-block-c37527f3205b7b62.yaml
nkanazawa1989 Mar 9, 2021
87d066f
Update qiskit/pulse/transforms.py
nkanazawa1989 Mar 9, 2021
53fff61
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
c88b5cf
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
53e5c56
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
7ba67ce
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
0813a28
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
529fde0
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
76a5cb7
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
07687a7
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 9, 2021
e66619f
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
3a3173e
Update qiskit/pulse/filter_utils.py
nkanazawa1989 Mar 10, 2021
8c66bae
Update qiskit/pulse/filter_utils.py
nkanazawa1989 Mar 10, 2021
9c3d237
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
12bde3c
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
6204fe6
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
baf1d21
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
5e196fa
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
0458e5f
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
c56a79e
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
0c6e91b
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 10, 2021
0a92b7f
Update releasenotes/notes/add-schedule-block-c37527f3205b7b62.yaml
nkanazawa1989 Mar 10, 2021
78af6ec
response to comments
nkanazawa1989 Mar 10, 2021
50e6e4c
revert unexpected changes
nkanazawa1989 Mar 10, 2021
1d919bf
update test
nkanazawa1989 Mar 10, 2021
fd04e73
Merge branch 'master' into issue-5679-3_schedule_block
nkanazawa1989 Mar 11, 2021
482d3bf
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 11, 2021
62a314f
2nd round
nkanazawa1989 Mar 11, 2021
289ce3e
Merge branch 'issue-5679-3_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Mar 11, 2021
6917c4b
lint
nkanazawa1989 Mar 11, 2021
abc8906
docstring, remove unused syntax sugar
nkanazawa1989 Mar 12, 2021
5e8987e
remove type hint
nkanazawa1989 Mar 12, 2021
37a938b
remove unused import
nkanazawa1989 Mar 12, 2021
ff8428d
fix docstring
nkanazawa1989 Mar 12, 2021
55a47b2
Merge branch 'master' into issue-5679-3_schedule_block
nkanazawa1989 Mar 12, 2021
1b992ea
fix docstring
nkanazawa1989 Mar 12, 2021
42911a2
update docstring
nkanazawa1989 Mar 14, 2021
30cd83f
Merge branch 'issue-5679-3_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Mar 14, 2021
2b4e29f
update docstring
nkanazawa1989 Mar 14, 2021
25badb9
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 15, 2021
909f037
Update qiskit/pulse/transforms/dag.py
nkanazawa1989 Mar 15, 2021
e3da762
Update qiskit/pulse/transforms/dag.py
nkanazawa1989 Mar 15, 2021
57e0750
Update qiskit/pulse/transforms/__init__.py
nkanazawa1989 Mar 15, 2021
ae72237
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 15, 2021
d8e81b5
Update qiskit/pulse/transforms/__init__.py
nkanazawa1989 Mar 15, 2021
bccb54a
Update qiskit/pulse/transforms/__init__.py
nkanazawa1989 Mar 15, 2021
89e11bb
Update qiskit/pulse/transforms/__init__.py
nkanazawa1989 Mar 15, 2021
f52b7cf
feedback to comment
nkanazawa1989 Mar 15, 2021
b36f863
update parameter framework
nkanazawa1989 Mar 17, 2021
fecd28e
remove *blocks from ScheduleBlock constructor
nkanazawa1989 Mar 17, 2021
488e149
wip parameter manager test
nkanazawa1989 Mar 17, 2021
e7a04af
add parameter manager unittest and fix bugs
nkanazawa1989 Mar 18, 2021
7fa5f94
more bug fix
nkanazawa1989 Mar 18, 2021
4070b83
update reno
nkanazawa1989 Mar 18, 2021
a2a16c1
Merge branch 'master' of github.com:Qiskit/qiskit-terra into issue-56…
nkanazawa1989 Mar 18, 2021
60840b3
fix type hints
nkanazawa1989 Mar 18, 2021
0be878d
Update qiskit/pulse/instructions/acquire.py
nkanazawa1989 Mar 24, 2021
06bd629
Update qiskit/pulse/channels.py
nkanazawa1989 Mar 24, 2021
c5af458
Update qiskit/pulse/instructions/instruction.py
nkanazawa1989 Mar 24, 2021
9cde9b4
Update qiskit/pulse/transforms/__init__.py
nkanazawa1989 Mar 24, 2021
af17d83
Update releasenotes/notes/add-schedule-block-c37527f3205b7b62.yaml
nkanazawa1989 Mar 24, 2021
ecaa3de
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 24, 2021
f55af25
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 24, 2021
7435ca3
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 24, 2021
94c72c8
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 24, 2021
fb9152e
feedback from @lcapelluto
nkanazawa1989 Mar 24, 2021
c878c03
Merge branch 'issue-5679-3_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Mar 24, 2021
0b85d2e
channels tuple format
nkanazawa1989 Mar 24, 2021
772e723
feedback from @lcapelluto
nkanazawa1989 Mar 24, 2021
52a161d
update reno
nkanazawa1989 Mar 24, 2021
6417161
remove redundant logic from call
nkanazawa1989 Mar 24, 2021
f34fbcc
update parameter update logic
nkanazawa1989 Mar 25, 2021
dfffe33
add exception
nkanazawa1989 Mar 25, 2021
4f5a357
remove unused import
nkanazawa1989 Mar 25, 2021
4a0a3b6
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 25, 2021
ff938f4
further feedback
nkanazawa1989 Mar 25, 2021
d2d6aa7
Merge branch 'issue-5679-3_schedule_block' of github.com:nkanazawa198…
nkanazawa1989 Mar 25, 2021
69af4a5
minor updates
nkanazawa1989 Mar 25, 2021
2ac488e
Update qiskit/pulse/filters.py
nkanazawa1989 Mar 27, 2021
d025779
Update qiskit/pulse/filters.py
nkanazawa1989 Mar 27, 2021
d53571d
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Mar 27, 2021
d4c5ef2
Update qiskit/pulse/instructions/frequency.py
nkanazawa1989 Mar 27, 2021
6df9b06
Update qiskit/pulse/instructions/directives.py
nkanazawa1989 Mar 27, 2021
064a274
Update qiskit/pulse/instructions/frequency.py
nkanazawa1989 Mar 27, 2021
5aa8a33
Update qiskit/pulse/instructions/phase.py
nkanazawa1989 Mar 27, 2021
3d38886
Update qiskit/pulse/instructions/phase.py
nkanazawa1989 Mar 27, 2021
91f602d
Update qiskit/pulse/instructions/play.py
nkanazawa1989 Mar 27, 2021
ff61ff3
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 27, 2021
0f3ff4e
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 27, 2021
86f849f
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 27, 2021
2f980d5
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 27, 2021
3bb0527
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 27, 2021
da2c201
docstring updates
nkanazawa1989 Mar 27, 2021
0801e9b
docstring updates
nkanazawa1989 Mar 27, 2021
73a332e
channels become abstract
nkanazawa1989 Mar 27, 2021
967e622
block docstring update
nkanazawa1989 Mar 29, 2021
b48c7fb
Merge branch 'master' into issue-5679-3_schedule_block
nkanazawa1989 Mar 29, 2021
fc355dd
Merge branch 'master' into issue-5679-3_schedule_block
nkanazawa1989 Mar 30, 2021
6527de5
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Mar 30, 2021
28c5345
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Mar 30, 2021
dd72c77
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Mar 30, 2021
a9b6274
Update qiskit/pulse/instructions/delay.py
nkanazawa1989 Mar 30, 2021
c86bdd1
Update qiskit/pulse/instructions/call.py
nkanazawa1989 Mar 30, 2021
19480ee
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 30, 2021
1a171a9
Update qiskit/pulse/schedule.py
nkanazawa1989 Mar 30, 2021
0fa2a3f
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 30, 2021
2dc4190
Update qiskit/pulse/parameter_manager.py
nkanazawa1989 Mar 30, 2021
7bfe4ce
feedback from @taalexander
nkanazawa1989 Mar 30, 2021
3a5a6c2
replace __class__ with type
nkanazawa1989 Mar 30, 2021
378cd41
lint
nkanazawa1989 Mar 30, 2021
7a44b3c
fix type annotation
nkanazawa1989 Mar 30, 2021
8460bea
docs fix
nkanazawa1989 Mar 31, 2021
fc7f2b0
Merge branch 'master' into issue-5679-3_schedule_block
mergify[bot] Mar 31, 2021
a998ddf
Merge branch 'master' into issue-5679-3_schedule_block
mergify[bot] Mar 31, 2021
87bbbf2
Merge branch 'master' into issue-5679-3_schedule_block
mergify[bot] Mar 31, 2021
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
6 changes: 3 additions & 3 deletions qiskit/pulse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
:toctree: ../stubs/

Schedule
ScheduleBlock
Instruction


Expand All @@ -117,7 +118,7 @@
Schedule Transforms
===================

These functions take :class:`Schedule` s as input and return modified
Schedule transforms take :class:`Schedule` s as input and return modified
:class:`Schedule` s.

.. autosummary::
Expand All @@ -127,7 +128,6 @@
transforms.add_implicit_acquires
transforms.pad


Exceptions
==========

Expand Down Expand Up @@ -469,4 +469,4 @@
Waveform,
)
from qiskit.pulse.library.samplers.decorators import functional_pulse
from qiskit.pulse.schedule import Schedule
from qiskit.pulse.schedule import Schedule, ScheduleBlock
3 changes: 0 additions & 3 deletions qiskit/pulse/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1899,9 +1899,6 @@ def measure(qubit: int):
sched.draw()





Args:
func: The Python function to enable as a builder macro. There are no
requirements on the signature of the function, any calls to pulse
Expand Down
14 changes: 8 additions & 6 deletions qiskit/pulse/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
assembler.
"""
from abc import ABCMeta
from typing import Any, Set
from typing import Any, Set, Union

import numpy as np

from qiskit.circuit import Parameter
from qiskit.circuit.parameterexpression import ParameterExpression, ParameterValueType
from qiskit.pulse.exceptions import PulseError
from qiskit.pulse.utils import deprecated_functionality


class Channel(metaclass=ABCMeta):
Expand Down Expand Up @@ -68,13 +69,14 @@ def __init__(self, index: int):
"""
self._validate_index(index)
self._index = index
self._hash = None
self._hash = hash((self.__class__.__name__, self._index))

self._parameters = set()
if isinstance(index, ParameterExpression):
self._parameters = index.parameters

@property
def index(self) -> int:
def index(self) -> Union[int, ParameterExpression]:
"""Return the index of this channel. The index is a label for a control signal line
typically mapped trivially to a qubit index. For instance, ``DriveChannel(0)`` labels
the signal line driving the qubit labeled with index 0.
Expand All @@ -100,14 +102,16 @@ def _validate_index(self, index: Any) -> None:
raise PulseError('Channel index must be a nonnegative integer')

@property
@deprecated_functionality
def parameters(self) -> Set:
"""Parameters which determine the channel index."""
return self._parameters

def is_parameterized(self) -> bool:
"""Return True iff the channel is parameterized."""
return bool(self.parameters)
return isinstance(self.index, ParameterExpression)

@deprecated_functionality
def assign(self, parameter: Parameter, value: ParameterValueType) -> 'Channel':
"""Return a new channel with the input Parameter assigned to value.

Expand Down Expand Up @@ -153,8 +157,6 @@ def __eq__(self, other: 'Channel') -> bool:
return type(self) is type(other) and self._index == other._index

def __hash__(self):
if self._hash is None:
self._hash = hash((type(self), self._index))
return self._hash


Expand Down
185 changes: 185 additions & 0 deletions qiskit/pulse/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""A collection of functions that filter instructions in a pulse program."""

import abc
from typing import Callable, List, Union, Iterable, Optional, Tuple, Any

import numpy as np

from qiskit.pulse import Schedule
from qiskit.pulse.channels import Channel
from qiskit.pulse.schedule import Interval


def filter_instructions(sched: Schedule,
filters: List[Callable],
negate: bool = False,
recurse_subroutines: bool = True) -> Schedule:
"""A filtering function that takes a schedule and returns a schedule consisting of
filtered instructions.

Args:
sched: A pulse schedule to be filtered.
filters: List of callback functions that take an instruction and return boolean.
negate: Set `True` to accept an instruction if a filter function returns `False`.
Otherwise the instruction is accepted when the filter function returns `False`.
recurse_subroutines: Set `True` to individually filter instructions inside of a subroutine
defined by the :py:class:`~qiskit.pulse.instructions.Call` instruction.

Returns:
Filtered pulse schedule.
"""
from qiskit.pulse.transforms import flatten, inline_subroutines

target_sched = flatten(sched)
if recurse_subroutines:
target_sched = inline_subroutines(target_sched)

time_inst_tuples = np.array(target_sched.instructions)

valid_insts = np.ones(len(time_inst_tuples), dtype=bool)
for filt in filters:
valid_insts = np.logical_and(valid_insts, np.array(list(map(filt, time_inst_tuples))))

if negate and len(filters) > 0:
valid_insts = ~valid_insts

return Schedule(*time_inst_tuples[valid_insts], name=sched.name, metadata=sched.metadata)


def composite_filter(channels: Optional[Union[Iterable[Channel], Channel]] = None,
instruction_types: Optional[Union[Iterable[abc.ABCMeta], abc.ABCMeta]] = None,
time_ranges: Optional[Iterable[Tuple[int, int]]] = None,
intervals: Optional[Iterable[Interval]] = None) -> List[Callable]:
"""A helper function to generate a list of filter functions based on
typical elements to be filtered.

Args:
channels: For example, ``[DriveChannel(0), AcquireChannel(0)]``.
instruction_types (Optional[Iterable[Type[qiskit.pulse.Instruction]]]): For example,
``[PulseInstruction, AcquireInstruction]``.
time_ranges: For example, ``[(0, 5), (6, 10)]``.
intervals: For example, ``[(0, 5), (6, 10)]``.

Returns:
List of filtering functions.
"""
filters = []

# An empty list is also valid input for filter generators.
# See unittest `test.python.pulse.test_schedule.TestScheduleFilter.test_empty_filters`.
if channels is not None:
filters.append(with_channels(channels))
if instruction_types is not None:
filters.append(with_instruction_types(instruction_types))
if time_ranges is not None:
filters.append(with_intervals(time_ranges))
if intervals is not None:
filters.append(with_intervals(intervals))

return filters


def with_channels(channels: Union[Iterable[Channel], Channel]) -> Callable:
"""Channel filter generator.

Args:
channels: List of channels to filter.

Returns:
A callback function to filter channels.
"""
channels = _if_scalar_cast_to_list(channels)

def channel_filter(time_inst) -> bool:
"""Filter channel.

Args:
time_inst (Tuple[int, Instruction]): Time

Returns:
If instruction matches with condition.
"""
return any(chan in channels for chan in time_inst[1].channels)
return channel_filter


def with_instruction_types(types: Union[Iterable[abc.ABCMeta], abc.ABCMeta]) -> Callable:
"""Instruction type filter generator.

Args:
types: List of instruction types to filter.

Returns:
A callback function to filter instructions.
"""
types = _if_scalar_cast_to_list(types)

def instruction_filter(time_inst) -> bool:
"""Filter instruction.

Args:
time_inst (Tuple[int, Instruction]): Time

Returns:
If instruction matches with condition.
"""
return isinstance(time_inst[1], tuple(types))

return instruction_filter


def with_intervals(ranges: Union[Iterable[Interval], Interval]) -> Callable:
"""Interval filter generator.

Args:
ranges: List of intervals ``[t0, t1]`` to filter.

Returns:
A callback function to filter intervals.
"""
ranges = _if_scalar_cast_to_list(ranges)

def interval_filter(time_inst) -> bool:
"""Filter interval.
Args:
time_inst (Tuple[int, Instruction]): Time

Returns:
If instruction matches with condition.
"""
for t0, t1 in ranges:
inst_start = time_inst[0]
inst_stop = inst_start + time_inst[1].duration
if t0 <= inst_start and inst_stop <= t1:
return True
return False

return interval_filter


def _if_scalar_cast_to_list(to_list: Any) -> List[Any]:
"""A helper function to create python list of input arguments.

Args:
to_list: Arbitrary object can be converted into a python list.

Returns:
Python list of input object.
"""
try:
iter(to_list)
except TypeError:
to_list = [to_list]
return to_list
14 changes: 11 additions & 3 deletions qiskit/pulse/instructions/acquire.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""The Acquire instruction is used to trigger the qubit measurement unit and provide
some metadata for the acquisition process, for example, where to store classified readout data.
"""
from typing import Optional, Union
from typing import Optional, Union, Tuple

from qiskit.circuit import ParameterExpression
from qiskit.pulse.channels import MemorySlot, RegisterSlot, AcquireChannel
Expand Down Expand Up @@ -74,8 +74,7 @@ def __init__(self,
self._kernel = kernel
self._discriminator = discriminator

all_channels = tuple(chan for chan in [channel, mem_slot, reg_slot] if chan is not None)
super().__init__((duration, channel, mem_slot, reg_slot), None, all_channels, name=name)
super().__init__(operands=(duration, channel, mem_slot, reg_slot), name=name)

@property
def channel(self) -> AcquireChannel:
Expand All @@ -84,6 +83,11 @@ def channel(self) -> AcquireChannel:
"""
return self.operands[1]

@property
def channels(self) -> Tuple[Union[AcquireChannel, MemorySlot, RegisterSlot]]:
"""Returns the channels that this schedule uses."""
return tuple(self.operands[ind] for ind in (1, 2, 3) if self.operands[ind] is not None)

@property
def duration(self) -> Union[int, ParameterExpression]:
"""Duration of this instruction."""
Expand Down Expand Up @@ -118,6 +122,10 @@ def reg_slot(self) -> RegisterSlot:
"""
return self.operands[3]

def is_parameterized(self) -> bool:
"""Return True iff the instruction is parameterized."""
return isinstance(self.duration, ParameterExpression) or super().is_parameterized()

def __repr__(self) -> str:
return "{}({}{}{}{}{}{})".format(
self.__class__.__name__,
Expand Down
Loading