diff --git a/qiskit/utils/backend_utils.py b/qiskit/utils/backend_utils.py index 5943c408fc47..cdce30da8dc6 100644 --- a/qiskit/utils/backend_utils.py +++ b/qiskit/utils/backend_utils.py @@ -226,6 +226,9 @@ def is_simulator_backend(backend): backend_interface_version = _get_backend_interface_version(backend) if backend_interface_version <= 1: return backend.configuration().simulator + else: + if "simulator" in backend.name: + return True return False @@ -246,6 +249,9 @@ def is_local_backend(backend): backend_interface_version = _get_backend_interface_version(backend) if backend_interface_version <= 1: return backend.configuration().local + else: + if "simulator" in backend.name: + return True return False diff --git a/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml new file mode 100644 index 000000000000..5abfdc2d00cc --- /dev/null +++ b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fix `backend_utils.py` for simulator backends with BackendV2. + `is_simulator_backend` and `is_local_backend` returns `True` + if backend name that contains `simulator`. diff --git a/test/python/circuit/test_controlled_gate.py b/test/python/circuit/test_controlled_gate.py index e53318a7c442..7f3bcee95f12 100644 --- a/test/python/circuit/test_controlled_gate.py +++ b/test/python/circuit/test_controlled_gate.py @@ -1021,12 +1021,13 @@ def test_standard_base_gate_setting(self, gate_class): """ if gate_class in {SingletonControlledGate, _SingletonControlledGateOverrides}: self.skipTest("SingletonControlledGate isn't directly instantiated.") - num_free_params = len(_get_free_params(gate_class.__init__, ignore=["self"])) + gate_params = _get_free_params(gate_class.__init__, ignore=["self"]) + num_free_params = len(gate_params) free_params = [0.1 * i for i in range(num_free_params)] - if gate_class in [MCU1Gate, MCPhaseGate]: - free_params[1] = 3 - elif gate_class in [MCXGate]: - free_params[0] = 3 + # set number of control qubits + for i in range(num_free_params): + if gate_params[i] == "num_ctrl_qubits": + free_params[i] = 3 base_gate = gate_class(*free_params) cgate = base_gate.control() @@ -1153,12 +1154,13 @@ def test_base_gate_params_reference(self): with self.subTest(i=repr(gate_class)): if gate_class in {SingletonControlledGate, _SingletonControlledGateOverrides}: self.skipTest("Singleton class isn't intended to be created directly.") - num_free_params = len(_get_free_params(gate_class.__init__, ignore=["self"])) - free_params = [0.1 * (i + 1) for i in range(num_free_params)] - if gate_class in [MCU1Gate, MCPhaseGate]: - free_params[1] = 3 - elif gate_class in [MCXGate]: - free_params[0] = 3 + gate_params = _get_free_params(gate_class.__init__, ignore=["self"]) + num_free_params = len(gate_params) + free_params = [0.1 * i for i in range(num_free_params)] + # set number of control qubits + for i in range(num_free_params): + if gate_params[i] == "num_ctrl_qubits": + free_params[i] = 3 base_gate = gate_class(*free_params) if base_gate.params: @@ -1379,12 +1381,13 @@ def test_open_controlled_to_matrix(self, gate_class, ctrl_state): """Test open controlled to_matrix.""" if gate_class in {SingletonControlledGate, _SingletonControlledGateOverrides}: self.skipTest("SingletonGateClass isn't intended for direct initalization") - num_free_params = len(_get_free_params(gate_class.__init__, ignore=["self"])) + gate_params = _get_free_params(gate_class.__init__, ignore=["self"]) + num_free_params = len(gate_params) free_params = [0.1 * i for i in range(1, num_free_params + 1)] - if gate_class in [MCU1Gate, MCPhaseGate]: - free_params[1] = 3 - elif gate_class in [MCXGate]: - free_params[0] = 3 + # set number of control qubits + for i in range(num_free_params): + if gate_params[i] == "num_ctrl_qubits": + free_params[i] = 3 cgate = gate_class(*free_params) cgate.ctrl_state = ctrl_state @@ -1487,7 +1490,8 @@ def test_controlled_standard_gates(self, num_ctrl_qubits, gate_class): ctrl_state_zeros = 0 ctrl_state_mixed = ctrl_state_ones >> int(num_ctrl_qubits / 2) - numargs = len(_get_free_params(gate_class)) + gate_params = _get_free_params(gate_class) + numargs = len(gate_params) args = [theta] * numargs if gate_class in [MSGate, Barrier]: args[0] = 2 @@ -1495,6 +1499,12 @@ def test_controlled_standard_gates(self, num_ctrl_qubits, gate_class): args[1] = 2 elif issubclass(gate_class, MCXGate): args = [5] + else: + # set number of control qubits + for i in range(numargs): + if gate_params[i] == "num_ctrl_qubits": + args[i] = 2 + gate = gate_class(*args) for ctrl_state in (ctrl_state_ones, ctrl_state_zeros, ctrl_state_mixed): diff --git a/test/python/providers/test_fake_backends.py b/test/python/providers/test_fake_backends.py index 84fd607a544e..1b7668a5b9f4 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -416,6 +416,9 @@ def __init__(self, num_ctrl_qubits, ctrl_state=None): from qiskit_aer.noise.noise_model import QuantumErrorLocation sim = AerSimulator() + # test only if simulator's backend is V1 + if sim.version > 1: + return phi = Parameter("phi") lam = Parameter("lam") backend = BackendV2Converter( diff --git a/test/python/pulse/test_block.py b/test/python/pulse/test_block.py index 0dc56fbe6b27..37f94ff7f23f 100644 --- a/test/python/pulse/test_block.py +++ b/test/python/pulse/test_block.py @@ -14,14 +14,12 @@ """Test cases for the pulse schedule block.""" import re -import unittest from typing import List, Any from qiskit import pulse, circuit from qiskit.pulse import transforms from qiskit.pulse.exceptions import PulseError from qiskit.test import QiskitTestCase -from qiskit.providers.fake_provider import FakeOpenPulse2Q, FakeArmonk -from qiskit.utils import has_aer +from qiskit.providers.fake_provider import FakeOpenPulse2Q class BaseTestBlock(QiskitTestCase): @@ -372,20 +370,6 @@ def test_inherit_from(self): self.assertEqual(new_sched.name, ref_name) self.assertDictEqual(new_sched.metadata, ref_metadata) - @unittest.skipUnless(has_aer(), "qiskit-aer doesn't appear to be installed.") - def test_execute_block(self): - """Test executing a ScheduleBlock on a Pulse backend""" - - with pulse.build(name="test_block") as sched_block: - pulse.play(pulse.Constant(160, 1.0), pulse.DriveChannel(0)) - pulse.acquire(50, pulse.AcquireChannel(0), pulse.MemorySlot(0)) - - backend = FakeArmonk() - # TODO: Rewrite test to simulate with qiskit-dynamics - with self.assertWarns(DeprecationWarning): - test_result = backend.run(sched_block).result() - self.assertDictEqual(test_result.get_counts(), {"0": 1024}) - class TestBlockEquality(BaseTestBlock): """Test equality of blocks.