Skip to content

Commit

Permalink
Attempt to address test_macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Oct 3, 2023
1 parent 911c9e7 commit 9942ec3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
27 changes: 20 additions & 7 deletions qiskit/providers/fake_provider/fake_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Command,
)
from qiskit.qobj import PulseQobjInstruction
from qiskit.pulse import InstructionScheduleMap


class FakeGeneric(BackendV2):
Expand Down Expand Up @@ -114,6 +115,7 @@ def __init__(
enable_reset: bool = True,
dt: float = 0.222e-9,
skip_calibration_gates: Optional[List[str]] = [],
instruction_schedule_map: Optional[InstructionScheduleMap] = None,
):

super().__init__(
Expand Down Expand Up @@ -164,6 +166,7 @@ def __init__(
)
for _ in range(num_qubits)
],
concurrent_measurements=[[i for i in range(num_qubits)]],
)

instruction_dict = self._get_instruction_dict(num_qubits, coupling_map)
Expand All @@ -188,7 +191,11 @@ def __init__(

defaults = self._build_calibration_defaults(skip_calibration_gates)

inst_map = defaults.instruction_schedule_map
if not instruction_schedule_map:
inst_map = defaults.instruction_schedule_map
else:
inst_map = instruction_schedule_map

for inst in inst_map.instructions:
for qarg in inst_map.qubits_with_instruction(inst):
try:
Expand All @@ -198,12 +205,12 @@ def __init__(
# Do NOT call .get method. This parses Qpbj immediately.
# This operation is computationally expensive and should be bypassed.
calibration_entry = inst_map._get_calibration_entry(inst, qargs)
if inst in self._target._gate_map:
if inst in self._target:
if inst == "measure":
for qubit in qargs:
self._target._gate_map[inst][(qubit,)].calibration = calibration_entry
elif qargs in self._target._gate_map[inst] and inst != "delay":
self._target._gate_map[inst][qargs].calibration = calibration_entry
self._target[inst][(qubit,)].calibration = calibration_entry
elif qargs in self._target[inst] and inst is not "delay":
self._target[inst][qargs].calibration = calibration_entry

@property
def target(self):
Expand Down Expand Up @@ -324,14 +331,15 @@ def _build_calibration_defaults(self, skip_calibration_gates) -> PulseDefaults:
measure_command_sequence = [
PulseQobjInstruction(
name="acquire",
duration=10,
duration=1792,
t0=0,
qubits=list(range(self.num_qubits)),
memory_slot=list(range(self.num_qubits)),
).to_dict()
]

measure_command_sequence += [
PulseQobjInstruction(name="test_pulse_1", ch=f"m{i}", t0=0).to_dict()
PulseQobjInstruction(name="test_pulse_1", ch=f"m{i}", duration=1792, t0=0).to_dict()
for i in range(self.num_qubits)
]

Expand Down Expand Up @@ -395,3 +403,8 @@ def _build_calibration_defaults(self, skip_calibration_gates) -> PulseDefaults:
def run(self, circuit, **kwargs):
noise_model = None
return BasicAer.get_backend("qasm_simulator").run(circuit, **kwargs)

@property
def meas_map(self) -> List[List[int]]:
return self._target.concurrent_measurements

33 changes: 18 additions & 15 deletions test/python/pulse/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from qiskit.pulse import macros
from qiskit.pulse.exceptions import PulseError
from qiskit.providers.fake_provider import FakeOpenPulse2Q, Fake27QV1Pulse, FakeGeneric
from qiskit.providers.fake_provider import FakeOpenPulse2Q, Fake27QV1Pulse, FakeGeneric, FakeHanoiV2
from qiskit.test import QiskitTestCase


Expand All @@ -34,8 +34,12 @@ class TestMeasure(QiskitTestCase):
def setUp(self):
super().setUp()
self.backend = FakeOpenPulse2Q()
self.backend_v2 = FakeGeneric(num_qubits=27)
self.inst_map = self.backend.defaults().instruction_schedule_map
self.backend_v1 = Fake27QV1Pulse()
self.backend_v2 = FakeGeneric(
num_qubits=27,
instruction_schedule_map=self.backend_v1.defaults().instruction_schedule_map,
)

def test_measure(self):
"""Test macro - measure."""
Expand Down Expand Up @@ -149,14 +153,14 @@ def test_multiple_measure_v2(self):

def test_output_with_measure_v1_and_measure_v2(self):
"""Test make outputs of measure_v1 and measure_v2 consistent."""
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=Fake27QV1Pulse())
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=self.backend_v1)
sched_measure_v2 = macros.measure(qubits=[0, 1], backend=self.backend_v2)
self.assertEqual(sched_measure_v1.instructions, sched_measure_v2.instructions)

