From e322f4246b2dc0564e2a4f2f1b9f266790878331 Mon Sep 17 00:00:00 2001 From: Will Shanks Date: Fri, 24 Mar 2023 00:21:20 -0400 Subject: [PATCH 1/3] Update FakeBackend to BackendV2 --- qiskit_experiments/test/fake_backend.py | 40 +++++++++++-------------- test/framework/test_composite.py | 2 +- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/qiskit_experiments/test/fake_backend.py b/qiskit_experiments/test/fake_backend.py index 93823ad663..e788be2af5 100644 --- a/qiskit_experiments/test/fake_backend.py +++ b/qiskit_experiments/test/fake_backend.py @@ -12,38 +12,34 @@ """Fake backend class for tests.""" import uuid -from qiskit.providers.backend import BackendV1 -from qiskit.providers.models import QasmBackendConfiguration +from qiskit.circuit.library import CXGate, Measure +from qiskit.providers.backend import BackendV2 +from qiskit.providers.options import Options +from qiskit.transpiler import Target from qiskit.result import Result -from qiskit_experiments.framework import Options from qiskit_experiments.test.utils import FakeJob -class FakeBackend(BackendV1): +class FakeBackend(BackendV2): """ Fake backend for test purposes only. """ - def __init__(self, **config): - default_config = { - "backend_name": "fake_backend", - "backend_version": "0", - "n_qubits": int(1e6), - "basis_gates": [], - "gates": [], - "local": True, - "simulator": True, - "conditional": False, - "open_pulse": False, - "memory": False, - "max_shots": int(1e6), - "max_experiments": None, - "coupling_map": None, - } - default_config.update(**config) - super().__init__(QasmBackendConfiguration(**default_config)) + target = None + + def __init__(self, backend_name="fake_backend", num_qubits=1, max_experiments=100): + super().__init__(name=backend_name) + self.target = Target(num_qubits=num_qubits) + # Add a measure for each qubit so a simple measure circuit works + self.target.add_instruction(Measure()) + self._max_circuits = max_experiments + + @property + def max_circuits(self): + """Maximum circuits to run at once""" + return self._max_circuits @classmethod def _default_options(cls): diff --git a/test/framework/test_composite.py b/test/framework/test_composite.py index fa4cb07804..ee8d0b5c75 100644 --- a/test/framework/test_composite.py +++ b/test/framework/test_composite.py @@ -493,7 +493,7 @@ def circuits(self): par_exp = ParallelExperiment( [exp1, BatchExperiment([ParallelExperiment([exp2, exp3]), exp4])] ) - expdata = par_exp.run(Backend()) + expdata = par_exp.run(Backend(num_qubits=4)) self.assertExperimentDone(expdata) self.assertEqual(len(expdata.data()), len(counts)) From 488bc7712564ac3ecb50f20888d8567b7d3f20e8 Mon Sep 17 00:00:00 2001 From: Will Shanks Date: Fri, 24 Mar 2023 13:38:28 -0400 Subject: [PATCH 2/3] Remvoe unused import --- qiskit_experiments/test/fake_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_experiments/test/fake_backend.py b/qiskit_experiments/test/fake_backend.py index e788be2af5..b290c18aa1 100644 --- a/qiskit_experiments/test/fake_backend.py +++ b/qiskit_experiments/test/fake_backend.py @@ -12,7 +12,7 @@ """Fake backend class for tests.""" import uuid -from qiskit.circuit.library import CXGate, Measure +from qiskit.circuit.library import Measure from qiskit.providers.backend import BackendV2 from qiskit.providers.options import Options from qiskit.transpiler import Target From a12b8b2e3c2d83d679c183898482e71a6edb678e Mon Sep 17 00:00:00 2001 From: Will Shanks Date: Sun, 26 Mar 2023 14:12:47 -0400 Subject: [PATCH 3/3] Replace target class variables with properties --- qiskit_experiments/test/fake_backend.py | 8 +++++--- test/calibration/test_calibrations.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/qiskit_experiments/test/fake_backend.py b/qiskit_experiments/test/fake_backend.py index b290c18aa1..07bab78a78 100644 --- a/qiskit_experiments/test/fake_backend.py +++ b/qiskit_experiments/test/fake_backend.py @@ -27,11 +27,9 @@ class FakeBackend(BackendV2): Fake backend for test purposes only. """ - target = None - def __init__(self, backend_name="fake_backend", num_qubits=1, max_experiments=100): super().__init__(name=backend_name) - self.target = Target(num_qubits=num_qubits) + self._target = Target(num_qubits=num_qubits) # Add a measure for each qubit so a simple measure circuit works self.target.add_instruction(Measure()) self._max_circuits = max_experiments @@ -45,6 +43,10 @@ def max_circuits(self): def _default_options(cls): return Options() + @property + def target(self) -> Target: + return self._target + def run(self, run_input, **options): result = { "backend_name": "fake_backend", diff --git a/test/calibration/test_calibrations.py b/test/calibration/test_calibrations.py index 7634dab578..e56f2b46a2 100644 --- a/test/calibration/test_calibrations.py +++ b/test/calibration/test_calibrations.py @@ -51,11 +51,9 @@ class MinimalBackend(BackendV2): """Class for testing a backend with minimal data""" - target = None - - def __init__(self): + def __init__(self, num_qubits=1): super().__init__() - self.target = Target() + self._target = Target(num_qubits=num_qubits) @property def max_circuits(self): @@ -66,6 +64,11 @@ def max_circuits(self): def _default_options(cls): return Options() + @property + def target(self) -> Target: + """Target instance for the backend""" + return self._target + def run(self, run_input, **options): """Empty method to satisfy abstract base class""" pass @@ -342,8 +345,7 @@ def test_from_minimal_backend(self, num_qubits, gate_name, pass_properties): else: gate = None - backend = MinimalBackend() - backend.target = Target(num_qubits=num_qubits) + backend = MinimalBackend(num_qubits=num_qubits) if gate is not None: backend.target.add_instruction(gate, properties=properties) Calibrations.from_backend(backend)