From 5ce3a97b5cbb51d9355fedc99f947dceb76d359a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Wed, 31 Jan 2024 18:25:14 +0100 Subject: [PATCH] Fix more tests --- .../fake_provider/generic_backend_v2.py | 3 +- test/python/compiler/test_transpiler.py | 3 +- test/python/legacy_cmaps.py | 148 ++++++++++++++++++ .../transpiler/test_calibrationbuilder.py | 5 +- .../transpiler/test_preset_passmanagers.py | 36 ++--- .../python/transpiler/test_pulse_gate_pass.py | 23 +-- test/python/transpiler/test_sabre_layout.py | 2 +- test/python/transpiler/test_trivial_layout.py | 1 + test/python/transpiler/test_vf2_layout.py | 2 +- 9 files changed, 179 insertions(+), 44 deletions(-) diff --git a/qiskit/providers/fake_provider/generic_backend_v2.py b/qiskit/providers/fake_provider/generic_backend_v2.py index 3d0502aeb617..867ed4890c04 100644 --- a/qiskit/providers/fake_provider/generic_backend_v2.py +++ b/qiskit/providers/fake_provider/generic_backend_v2.py @@ -434,7 +434,8 @@ def _add_noisy_instruction_to_target( instruction.name, qargs ) for qubit in qargs: - self._target[instruction.name][(qubit,)].calibration = calibration_entry + if qubit < self.num_qubits: + self._target[instruction.name][(qubit,)].calibration = calibration_entry def run(self, run_input, **options): """Run on the backend using a simulator. diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py index d43f2f9fd836..c979413ee707 100644 --- a/test/python/compiler/test_transpiler.py +++ b/test/python/compiler/test_transpiler.py @@ -204,7 +204,6 @@ def test_transpile_non_adjacent_layout(self): 13 - 12 - 11 - 10 - 9 - 8 - 7 """ - cmap = [ [0, 1], [0, 14], @@ -2292,7 +2291,7 @@ def test_parallel_num_processes_kwarg(self, num_processes): @data(0, 1, 2, 3) def test_parallel_dispatch(self, opt_level): """Test that transpile in parallel works for all optimization levels.""" - backend = GenericBackendV2(num_qubits=5) + backend = GenericBackendV2(num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], seed=42) qr = QuantumRegister(5) cr = ClassicalRegister(5) qc = QuantumCircuit(qr, cr) diff --git a/test/python/legacy_cmaps.py b/test/python/legacy_cmaps.py index 29d613a81c07..7ce931f00b83 100644 --- a/test/python/legacy_cmaps.py +++ b/test/python/legacy_cmaps.py @@ -279,3 +279,151 @@ [25, 26], [26, 25], ] + +# 65 qubits +MANHATTAN_CMAP = [ + [0, 1], + [0, 10], + [1, 0], + [1, 2], + [2, 1], + [2, 3], + [3, 2], + [3, 4], + [4, 3], + [4, 5], + [4, 11], + [5, 4], + [5, 6], + [6, 5], + [6, 7], + [7, 6], + [7, 8], + [8, 7], + [8, 9], + [8, 12], + [9, 8], + [10, 0], + [10, 13], + [11, 4], + [11, 17], + [12, 8], + [12, 21], + [13, 10], + [13, 14], + [14, 13], + [14, 15], + [15, 14], + [15, 16], + [15, 24], + [16, 15], + [16, 17], + [17, 11], + [17, 16], + [17, 18], + [18, 17], + [18, 19], + [19, 18], + [19, 20], + [19, 25], + [20, 19], + [20, 21], + [21, 12], + [21, 20], + [21, 22], + [22, 21], + [22, 23], + [23, 22], + [23, 26], + [24, 15], + [24, 29], + [25, 19], + [25, 33], + [26, 23], + [26, 37], + [27, 28], + [27, 38], + [28, 27], + [28, 29], + [29, 24], + [29, 28], + [29, 30], + [30, 29], + [30, 31], + [31, 30], + [31, 32], + [31, 39], + [32, 31], + [32, 33], + [33, 25], + [33, 32], + [33, 34], + [34, 33], + [34, 35], + [35, 34], + [35, 36], + [35, 40], + [36, 35], + [36, 37], + [37, 26], + [37, 36], + [38, 27], + [38, 41], + [39, 31], + [39, 45], + [40, 35], + [40, 49], + [41, 38], + [41, 42], + [42, 41], + [42, 43], + [43, 42], + [43, 44], + [43, 52], + [44, 43], + [44, 45], + [45, 39], + [45, 44], + [45, 46], + [46, 45], + [46, 47], + [47, 46], + [47, 48], + [47, 53], + [48, 47], + [48, 49], + [49, 40], + [49, 48], + [49, 50], + [50, 49], + [50, 51], + [51, 50], + [51, 54], + [52, 43], + [52, 56], + [53, 47], + [53, 60], + [54, 51], + [54, 64], + [55, 56], + [56, 52], + [56, 55], + [56, 57], + [57, 56], + [57, 58], + [58, 57], + [58, 59], + [59, 58], + [59, 60], + [60, 53], + [60, 59], + [60, 61], + [61, 60], + [61, 62], + [62, 61], + [62, 63], + [63, 62], + [63, 64], + [64, 54], + [64, 63], +] diff --git a/test/python/transpiler/test_calibrationbuilder.py b/test/python/transpiler/test_calibrationbuilder.py index 5c6110ad82e9..bc64d954a429 100644 --- a/test/python/transpiler/test_calibrationbuilder.py +++ b/test/python/transpiler/test_calibrationbuilder.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2020. +# (C) Copyright IBM 2020, 2024. # # 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 @@ -554,7 +554,6 @@ def test_pulse_schedule(self, theta: float): def test_with_normalizerxangles(self): """Checks that this pass works well with the NormalizeRXAngles pass.""" - backend = GenericBackendV2(num_qubits=5) # add Drag pulse to 'sx' calibrations sched = ScheduleBlock() sched.append( @@ -573,7 +572,7 @@ def test_with_normalizerxangles(self): ) ism = InstructionScheduleMap() ism.add("sx", (0,), sched) - backend.target.add_calibrations_from_instruction_schedule_map(ism) + backend = GenericBackendV2(num_qubits=5, calibrate_instructions=ism) # NormalizeRXAngle pass should also be included because it's a required pass. pm = PassManager(RXCalibrationBuilder(backend.target)) diff --git a/test/python/transpiler/test_preset_passmanagers.py b/test/python/transpiler/test_preset_passmanagers.py index ed509d494c4a..7b7d656d3b9e 100644 --- a/test/python/transpiler/test_preset_passmanagers.py +++ b/test/python/transpiler/test_preset_passmanagers.py @@ -344,9 +344,7 @@ def test_backend(self, level): qc = QuantumCircuit(qr) qc.cx(qr[2], qr[4]) - backend = GenericBackendV2( - num_qubits=14, coupling_map=MELBOURNE_CMAP - ) + backend = GenericBackendV2(num_qubits=14, coupling_map=MELBOURNE_CMAP) _ = transpile(qc, backend, optimization_level=level, callback=self.callback) @@ -469,9 +467,7 @@ def get_translation_stage_plugin(self): def test_level1_runs_vf2post_layout_when_routing_required(self): """Test that if we run routing as part of sabre layout VF2PostLayout runs.""" - target = GenericBackendV2( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = GenericBackendV2(num_qubits=7, coupling_map=LAGOS_CMAP) qc = QuantumCircuit(5) qc.h(0) qc.cy(0, 1) @@ -494,9 +490,7 @@ def test_level1_runs_vf2post_layout_when_routing_required(self): def test_level1_runs_vf2post_layout_when_routing_method_set_and_required(self): """Test that if we run routing as part of sabre layout VF2PostLayout runs.""" - target = GenericBackendV2( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = GenericBackendV2(num_qubits=7, coupling_map=LAGOS_CMAP) qc = QuantumCircuit(5) qc.h(0) qc.cy(0, 1) @@ -1303,9 +1297,7 @@ def test_with_backend(self, optimization_level): @data(0, 1, 2, 3) def test_with_no_backend(self, optimization_level): """Test a passmanager is constructed with no backend and optimization level.""" - target = GenericBackendV2( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = GenericBackendV2(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager( optimization_level, coupling_map=target.coupling_map, @@ -1320,9 +1312,7 @@ def test_with_no_backend(self, optimization_level): @data(0, 1, 2, 3) def test_with_no_backend_only_target(self, optimization_level): """Test a passmanager is constructed with a manual target and optimization level.""" - target = GenericBackendV2( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = GenericBackendV2(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager(optimization_level, target=target.target) self.assertIsInstance(pm, PassManager) @@ -1351,9 +1341,7 @@ def get_translation_stage_plugin(self): """Custom post translation stage.""" return "custom_stage_for_test" - target = TargetBackend( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = TargetBackend(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager(optimization_level, backend=target) self.assertIsInstance(pm, PassManager) @@ -1385,9 +1373,7 @@ def get_translation_stage_plugin(self): """Custom post translation stage.""" return "custom_stage_for_test" - target = TargetBackend( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = TargetBackend(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager(optimization_level, backend=target) self.assertIsInstance(pm, PassManager) @@ -1419,9 +1405,7 @@ def get_translation_stage_plugin(self): """Custom post translation stage.""" return "custom_stage_for_test" - target = TargetBackend( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = TargetBackend(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager(optimization_level, backend=target) self.assertIsInstance(pm, PassManager) @@ -1453,9 +1437,7 @@ def get_translation_stage_plugin(self): """Custom post translation stage.""" return "custom_stage_for_test" - target = TargetBackend( - num_qubits=7, coupling_map=LAGOS_CMAP - ) + target = TargetBackend(num_qubits=7, coupling_map=LAGOS_CMAP) pm = generate_preset_pass_manager(optimization_level, backend=target) self.assertIsInstance(pm, PassManager) diff --git a/test/python/transpiler/test_pulse_gate_pass.py b/test/python/transpiler/test_pulse_gate_pass.py index 3880a41fa864..a94a05588d60 100644 --- a/test/python/transpiler/test_pulse_gate_pass.py +++ b/test/python/transpiler/test_pulse_gate_pass.py @@ -19,7 +19,7 @@ from qiskit.quantum_info.random import random_unitary from test import QiskitTestCase # pylint: disable=wrong-import-order -from ..legacy_cmaps import BOGOTA_CMAP +from test.python.legacy_cmaps import BOGOTA_CMAP @ddt.ddt @@ -29,7 +29,6 @@ class TestPulseGate(QiskitTestCase): def setUp(self): super().setUp() - self.basis_gates = ["cx", "id", "rz", "sx", "x"] self.sched_param = circuit.Parameter("P0") with pulse.build(name="sx_q0") as custom_sx_q0: @@ -267,6 +266,7 @@ def test_multiple_instructions_with_different_parameters(self): # Remove timing constraints to avoid triggering # scheduling passes. backend.configuration().timing_constraints = {} + qc = circuit.QuantumCircuit(1) qc.append(circuit.Gate("my_gate", 1, [1.0]), [0]) qc.append(circuit.Gate("my_gate", 1, [2.0]), [0]) @@ -310,6 +310,11 @@ def test_transpile_with_both_instmap_and_empty_target(self, opt_level): Test case from Qiskit/qiskit-terra/#9489 """ + instmap = Fake27QPulseV1().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 = GenericBackendV2( num_qubits=5, @@ -347,12 +352,7 @@ def test_transpile_with_instmap_with_v2backend(self, opt_level): Test case from Qiskit/qiskit-terra/#9489 """ - backend = GenericBackendV2( - num_qubits=5, - calibrate_instructions=Fake27QPulseV1().defaults().instruction_schedule_map, - ) - - instmap = backend.target.instruction_schedule_map() + instmap = Fake27QPulseV1().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) @@ -361,6 +361,11 @@ def test_transpile_with_instmap_with_v2backend(self, opt_level): qc.append(random_unitary(4, seed=123), [0, 1]) qc.measure_all() + backend = GenericBackendV2( + num_qubits=5, + calibrate_instructions=Fake27QPulseV1().defaults().instruction_schedule_map, + ) + transpiled_qc = transpile( qc, backend, @@ -432,7 +437,7 @@ def test_transpile_with_instmap_not_mutate_backend(self): ) original_sx0 = backend.target["sx"][(0,)].calibration - instmap = backend.target.instruction_schedule_map() + instmap = Fake27QPulseV1().defaults().instruction_schedule_map instmap.add("sx", (0,), self.custom_sx_q0) qc = circuit.QuantumCircuit(1) diff --git a/test/python/transpiler/test_sabre_layout.py b/test/python/transpiler/test_sabre_layout.py index 66f2d7d70904..7640149e039e 100644 --- a/test/python/transpiler/test_sabre_layout.py +++ b/test/python/transpiler/test_sabre_layout.py @@ -431,7 +431,7 @@ def test_integration_with_pass_manager(self): qct_initial_layout = qct.layout.initial_layout self.assertEqual( [qct_initial_layout[q] for q in self.circuit.qubits], - [8, 9, 14, 13, 18, 19, 17, 16, 11, 10, 5, 6, 1, 2, 3, 7], + [3, 8, 7, 12, 13, 14, 18, 17, 16, 11, 10, 5, 6, 1, 2, 4], ) diff --git a/test/python/transpiler/test_trivial_layout.py b/test/python/transpiler/test_trivial_layout.py index fcf5749c0e4d..c29a11bdefd8 100644 --- a/test/python/transpiler/test_trivial_layout.py +++ b/test/python/transpiler/test_trivial_layout.py @@ -25,6 +25,7 @@ from ..legacy_cmaps import RUESCHLIKON_CMAP, TENERIFE_CMAP + class TestTrivialLayout(QiskitTestCase): """Tests the TrivialLayout pass""" diff --git a/test/python/transpiler/test_vf2_layout.py b/test/python/transpiler/test_vf2_layout.py index 767892ca5b67..b0957c824688 100644 --- a/test/python/transpiler/test_vf2_layout.py +++ b/test/python/transpiler/test_vf2_layout.py @@ -35,7 +35,7 @@ from qiskit.transpiler.preset_passmanagers.common import generate_embed_passmanager from test import QiskitTestCase # pylint: disable=wrong-import-order -from ..legacy_cmaps import TENERIFE_CMAP, RUESCHLIKON_CMAP +from ..legacy_cmaps import TENERIFE_CMAP, RUESCHLIKON_CMAP, MANHATTAN_CMAP class LayoutTestCase(QiskitTestCase):