Skip to content

Commit

Permalink
address deprecation and property change in fake backends
Browse files Browse the repository at this point in the history
  • Loading branch information
nkanazawa1989 committed Jun 19, 2024
1 parent 188eb5c commit f372eb3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 74 deletions.
99 changes: 38 additions & 61 deletions test/framework/test_backend_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,44 @@ class TestBackendTiming(QiskitExperimentsTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()

cls.acquire_alignment = 16
cls.dt = 1 / 4.5e9
cls.granularity = 16
cls.min_length = 64
cls.pulse_alignment = 1

def setUp(self):
super().setUp()
# Creating a complete fake backend is difficult so we use one from
# qiskit. Just to be safe, we check that the properties we care about
# for these tests are never changed from what the tests assume.
backend = FakeNairobiV2()
target = backend.target
assumptions = (
(abs(target.dt * 4.5e9 - 1) < 1e-6)
and target.acquire_alignment == 16
and target.pulse_alignment == 1
and target.min_length == 64
and target.granularity == 16
)
if not assumptions: # pragma: no cover
raise ValueError("FakeNairobiV2 properties have changed!")

cls.acquire_alignment = target.acquire_alignment
cls.dt = target.dt
cls.granularity = target.granularity
cls.min_length = target.min_length
cls.pulse_alignment = target.pulse_alignment
# qiskit. Just to be safe, we override hardware properties
# with the values assumed for the unit tests.
self.backend = FakeNairobiV2()
self.backend.target.dt = self.dt
self.backend.target.acquire_alignment = self.acquire_alignment
self.backend.target.pulse_alignment = self.pulse_alignment
self.backend.target.min_length = self.min_length
self.backend.target.granularity = self.granularity

@data((True, "s"), (False, "dt"))
@unpack
def test_delay_unit(self, null_dt, result):
"""Test delay unit matches dt"""
backend = FakeNairobiV2()
if null_dt:
backend.target.dt = None
timing = BackendTiming(backend)
self.backend.target.dt = None
timing = BackendTiming(self.backend)
self.assertEqual(timing.delay_unit, result)

def test_round_delay_args(self):
"""Test argument checking in round_delay"""
backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
with self.assertRaises(QiskitError):
timing.round_delay(time=self.dt * 16, samples=16)
with self.assertRaises(QiskitError):
timing.round_delay()

def test_round_pulse_args(self):
"""Test argument checking in round_pulse"""
backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
with self.assertRaises(QiskitError):
timing.round_pulse(time=self.dt * 64, samples=64)
with self.assertRaises(QiskitError):
Expand All @@ -84,25 +77,22 @@ def test_round_delay(self, samples_in, samples_out):
"""Test delay calculation with time input"""
time = self.dt * samples_in

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertEqual(timing.round_delay(time=time), samples_out)

def test_round_delay_no_dt(self):
"""Test delay when dt is None"""
time = self.dt * 16

backend = FakeNairobiV2()
backend.target.dt = None
timing = BackendTiming(backend)
self.backend.target.dt = None
timing = BackendTiming(self.backend)
self.assertEqual(timing.round_delay(time=time), time)

@data([14, 16], [16, 16], [18, 16], [64.5, 64])
@unpack
def test_round_delay_samples_in(self, samples_in, samples_out):
"""Test delay calculation with samples input"""
backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertEqual(timing.round_delay(samples=samples_in), samples_out)

@data([12, 64], [65, 64], [79, 80], [83, 80])
Expand All @@ -111,34 +101,30 @@ def test_round_pulse(self, samples_in, samples_out):
"""Test round pulse calculation with time input"""
time = self.dt * samples_in

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertEqual(timing.round_pulse(time=time), samples_out)

@data([12, 64], [65, 64], [79, 80], [83, 80], [80.5, 80])
@unpack
def test_round_pulse_samples_in(self, samples_in, samples_out):
"""Test round pulse calculation with samples input"""
backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertEqual(timing.round_pulse(samples=samples_in), samples_out)

def test_delay_time(self):
"""Test delay_time calculation"""
time_in = self.dt * 16.1
time_out = self.dt * 16

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertAlmostEqual(timing.delay_time(time=time_in), time_out, delta=1e-6 * self.dt)

def test_delay_time_samples_in(self):
"""Test delay_time calculation"""
samples_in = 16.1
time_out = self.dt * 16

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertAlmostEqual(
timing.delay_time(samples=samples_in), time_out, delta=1e-6 * self.dt
)
Expand All @@ -148,36 +134,32 @@ def test_delay_time_no_dt(self):
time_in = self.dt * 16.1
time_out = time_in

backend = FakeNairobiV2()
backend.target.dt = None
timing = BackendTiming(backend)
self.backend.target.dt = None
timing = BackendTiming(self.backend)
self.assertAlmostEqual(timing.delay_time(time=time_in), time_out, delta=1e-6 * self.dt)

def test_pulse_time(self):
"""Test pulse_time calculation"""
time_in = self.dt * 85.1
time_out = self.dt * 80

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertAlmostEqual(timing.pulse_time(time=time_in), time_out, delta=1e-6 * self.dt)

def test_pulse_time_samples_in(self):
"""Test pulse_time calculation"""
samples_in = 85.1
time_out = self.dt * 80

backend = FakeNairobiV2()
timing = BackendTiming(backend)
timing = BackendTiming(self.backend)
self.assertAlmostEqual(
timing.pulse_time(samples=samples_in), time_out, delta=1e-6 * self.dt
)

def test_round_pulse_no_dt_error(self):
"""Test methods that don't work when dt is None raise exceptions"""
backend = FakeNairobiV2()
backend.target.dt = None
timing = BackendTiming(backend)
self.backend.target.dt = None
timing = BackendTiming(self.backend)

time = self.dt * 81

Expand All @@ -186,19 +168,14 @@ def test_round_pulse_no_dt_error(self):

def test_unexpected_pulse_alignment(self):
"""Test that a weird pulse_alignment parameter is caught"""
backend = FakeNairobiV2()
backend.target.pulse_alignment = 33
timing = BackendTiming(backend)
self.backend.target.pulse_alignment = 33
timing = BackendTiming(self.backend)
with self.assertRaises(QiskitError):
timing.round_pulse(samples=81)

def test_unexpected_acquire_alignment(self):
"""Test that a weird acquire_alignment parameter is caught"""
backend = FakeNairobiV2()
try:
backend.target.acquire_alignment = 33
except AttributeError:
backend.target.aquire_alignment = 33
timing = BackendTiming(backend)
self.backend.target.acquire_alignment = 33
timing = BackendTiming(self.backend)
with self.assertRaises(QiskitError):
timing.round_pulse(samples=81)
4 changes: 2 additions & 2 deletions test/library/characterization/test_half_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from qiskit import pulse, transpile
from qiskit.pulse import InstructionScheduleMap
from qiskit_ibm_runtime.fake_provider import FakeAthens
from qiskit_ibm_runtime.fake_provider import FakeAthensV2

from qiskit_experiments.test.mock_iq_backend import MockIQBackend
from qiskit_experiments.test.mock_iq_helpers import MockIQHalfAngleHelper as HalfAngleHelper
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_circuits(self):
# mimic what will happen in the experiment.
transpile_opts = copy.copy(hac.transpile_options.__dict__)
transpile_opts["initial_layout"] = list(hac._physical_qubits)
circuits = transpile(hac.circuits(), FakeAthens(), **transpile_opts)
circuits = transpile(hac.circuits(), FakeAthensV2(), **transpile_opts)

for idx, circ in enumerate(circuits):
self.assertEqual(circ.count_ops()["sx"], idx * 2 + 2)
Expand Down
19 changes: 8 additions & 11 deletions test/library/randomized_benchmarking/test_interleaved_rb.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit.transpiler import InstructionProperties
from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel, depolarizing_error
from qiskit_ibm_runtime.fake_provider import FakeManila, FakeManilaV2, FakeWashington
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
from qiskit_experiments.library import randomized_benchmarking as rb


Expand All @@ -34,8 +34,7 @@ class TestInterleavedRB(QiskitExperimentsTestCase, RBTestMixin):
def setUp(self):
"""Setup the tests."""
super().setUp()
self.backend = FakeManila()
self.backend_with_timing_constraint = FakeWashington()
self.backend = FakeManilaV2()

# ### Tests for configuration ###
def test_non_clifford_interleaved_element(self):
Expand All @@ -56,7 +55,7 @@ def test_interleaving_delay_with_invalid_duration(self, duration, unit):
interleaved_element=Delay(duration, unit=unit),
physical_qubits=[0],
lengths=[1, 2, 3],
backend=self.backend_with_timing_constraint,
backend=self.backend,
)

