From f5a2ea96e3757410473c3473d8391276eafc8461 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 Nov 2023 11:08:42 +0900 Subject: [PATCH 1/7] fix backend_util.py to support Aer 0.13 --- qiskit/utils/backend_utils.py | 6 ++++++ releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml 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..5c625c54cdf7 --- /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` From c7835f2432702f1517f22fba7b444f7010d5e4fc Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 Nov 2023 15:59:30 +0900 Subject: [PATCH 2/7] Fix test cases for Aer 0.13.0 --- test/python/providers/test_fake_backends.py | 4 ++++ test/python/pulse/test_block.py | 15 --------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/test/python/providers/test_fake_backends.py b/test/python/providers/test_fake_backends.py index 84fd607a544e..5de94128fe08 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -41,6 +41,7 @@ from qiskit.providers.models.backendproperties import BackendProperties from qiskit.providers.backend import BackendV2 from qiskit.utils import optionals +from qiskit.utils.backend_utils import _get_backend_interface_version from qiskit.circuit.library import ( SXGate, MCPhaseGate, @@ -416,6 +417,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 _get_backend_interface_version(sim) > 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..4a31fe5b594b 100644 --- a/test/python/pulse/test_block.py +++ b/test/python/pulse/test_block.py @@ -372,21 +372,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. From 43ec21fd93048a4c0f2dbef2fa75addd1c7db1cf Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 Nov 2023 17:25:36 +0900 Subject: [PATCH 3/7] format --- releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml | 8 +++++++- test/python/pulse/test_block.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml index 5c625c54cdf7..582f2ab71217 100644 --- a/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml +++ b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml @@ -3,4 +3,10 @@ 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` + if backend name that contains `simulator`. + + In `test/python/providers/test_fake_backends.py`, ignore running test + when backend is V2. + + In `test/python/pulse/test_block.py` test case is removed because + Aer does not have pulse simulator anymore. diff --git a/test/python/pulse/test_block.py b/test/python/pulse/test_block.py index 4a31fe5b594b..39904db3d1b5 100644 --- a/test/python/pulse/test_block.py +++ b/test/python/pulse/test_block.py @@ -372,6 +372,7 @@ def test_inherit_from(self): self.assertEqual(new_sched.name, ref_name) self.assertDictEqual(new_sched.metadata, ref_metadata) + class TestBlockEquality(BaseTestBlock): """Test equality of blocks. From 355f3ce2a926538406d8541aad7e4ddf4ca193a8 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 Nov 2023 18:21:04 +0900 Subject: [PATCH 4/7] remove unused imports --- test/python/pulse/test_block.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/python/pulse/test_block.py b/test/python/pulse/test_block.py index 39904db3d1b5..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): From 539ebd610dae57a32c1a38ef2ff5bb286d4872e9 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 8 Nov 2023 10:31:06 +0900 Subject: [PATCH 5/7] Update test/python/providers/test_fake_backends.py Co-authored-by: Matthew Treinish --- test/python/providers/test_fake_backends.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/providers/test_fake_backends.py b/test/python/providers/test_fake_backends.py index 5de94128fe08..0dd2f5d4231c 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -418,7 +418,7 @@ def __init__(self, num_ctrl_qubits, ctrl_state=None): sim = AerSimulator() # test only if simulator's backend is V1 - if _get_backend_interface_version(sim) > 1: + if sim.version > 1:: return phi = Parameter("phi") lam = Parameter("lam") From 43d7e59e490d4425b42c972e9cd7ca6ee6de59f8 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 8 Nov 2023 15:38:10 +0900 Subject: [PATCH 6/7] fix setting num_control_qubits param in test.python.circuit.test_controlled_gate --- .../fix_sim_backend-f3971b1ed4d0c4e6.yaml | 6 --- test/python/circuit/test_controlled_gate.py | 44 ++++++++++++------- test/python/providers/test_fake_backends.py | 2 +- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml index 582f2ab71217..5abfdc2d00cc 100644 --- a/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml +++ b/releasenotes/notes/fix_sim_backend-f3971b1ed4d0c4e6.yaml @@ -4,9 +4,3 @@ 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`. - - In `test/python/providers/test_fake_backends.py`, ignore running test - when backend is V2. - - In `test/python/pulse/test_block.py` test case is removed because - Aer does not have pulse simulator anymore. 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 0dd2f5d4231c..2ad7f7e01742 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -418,7 +418,7 @@ def __init__(self, num_ctrl_qubits, ctrl_state=None): sim = AerSimulator() # test only if simulator's backend is V1 - if sim.version > 1:: + if sim.version > 1: return phi = Parameter("phi") lam = Parameter("lam") From f08a9f946e8e1369594d7b14cf39cd57f92f8f76 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 8 Nov 2023 17:52:11 +0900 Subject: [PATCH 7/7] remove unused import --- test/python/providers/test_fake_backends.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/python/providers/test_fake_backends.py b/test/python/providers/test_fake_backends.py index 2ad7f7e01742..1b7668a5b9f4 100644 --- a/test/python/providers/test_fake_backends.py +++ b/test/python/providers/test_fake_backends.py @@ -41,7 +41,6 @@ from qiskit.providers.models.backendproperties import BackendProperties from qiskit.providers.backend import BackendV2 from qiskit.utils import optionals -from qiskit.utils.backend_utils import _get_backend_interface_version from qiskit.circuit.library import ( SXGate, MCPhaseGate,