Skip to content

Commit

Permalink
Fixes Qiskit#7078: Add unit tests for restrictions on Classical IO ch…
Browse files Browse the repository at this point in the history
…annels
  • Loading branch information
pollyshaw committed Jul 18, 2022
1 parent 65f06a4 commit 45d3f03
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
6 changes: 6 additions & 0 deletions qiskit/pulse/instructions/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@ def __init__(
frequency: Frequency shift of the channel in Hz.
channel: The channel this instruction operates on.
name: Name of this set channel frequency instruction.
Raises:
PulseError: If channel is not a PulseChannel.
"""
if not isinstance(frequency, ParameterExpression):
frequency = float(frequency)
if not isinstance(channel, PulseChannel):
raise PulseError(
"The `channel` argument to `SetFrequency` must be of type `channels.PulseChannel`."
)
super().__init__(operands=(frequency, channel), name=name)

@property
Expand Down
6 changes: 5 additions & 1 deletion test/python/pulse/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
from qiskit.pulse.channels import (
AcquireChannel,
Channel,
DriveChannel,
ClassicalIOChannel,
ControlChannel,
DriveChannel,
MeasureChannel,
MemorySlot,
PulseChannel,
Expand Down Expand Up @@ -76,6 +77,7 @@ def test_default(self):

self.assertEqual(memory_slot.index, 123)
self.assertEqual(memory_slot.name, "m123")
self.assertTrue(isinstance(memory_slot, ClassicalIOChannel))


class TestRegisterSlot(QiskitTestCase):
Expand All @@ -87,6 +89,7 @@ def test_default(self):

self.assertEqual(register_slot.index, 123)
self.assertEqual(register_slot.name, "c123")
self.assertTrue(isinstance(register_slot, ClassicalIOChannel))


class TestSnapshotChannel(QiskitTestCase):
Expand All @@ -98,6 +101,7 @@ def test_default(self):

self.assertEqual(snapshot_channel.index, 0)
self.assertEqual(snapshot_channel.name, "s0")
self.assertTrue(isinstance(snapshot_channel, ClassicalIOChannel))


class TestDriveChannel(QiskitTestCase):
Expand Down
63 changes: 63 additions & 0 deletions test/python/pulse/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,64 @@ def test_freq(self):
)
self.assertEqual(repr(set_freq), "SetFrequency(4500000000.0, DriveChannel(1), name='test')")

def test_freq_non_pulse_channel(self):
"""Test set frequency constructor with illegal channel"""
with self.assertRaises(exceptions.PulseError):
instructions.SetFrequency(4.5e9, channels.RegisterSlot(1), name="test")


class TestShiftFrequency(QiskitTestCase):
"""Shift frequency tests."""

def test_shift_freq(self):
"""Test shift frequency basic functionality."""
shift_freq = instructions.ShiftFrequency(4.5e9, channels.DriveChannel(1), name="test")

self.assertIsInstance(shift_freq.id, int)
self.assertEqual(shift_freq.duration, 0)
self.assertEqual(shift_freq.frequency, 4.5e9)
self.assertEqual(shift_freq.operands, (4.5e9, channels.DriveChannel(1)))
self.assertEqual(
shift_freq, instructions.ShiftFrequency(4.5e9, channels.DriveChannel(1), name="test")
)
self.assertNotEqual(
shift_freq, instructions.ShiftFrequency(4.5e8, channels.DriveChannel(1), name="test")
)
self.assertEqual(
repr(shift_freq), "ShiftFrequency(4500000000.0, DriveChannel(1), name='test')"
)

def test_freq_non_pulse_channel(self):
"""Test shift frequency constructor with illegal channel"""
with self.assertRaises(exceptions.PulseError):
instructions.ShiftFrequency(4.5e9, channels.RegisterSlot(1), name="test")


class TestSetPhase(QiskitTestCase):
"""Test the instruction construction."""

def test_default(self):
"""Test basic SetPhase."""
set_phase = instructions.SetPhase(1.57, channels.DriveChannel(0))

self.assertIsInstance(set_phase.id, int)
self.assertEqual(set_phase.name, None)
self.assertEqual(set_phase.duration, 0)
self.assertEqual(set_phase.phase, 1.57)
self.assertEqual(set_phase.operands, (1.57, channels.DriveChannel(0)))
self.assertEqual(
set_phase, instructions.SetPhase(1.57, channels.DriveChannel(0), name="test")
)
self.assertNotEqual(
set_phase, instructions.SetPhase(1.57j, channels.DriveChannel(0), name="test")
)
self.assertEqual(repr(set_phase), "SetPhase(1.57, DriveChannel(0))")

def test_set_phase_non_pulse_channel(self):
"""Test shift phase constructor with illegal channel"""
with self.assertRaises(exceptions.PulseError):
instructions.SetPhase(1.57, channels.RegisterSlot(1), name="test")


class TestShiftPhase(QiskitTestCase):
"""Test the instruction construction."""
Expand All @@ -177,6 +235,11 @@ def test_default(self):
)
self.assertEqual(repr(shift_phase), "ShiftPhase(1.57, DriveChannel(0))")

def test_shift_phase_non_pulse_channel(self):
"""Test shift phase constructor with illegal channel"""
with self.assertRaises(exceptions.PulseError):
instructions.ShiftPhase(1.57, channels.RegisterSlot(1), name="test")


class TestSnapshot(QiskitTestCase):
"""Snapshot tests."""
Expand Down
19 changes: 18 additions & 1 deletion test/python/pulse/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
Constant,
)
from qiskit.pulse import transforms, instructions
from qiskit.pulse.channels import MemorySlot, DriveChannel, AcquireChannel
from qiskit.pulse.channels import (
MemorySlot,
DriveChannel,
AcquireChannel,
RegisterSlot,
SnapshotChannel,
)
from qiskit.pulse.instructions import directives
from qiskit.test import QiskitTestCase
from qiskit.providers.fake_provider import FakeOpenPulse2Q
Expand Down Expand Up @@ -349,6 +355,17 @@ def test_padding_prepended_delay(self):

self.assertEqual(transforms.pad(sched, until=30, inplace=True), ref_sched)

def test_pad_no_delay_on_classical_io_channels(self):
"""Test padding does not apply to classical IO channels."""
delay = 10
sched = (
Delay(delay, MemorySlot(0)).shift(20)
+ Delay(delay, RegisterSlot(0)).shift(10)
+ Delay(delay, SnapshotChannel())
)

self.assertEqual(transforms.pad(sched, until=15), sched)


def get_pulse_ids(schedules: List[Schedule]) -> Set[int]:
"""Returns ids of pulses used in Schedules."""
Expand Down

0 comments on commit 45d3f03

Please sign in to comment.