def test_experiment_config(self):
Expand Down Expand Up @@ -269,29 +268,27 @@ def test_interleaved_circuit_is_decomposed(self):

def test_interleaving_cnot_gate_with_non_supported_direction(self):
"""Test if fails to interleave cx(1, 2) for backend that support only cx(2, 1)."""
my_backend = FakeManilaV2()
del my_backend.target["cx"][(1, 2)] # make support only cx(2, 1)
del self.backend.target["cx"][(1, 2)] # make support only cx(2, 1)

exp = rb.InterleavedRB(
interleaved_element=CXGate(),
physical_qubits=(1, 2),
lengths=[3],
num_samples=4,
backend=my_backend,
backend=self.backend,
seed=1234,
)
with self.assertRaises(QiskitError):
exp.circuits()

def test_interleaving_three_qubit_gate_with_calibration(self):
"""Test if circuits for 3Q InterleavedRB contain custom calibrations supplied via target."""
my_backend = FakeManilaV2()
with pulse.build(my_backend) as custom_3q_sched: # meaningless schedule
with pulse.build(self.backend) as custom_3q_sched: # meaningless schedule
pulse.play(pulse.GaussianSquare(1600, 0.2, 64, 1300), pulse.drive_channel(0))

physical_qubits = (2, 1, 3)
custom_3q_gate = self.ThreeQubitGate()
my_backend.target.add_instruction(
self.backend.target.add_instruction(
custom_3q_gate, {physical_qubits: InstructionProperties(calibration=custom_3q_sched)}
)

Expand All @@ -300,7 +297,7 @@ def test_interleaving_three_qubit_gate_with_calibration(self):
physical_qubits=physical_qubits,
lengths=[3],
num_samples=1,
backend=my_backend,
backend=self.backend,
seed=1234,
)
circuits = exp._transpiled_circuits()
Expand Down

0 comments on commit f372eb3

Please sign in to comment.