diff --git a/qiskit/providers/fake_provider/__init__.py b/qiskit/providers/fake_provider/__init__.py new file mode 100644 index 000000000000..e67dcf76dd2b --- /dev/null +++ b/qiskit/providers/fake_provider/__init__.py @@ -0,0 +1,39 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2022. +# +# 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 +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +""" +Utilities for mocking the IBMQ provider, including job responses and backends. + +The module includes dummy provider, backends, and jobs. +The purpose of these classes is to fake backends for testing purposes: +testing local timeouts, arbitrary responses or behavior, etc. + +The mock devices are mainly for testing the compiler. +""" + +from qiskit.test.mock.fake_provider import FakeProvider, FakeLegacyProvider +from qiskit.test.mock.fake_provider import FakeProviderFactory +from qiskit.test.mock.fake_backend import FakeBackend, FakeLegacyBackend +from qiskit.test.mock.fake_pulse_backend import FakePulseBackend, FakePulseLegacyBackend +from qiskit.test.mock.fake_qasm_backend import FakeQasmBackend, FakeQasmLegacyBackend +from qiskit.test.mock.utils.configurable_backend import ConfigurableFakeBackend +from qiskit.test.mock.fake_backend_v2 import FakeBackendV2, FakeBackend5QV2 +from qiskit.test.mock.fake_mumbai_v2 import FakeMumbaiV2 +from qiskit.test.mock.fake_job import FakeJob, FakeLegacyJob +from qiskit.test.mock.fake_qobj import FakeQobj + +from qiskit.test.mock.backends import * + +from qiskit.test.mock.fake_qasm_simulator import FakeQasmSimulator +from qiskit.test.mock.fake_openpulse_2q import FakeOpenPulse2Q +from qiskit.test.mock.fake_openpulse_3q import FakeOpenPulse3Q +from qiskit.test.mock.fake_1q import Fake1Q diff --git a/releasenotes/notes/access-backends-from-mock-d3897ecb8490219a.yaml b/releasenotes/notes/access-backends-from-mock-d3897ecb8490219a.yaml new file mode 100644 index 000000000000..be7aee8bf858 --- /dev/null +++ b/releasenotes/notes/access-backends-from-mock-d3897ecb8490219a.yaml @@ -0,0 +1,6 @@ +--- + +upgrade: + - | + Mock backends from `~qiskit.test.mock` are now accessible from `~qiskit.providers.fake_provider`. + The `~qiskit.test.mock` package will be deprecated soon. diff --git a/test/python/mock/test_new_qasm_backend.py b/test/python/mock/test_new_qasm_backend.py new file mode 100644 index 000000000000..0b8425647666 --- /dev/null +++ b/test/python/mock/test_new_qasm_backend.py @@ -0,0 +1,36 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2021. +# +# 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 +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +"""Test of qasm fake backends from qiskit.mock package.""" +import unittest +from qiskit import QuantumCircuit, transpile +from qiskit.test import QiskitTestCase +from qiskit.providers.fake_provider import FakeBogota +from qiskit.utils import optionals as _optionals + + +class FakeQasmBackendsTest(QiskitTestCase): + """Tests for FakeQasmBackend""" + + @unittest.skipUnless(_optionals.HAS_AER, "qiskit-aer is required to run this test") + def test_fake_qasm_backend_configured(self): + """Fake backends honor kwargs passed.""" + backend = FakeBogota() # this is a FakePulseBackend implementation + + qc = QuantumCircuit(2) + qc.x(range(0, 2)) + qc.measure_all() + + trans_qc = transpile(qc, backend) + raw_counts = backend.run(trans_qc, shots=1000).result().get_counts() + + self.assertEqual(sum(raw_counts.values()), 1000) diff --git a/test/python/providers/fake_provider/__init__.py b/test/python/providers/fake_provider/__init__.py new file mode 100644 index 000000000000..2eafea8b53c3 --- /dev/null +++ b/test/python/providers/fake_provider/__init__.py @@ -0,0 +1,13 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2022. +# +# 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 +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +"""Qiskit Fake Provider tests.""" diff --git a/test/python/providers/fake_provider/test_new_pulse_backend.py b/test/python/providers/fake_provider/test_new_pulse_backend.py new file mode 100644 index 000000000000..e9c41334db54 --- /dev/null +++ b/test/python/providers/fake_provider/test_new_pulse_backend.py @@ -0,0 +1,49 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2021. +# +# 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 +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +"""Test of pulse fake backends from qiskit.mock package.""" +import unittest +from qiskit import QuantumCircuit, transpile +from qiskit.test import QiskitTestCase +from qiskit.providers.fake_provider import FakeAthens +from qiskit.utils import optionals as _optionals +from qiskit.providers.fake_provider import FakePulseBackend + + +class FakePulseBackendConfigError(FakePulseBackend): + """Example backend not configured used for testing errors.""" + + props_filename = None + + +class FakePulseBackendsTest(QiskitTestCase): + """Tests for FakePulseBackend""" + + @unittest.skipUnless(_optionals.HAS_AER, "qiskit-aer is required to run this test") + def test_fake_pulse_backend_configured(self): + """Fake backends honor kwargs passed.""" + backend = FakeAthens() # this is a FakePulseBackend implementation + + qc = QuantumCircuit(2) + qc.x(range(0, 2)) + qc.measure_all() + + trans_qc = transpile(qc, backend) + raw_counts = backend.run(trans_qc, shots=1000).result().get_counts() + + self.assertEqual(sum(raw_counts.values()), 1000) + + @unittest.expectedFailure + def test_fake_pulse_backend_configuration_error(self): + """Test configuration error for pulse backend.""" + backend = FakePulseBackendConfigError() + backend.defaults()