From f372eb3efd474457f4b8959cd36d9c2d9ad175be Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Thu, 20 Jun 2024 00:49:36 +0900 Subject: [PATCH] address deprecation and property change in fake backends --- test/framework/test_backend_timing.py | 99 +++++++------------ .../characterization/test_half_angle.py | 4 +- .../test_interleaved_rb.py | 19 ++-- 3 files changed, 48 insertions(+), 74 deletions(-) diff --git a/test/framework/test_backend_timing.py b/test/framework/test_backend_timing.py index a7951b2c21..9e10c402b7 100644 --- a/test/framework/test_backend_timing.py +++ b/test/framework/test_backend_timing.py @@ -28,42 +28,36 @@ 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): @@ -71,8 +65,7 @@ def test_round_delay_args(self): 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): @@ -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]) @@ -111,16 +101,14 @@ 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): @@ -128,8 +116,7 @@ def test_delay_time(self): 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): @@ -137,8 +124,7 @@ def test_delay_time_samples_in(self): 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 ) @@ -148,9 +134,8 @@ 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): @@ -158,8 +143,7 @@ def test_pulse_time(self): 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): @@ -167,17 +151,15 @@ def test_pulse_time_samples_in(self): 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 @@ -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) diff --git a/test/library/characterization/test_half_angle.py b/test/library/characterization/test_half_angle.py index 7e2359b292..9d0ebfd58f 100644 --- a/test/library/characterization/test_half_angle.py +++ b/test/library/characterization/test_half_angle.py @@ -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 @@ -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) diff --git a/test/library/randomized_benchmarking/test_interleaved_rb.py b/test/library/randomized_benchmarking/test_interleaved_rb.py index f01fd6a764..47f46d4d07 100644 --- a/test/library/randomized_benchmarking/test_interleaved_rb.py +++ b/test/library/randomized_benchmarking/test_interleaved_rb.py @@ -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 @@ -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): @@ -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): @@ -269,15 +268,14 @@ 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): @@ -285,13 +283,12 @@ def test_interleaving_cnot_gate_with_non_supported_direction(self): 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)} ) @@ -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()