Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Nov 23, 2023
1 parent dab6e54 commit 18da8e6
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 279 deletions.
16 changes: 9 additions & 7 deletions qiskit/providers/fake_provider/fake_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numpy as np

from qiskit import pulse
from qiskit.pulse.instruction_schedule_map import InstructionScheduleMap
from qiskit.circuit import Measure, Parameter, Delay, Reset, QuantumCircuit, Instruction
from qiskit.circuit.controlflow import (
IfElseOp,
Expand Down Expand Up @@ -77,8 +78,7 @@ def __init__(
(defaults to False).
calibrate_gates (list[str] | None): List of gate names which should contain
default calibration entries (overriden if an ``instruction_schedule_map`` is
provided). These must be a subset of ``basis_gates``.
default calibration entries. These must be a subset of ``basis_gates``.
rng (np.random.Generator): Optional fixed-seed generator for default random values.
"""
Expand Down Expand Up @@ -138,9 +138,9 @@ def __init__(
# generate block of calibration defaults and add to target
# Note: this could be improved if we could generate and add
# calibration defaults per-gate, and not as a block.
if calibrate_gates is not None:
defaults = self._generate_calibration_defaults(calibrate_gates)
self.add_calibration_defaults(defaults)
defaults = self._generate_calibration_defaults(calibrate_gates)
inst_map = defaults.instruction_schedule_map
self.add_calibrations_from_instruction_schedule_map(inst_map)

@property
def supported_operations(self) -> dict[str, Instruction]:
Expand Down Expand Up @@ -222,7 +222,9 @@ def add_noisy_instruction(

self.add_instruction(instruction, props)

def add_calibration_defaults(self, defaults: PulseDefaults) -> None:
def add_calibrations_from_instruction_schedule_map(
self, inst_map: InstructionScheduleMap
) -> None:
"""Add calibration entries from provided pulse defaults to target.
Args:
Expand All @@ -231,7 +233,6 @@ def add_calibration_defaults(self, defaults: PulseDefaults) -> None:
Returns:
None
"""
inst_map = defaults.instruction_schedule_map
for inst in inst_map.instructions:
for qarg in inst_map.qubits_with_instruction(inst):
try:
Expand All @@ -257,6 +258,7 @@ def _generate_calibration_defaults(self, calibrate_gates: list[str] | None) -> P
Returns:
Corresponding PulseDefaults
"""
calibrate_gates = calibrate_gates or []
measure_command_sequence = [
PulseQobjInstruction(
name="acquire",
Expand Down
4 changes: 1 addition & 3 deletions test/python/pulse/test_builder_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ class TestBuilderV2(QiskitTestCase):
def setUp(self):
super().setUp()
basis_gates = ["cx", "id", "rz", "sx", "x"]
self.backend = FakeGeneric(
basis_gates=basis_gates, coupling_map=MUMBAI_CMAP, calibrate_gates=[]
)
self.backend = FakeGeneric(basis_gates=basis_gates, num_qubits=27, coupling_map=MUMBAI_CMAP)

def assertScheduleEqual(self, program, target):
"""Assert an error when two pulse programs are not equal.
Expand Down
36 changes: 14 additions & 22 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, FakeHanoi, FakeGeneric
from qiskit.providers.fake_provider import FakeOpenPulse2Q, FakeHanoi, FakeHanoiV2
from qiskit.test import QiskitTestCase


Expand All @@ -34,13 +34,8 @@ class TestMeasure(QiskitTestCase):
def setUp(self):
super().setUp()
self.backend = FakeOpenPulse2Q()
self.backend_v2 = FakeHanoiV2()
self.inst_map = self.backend.defaults().instruction_schedule_map
self.backend_v1 = FakeHanoi()
self.backend_v2 = FakeGeneric(
basis_gates=["cx", "id", "rz", "sx", "x"],
num_qubits=27,
instruction_schedule_map=self.backend_v1.defaults().instruction_schedule_map,
)

def test_measure(self):
"""Test macro - measure."""
Expand Down Expand Up @@ -154,15 +149,13 @@ 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=self.backend_v1)
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=FakeHanoi())
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=self.backend_v1, qubit_mem_slots={0: 2}
)
sched_measure_v1 = macros.measure(qubits=[0], backend=FakeHanoi(), qubit_mem_slots={0: 2})
sched_measure_v2 = macros.measure(
qubits=[0], backend=self.backend_v2, qubit_mem_slots={0: 2}
)
Expand All @@ -174,11 +167,11 @@ def test_output_with_measure_v1_and_measure_v2_sched_with_meas_map(self):
num_qubits_list_measure_v1 = list(range(FakeHanoi().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=self.backend_v1, meas_map=[num_qubits_list_measure_v1]
qubits=[0], backend=FakeHanoi(), meas_map=[num_qubits_list_measure_v1]
)
sched_with_meas_map_dict_v1 = macros.measure(
qubits=[0],
backend=self.backend_v1,
backend=FakeHanoi(),
meas_map={0: num_qubits_list_measure_v1, 1: num_qubits_list_measure_v1},
)
sched_with_meas_map_list_v2 = macros.measure(
Expand All @@ -200,7 +193,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=self.backend_v1)
sched_measure_v1 = macros.measure(qubits=[0, 1], backend=FakeHanoi())
sched_measure_v2 = macros.measure(qubits=[0, 1], backend=self.backend_v2)
self.assertEqual(sched_measure_v1.instructions, sched_measure_v2.instructions)

Expand All @@ -211,12 +204,8 @@ class TestMeasureAll(QiskitTestCase):
def setUp(self):
super().setUp()
self.backend = FakeOpenPulse2Q()
self.backend_v2 = FakeHanoiV2()
self.inst_map = self.backend.defaults().instruction_schedule_map
self.backend_v2 = FakeGeneric(
basis_gates=["cx", "id", "rz", "sx", "x"],
num_qubits=2,
instruction_schedule_map=self.inst_map,
)

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

def test_measure_all_v2(self):
"""Test measure_all function with backendV2."""
sched = macros.measure_all(self.backend)
backend_v1 = FakeHanoi()
sched = macros.measure_all(self.backend_v2)
expected = Schedule(
self.inst_map.get("measure", list(range(self.backend.configuration().num_qubits)))
backend_v1.defaults().instruction_schedule_map.get(
"measure", list(range(backend_v1.configuration().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=self.backend)
sched_measure_v1 = macros.measure_all(backend=FakeHanoi())
sched_measure_v2 = macros.measure_all(backend=self.backend_v2)
self.assertEqual(sched_measure_v1.instructions, sched_measure_v2.instructions)
12 changes: 7 additions & 5 deletions test/python/transpiler/test_csp_layout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
# (C) Copyright IBM 2019, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -20,7 +20,7 @@
from qiskit.transpiler.passes import CSPLayout
from qiskit.converters import circuit_to_dag
from qiskit.test import QiskitTestCase
from qiskit.providers.fake_provider import FakeGeneric
from qiskit.providers.fake_provider.fake_generic import GenericTarget
from qiskit.utils import optionals

# fmt: off
Expand Down Expand Up @@ -115,9 +115,11 @@ def test_3q_circuit_5q_coupling_with_target(self):
[4, 2],
[4, 3],
]
target = FakeGeneric(
basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=yorktown_cm
).target
target = GenericTarget(
basis_gates=["cx", "id", "rz", "sx", "x"],
num_qubits=5,
coupling_map=CouplingMap(yorktown_cm),
)

qr = QuantumRegister(3, "qr")
circuit = QuantumCircuit(qr)
Expand Down
45 changes: 30 additions & 15 deletions test/python/transpiler/test_pulse_gate_pass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -16,9 +16,13 @@

from qiskit import pulse, circuit, transpile
from qiskit.providers.fake_provider import FakeAthens, FakeGeneric
from qiskit.providers.fake_provider.fake_generic import GenericTarget
from qiskit.quantum_info.random import random_unitary
from qiskit.transpiler import CouplingMap
from qiskit.test import QiskitTestCase

ATHENS_CMAP = CouplingMap([[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2], [3, 4], [4, 3]])


@ddt.ddt
class TestPulseGate(QiskitTestCase):
Expand Down Expand Up @@ -69,8 +73,9 @@ def test_transpile_with_bare_backend(self):

def test_transpile_with_backend_target(self):
"""Test transpile without custom calibrations from target."""
backend = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5)
target = backend.target
target = GenericTarget(
num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=ATHENS_CMAP
)

qc = circuit.QuantumCircuit(2)
qc.sx(0)
Expand Down Expand Up @@ -109,8 +114,12 @@ def test_transpile_with_custom_basis_gate(self):

def test_transpile_with_custom_basis_gate_in_target(self):
"""Test transpile with custom calibrations."""
backend = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5)
target = backend.target
target = GenericTarget(
num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=ATHENS_CMAP
)
target.add_calibrations_from_instruction_schedule_map(
FakeAthens().defaults().instruction_schedule_map
)
target["sx"][(0,)].calibration = self.custom_sx_q0
target["sx"][(1,)].calibration = self.custom_sx_q1

Expand Down Expand Up @@ -276,14 +285,18 @@ def test_transpile_with_both_instmap_and_empty_target(self, opt_level):
Test case from Qiskit/qiskit-terra/#9489
"""
backend = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5)
instmap = backend.target.instruction_schedule_map()
instmap = FakeAthens().defaults().instruction_schedule_map
instmap.add("sx", (0,), self.custom_sx_q0)
instmap.add("sx", (1,), self.custom_sx_q1)
instmap.add("cx", (0, 1), self.custom_cx_q01)

# This doesn't have custom schedule definition
target = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5).target
target = GenericTarget(
num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=ATHENS_CMAP
)
target.add_calibrations_from_instruction_schedule_map(
FakeAthens().defaults().instruction_schedule_map
)

qc = circuit.QuantumCircuit(2)
qc.append(random_unitary(4, seed=123), [0, 1])
Expand Down Expand Up @@ -323,10 +336,11 @@ def test_transpile_with_instmap_with_v2backend(self, opt_level):
qc.append(random_unitary(4, seed=123), [0, 1])
qc.measure_all()

from qiskit.pulse import InstructionScheduleMap
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"])
backend.target.add_calibrations_from_instruction_schedule_map(
FakeAthens().defaults().instruction_schedule_map
)

backend = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5)
# backend.target.update_from_instruction_schedule_map(InstructionScheduleMap())
transpiled_qc = transpile(
qc,
backend,
Expand Down Expand Up @@ -366,9 +380,10 @@ def test_transpile_with_instmap_with_v2backend_with_custom_gate(self, opt_level)
qc.append(gate, [0])
qc.measure_all()

backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"])
transpiled_qc = transpile(
qc,
FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5),
backend,
optimization_level=opt_level,
inst_map=instmap,
initial_layout=[0],
Expand All @@ -388,10 +403,10 @@ def test_transpile_with_instmap_not_mutate_backend(self):
This should not override the source object since the same backend may
be used for future transpile without intention of instruction overriding.
"""
backend = FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5)
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"])
original_sx0 = backend.target["sx"][(0,)].calibration

instmap = backend.target.instruction_schedule_map()
instmap = FakeAthens().defaults().instruction_schedule_map
instmap.add("sx", (0,), self.custom_sx_q0)

qc = circuit.QuantumCircuit(1)
Expand All @@ -400,7 +415,7 @@ def test_transpile_with_instmap_not_mutate_backend(self):

transpiled_qc = transpile(
qc,
FakeGeneric(basis_gates=["cx", "id", "rz", "sx", "x"], num_qubits=5),
backend,
inst_map=instmap,
initial_layout=[0],
)
Expand Down
Loading

0 comments on commit 18da8e6

Please sign in to comment.