Skip to content

Commit

Permalink
Add __len__ to Schedule in Pulse along with related tests. (Qiskit#4394)
Browse files Browse the repository at this point in the history
* Add __len__ to Schedule in Pulse along with its related tests (Qiskit#4171)

* Update __len__ in Pulse Schedule (Qiskit#4171)

* Update function name to test __len__ of Schedule in Pulse (Qiskit#4171)

Co-authored-by: Thomas Alexander <thomasalexander2718@gmail.com>

Co-authored-by: Thomas Alexander <thomasalexander2718@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 5, 2020
1 parent b902b94 commit ae74c84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions qiskit/pulse/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ def __lshift__(self, time: int) -> 'Schedule':
"""Return a new schedule which is shifted forward by ``time``."""
return self.shift(time)

def __len__(self) -> int:
"""Return number of instructions in the schedule."""
return len(self.instructions)

def __repr__(self):
name = format(self._name) if self._name else ""
instructions = ", ".join([repr(instr) for instr in self.instructions[:50]])
Expand Down
12 changes: 12 additions & 0 deletions test/python/pulse/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ def test_empty_schedule(self):
self.assertEqual(0, sched.start_time)
self.assertEqual(0, sched.stop_time)
self.assertEqual(0, sched.duration)
self.assertEqual(0, len(sched))
self.assertEqual((), sched._children)
self.assertEqual({}, sched.timeslots)
self.assertEqual([], list(sched.instructions))
self.assertFalse(sched)

def test_overlapping_schedules(self):
"""Test overlapping schedules."""
Expand Down Expand Up @@ -530,6 +532,16 @@ def test_timeslots(self):
self.assertEqual(
reference_sched.timeslots[DriveChannel(1)], [(10, 60), (100, 100)])

def test_len(self):
"""Test __len__ method"""
sched = Schedule()
self.assertEqual(len(sched), 0)

lp0 = self.linear(duration=3, slope=0.2, intercept=0.1)
for j in range(1, 10):
sched = sched.append(Play(lp0, self.config.drive(0)))
self.assertEqual(len(sched), j)


class TestDelay(BaseTestSchedule):
"""Test Delay Instruction"""
Expand Down

0 comments on commit ae74c84

Please sign in to comment.