Skip to content

Commit 27cf27c

Browse files
authored
Fix error in schedule -> signal conversion for schedules with barrier instructions (#203)
1 parent 1239122 commit 27cf27c

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

qiskit_dynamics/pulse/pulse_to_signals.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ def get_signals(self, schedule: Schedule) -> List[DiscreteSignal]:
163163
)
164164

165165
for start_sample, inst in schedule.instructions:
166-
chan = inst.channel.name
167-
phi = phases[chan]
168-
freq = frequency_shifts[chan]
166+
# get channel name if instruction has it
167+
chan = inst.channel.name if hasattr(inst, "channel") else None
168+
169169
if isinstance(inst, Play):
170170
# get the instruction samples
171171
inst_samples = None
@@ -178,8 +178,8 @@ def get_signals(self, schedule: Schedule) -> List[DiscreteSignal]:
178178
times = self._dt * (start_sample + np.arange(len(inst_samples)))
179179
samples = inst_samples * np.exp(
180180
Array(
181-
2.0j * np.pi * freq * times
182-
+ 1.0j * phi
181+
2.0j * np.pi * frequency_shifts[chan] * times
182+
+ 1.0j * phases[chan]
183183
+ 2.0j * np.pi * phase_accumulations[chan]
184184
)
185185
)

releasenotes/notes/0.4/0.4-summary-3de98711c3b7aa09.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ fixes:
8383
:class:`~qiskit.pulse.instructions.SetFrequency` and
8484
:class:`~qiskit.pulse.instructions.ShiftFrequency` instructions has also been fixed. (`#140
8585
<https://github.com/Qiskit/qiskit-dynamics/issues/140>`__)
86+
- |
87+
:class:`.InstructionToSignals` has been updated to fix an error when parsing schedules that
88+
include barrier instructions. `#202 <https://github.com/Qiskit/qiskit-dynamics/issues/202>`__)
8689
- |
8790
Fixes a bug in the automatic jit-compilation of :meth:`Solver.solve` when using the ``t_eval``
8891
kwarg with a JAX method and ``Array.default_backend() == 'jax'``. The bug is fixed by updating

test/dynamics/pulse/test_pulse_to_signals.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from qiskit import pulse
2121
from qiskit.pulse import Schedule
2222
from qiskit.pulse.transforms.canonicalization import block_to_schedule
23+
from qiskit.providers.fake_provider import FakeQuito
2324
from qiskit import QiskitError
2425

2526
from qiskit_dynamics.pulse import InstructionToSignals
@@ -320,6 +321,27 @@ def test_InstructionToSignals(self):
320321
signals = converter.get_signals(block_to_schedule(schedule))
321322
self.assertAllClose(signals[0].samples, gauss_get_waveform_samples, atol=1e-7, rtol=1e-7)
322323

324+
def test_barrier_instructions(self):
325+
"""Test correct parsing of schedule with barrier instructions."""
326+
327+
# this example needs any backend with at least 2 qubits
328+
backend = FakeQuito()
329+
330+
with pulse.build(backend) as sched_block:
331+
pulse.play(pulse.Constant(duration=3, amp=0.5), pulse.DriveChannel(0))
332+
pulse.barrier(0, 1)
333+
pulse.play(pulse.Constant(duration=3, amp=-0.5), pulse.DriveChannel(1))
334+
335+
converter = InstructionToSignals(
336+
dt=1.0, carriers={"d0": 1.0, "d1": 1.0}, channels=["d0", "d1"]
337+
)
338+
sched = block_to_schedule(sched_block)
339+
340+
sigs = converter.get_signals(sched)
341+
342+
self.assertAllClose(sigs[0].samples, np.array([0.5, 0.5, 0.5, 0.0, 0.0, 0.0]))
343+
self.assertAllClose(sigs[1].samples, np.array([0.0, 0.0, 0.0, -0.5, -0.5, -0.5]))
344+
323345

324346
class TestPulseToSignalsJAXTransformations(QiskitDynamicsTestCase, TestJaxBase):
325347
"""Tests InstructionToSignals class by using Jax."""

0 commit comments

Comments
 (0)