Skip to content

Commit

Permalink
Support AreaBarrier instruction in QPY
Browse files Browse the repository at this point in the history
  • Loading branch information
nkanazawa1989 committed Oct 19, 2022
1 parent 805a5b2 commit a02bcb5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion qiskit/qpy/type_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SetPhase,
ShiftPhase,
RelativeBarrier,
AreaBarrier,
)
from qiskit.pulse.library import Waveform, SymbolicPulse
from qiskit.pulse.schedule import ScheduleBlock
Expand Down Expand Up @@ -231,10 +232,11 @@ class ScheduleInstruction(TypeKeyBase):
SET_PHASE = b"q"
SHIFT_PHASE = b"r"
BARRIER = b"b"
AREA_BARRIER = b"c"

# 's' is reserved by ScheduleBlock, i.e. block can be nested as an element.
# Call instructon is not supported by QPY.
# This instruction is excluded from ScheduleBlock instructions with
# This instruction has been excluded from ScheduleBlock instructions with
# qiskit-terra/#8005 and new instruction Reference will be added instead.
# Call is only applied to Schedule which is not supported by QPY.
# Also snapshot is not suppored because of its limited usecase.
Expand All @@ -257,6 +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

raise exceptions.QpyError(
f"Object type '{type(obj)}' is not supported in {cls.__name__} namespace."
Expand All @@ -280,6 +284,8 @@ def retrieve(cls, type_key):
return ShiftPhase
if type_key == cls.BARRIER:
return RelativeBarrier
if type_key == cls.AREA_BARRIER:
return AreaBarrier

raise exceptions.QpyError(
f"A class corresponding to type key '{type_key}' is not found in {cls.__name__} namespace."
Expand Down
23 changes: 22 additions & 1 deletion test/python/qpy/test_block_load_from_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np

from qiskit.pulse import builder
from qiskit.pulse import builder, Schedule
from qiskit.pulse.library import (
SymbolicPulse,
Gaussian,
Expand All @@ -34,6 +34,7 @@
MemorySlot,
RegisterSlot,
)
from qiskit.pulse.instructions import Play, AreaBarrier
from qiskit.circuit import Parameter, QuantumCircuit, Gate
from qiskit.test import QiskitTestCase
from qiskit.qpy import dump, load
Expand Down Expand Up @@ -151,6 +152,12 @@ def test_barrier(self):
builder.barrier(DriveChannel(0), DriveChannel(1), ControlChannel(2))
self.assert_roundtrip_equal(test_sched)

def test_area_barrier(self):
"""Test area barrier."""
with builder.build() as test_sched:
builder.append_instruction(AreaBarrier(10, DriveChannel(0)))
self.assert_roundtrip_equal(test_sched)

def test_measure(self):
"""Test measurement."""
with builder.build() as test_sched:
Expand Down Expand Up @@ -187,6 +194,20 @@ def test_nested_blocks(self):
builder.delay(200, DriveChannel(1))
self.assert_roundtrip_equal(test_sched)

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.
"""
refsched = Schedule()
refsched.insert(20, Play(Constant(100, 0.1), DriveChannel(0)))
refsched.insert(50, Play(Constant(100, 0.1), DriveChannel(1)))

with builder.build() as test_sched:
builder.call(refsched, name="test_ref")
self.assert_roundtrip_equal(test_sched)

def test_bell_schedule(self):
"""Test complex schedule to create a Bell state."""
with builder.build() as test_sched:
Expand Down

0 comments on commit a02bcb5

Please sign in to comment.