def test_output_with_measure_v1_and_measure_v2_sched_with_qubit_mem_slots(self):
"""Test make outputs of measure_v1 and measure_v2 with custom qubit_mem_slots consistent."""
sched_measure_v1 = macros.measure(
qubits=[0], backend=Fake27QV1Pulse(), qubit_mem_slots={0: 2}
qubits=[0], backend=self.backend_v1, qubit_mem_slots={0: 2}
)
sched_measure_v2 = macros.measure(
qubits=[0], backend=self.backend_v2, qubit_mem_slots={0: 2}
Expand All @@ -169,11 +173,11 @@ def test_output_with_measure_v1_and_measure_v2_sched_with_meas_map(self):
num_qubits_list_measure_v1 = list(range(Fake27QV1Pulse().configuration().num_qubits))
num_qubits_list_measure_v2 = list(range(self.backend_v2.num_qubits))
sched_with_meas_map_list_v1 = macros.measure(
qubits=[0], backend=Fake27QV1Pulse(), meas_map=[num_qubits_list_measure_v1]
qubits=[0], backend=self.backend_v1, meas_map=[num_qubits_list_measure_v1]
)
sched_with_meas_map_dict_v1 = macros.measure(
qubits=[0],
backend=Fake27QV1Pulse(),
backend=self.backend_v1,
meas_map={0: num_qubits_list_measure_v1, 1: num_qubits_list_measure_v1},
)
sched_with_meas_map_list_v2 = macros.measure(
Expand All @@ -195,7 +199,7 @@ def test_output_with_measure_v1_and_measure_v2_sched_with_meas_map(self):

def test_output_with_multiple_measure_v1_and_measure_v2(self):
"""Test macro - consistent output of multiple qubit measure with backendV1 and backendV2."""
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=Fake27QV1Pulse())
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=self.backend_v1)
sched_measure_v2 = macros.measure(qubits=[0, 1], backend=self.backend_v2)
self.assertEqual(sched_measure_v1.instructions, sched_measure_v2.instructions)

Expand All @@ -206,8 +210,10 @@ class TestMeasureAll(QiskitTestCase):
def setUp(self):
super().setUp()
self.backend = FakeOpenPulse2Q()
self.backend_v2 = FakeGeneric(num_qubits=27)
self.inst_map = self.backend.defaults().instruction_schedule_map
# self.backend_v1 = Fake27QV1Pulse()
self.backend_v2 = FakeGeneric(num_qubits=2, instruction_schedule_map=self.inst_map)
# self.backend_v2 = FakeHanoiV2()

def test_measure_all(self):
"""Test measure_all function."""
Expand All @@ -217,17 +223,14 @@ def test_measure_all(self):

def test_measure_all_v2(self):
"""Test measure_all function with backendV2."""
backend_v1 = Fake27QV1Pulse()
inst_map = self.backend_v2.instruction_schedule_map
sched = macros.measure_all(self.backend_v2)
expected = Schedule(
backend_v1.defaults().instruction_schedule_map.get(
"measure", list(range(backend_v1.configuration().num_qubits))
)
)
expected = Schedule(inst_map.get("measure", list(range(self.backend_v2.num_qubits))))
self.assertEqual(sched.instructions, expected.instructions)

def test_output_of_measure_all_with_backend_v1_and_v2(self):
"""Test make outputs of measure_all with backendV1 and backendV2 consistent."""
sched_measure_v1 = macros.measure_all(backend=Fake27QV1Pulse())
sched_measure_v1 = macros.measure_all(backend=self.backend)
sched_measure_v2 = macros.measure_all(backend=self.backend_v2)
self.assertEqual(sched_measure_v1.instructions, sched_measure_v2.instructions)

0 comments on commit 9942ec3

Please sign in to comment.