Skip to content

Commit

Permalink
Update remaining unit tests to use FakeGeneric (#10918)
Browse files Browse the repository at this point in the history
* Update V2 tests to use FakeGeneric or GenericTarget

* Add missing tests and fixes

Fix backend estimator test

Fix lint, backend estimator test

Include missing tests

* Fix calibrations and adjust tests

Initial fix

Fix calibrations

Fix remaining tests

* Fix remaining tests

* Add test_backend_v2 test

* Add Aer check to sparse_pauli_op test

* Fix noise defaults

* Fix value

* Fix lint

* Delete spurious files

* Update class names, fix conflicts post-merge.

* Extract coupling maps, improve style

* Fix passmanager test

* More test fixes

* New attempt at fixing tests

* Please, mr. backend_estimator test, give me a break and reproduce my local results.

* Trying again

* Apply suggestions from Kevin's code review

Co-authored-by: Kevin Hartman <[email protected]>

* Move cmaps to new file, fix black

* Fix tests

* Replace FakeBackendSimple with BasicSimulator

* Fix passmanager config test

* Fix seed for vf2 layout tests

---------

Co-authored-by: Kevin Hartman <[email protected]>
  • Loading branch information
ElePT and kevinhartman authored Jan 31, 2024
1 parent 0364786 commit d9b90de
Show file tree
Hide file tree
Showing 25 changed files with 719 additions and 380 deletions.
10 changes: 5 additions & 5 deletions qiskit/providers/fake_provider/generic_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ def _get_calibration_sequence(
name="acquire",
duration=1792,
t0=0,
qubits=list(range(num_qubits)),
memory_slot=list(range(num_qubits)),
qubits=qargs,
memory_slot=qargs,
)
] + [PulseQobjInstruction(name=pulse_library[1], ch=f"m{i}", t0=0) for i in qargs]
] + [PulseQobjInstruction(name=pulse_library[1].name, ch=f"m{i}", t0=0) for i in qargs]
return sequence
if num_qubits == 1:
return [
PulseQobjInstruction(name="fc", ch=f"u{qargs}", t0=0, phase="-P0"),
PulseQobjInstruction(name=pulse_library[0].name, ch=f"d{qargs}", t0=0),
PulseQobjInstruction(name="fc", ch=f"u{qargs[0]}", t0=0, phase="-P0"),
PulseQobjInstruction(name=pulse_library[0].name, ch=f"d{qargs[0]}", t0=0),
]
return [
PulseQobjInstruction(name=pulse_library[1].name, ch=f"d{qargs[0]}", t0=0),
Expand Down
194 changes: 194 additions & 0 deletions test/python/legacy_cmaps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 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
# 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.

"""Coupling maps for fake backend generation and transpiler testing."""

# 5 qubits
# bidirectional
BOGOTA_CMAP = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2], [3, 4], [4, 3]]
LIMA_CMAP = [[0, 1], [1, 0], [1, 2], [1, 3], [2, 1], [3, 1], [3, 4], [4, 3]]
YORKTOWN_CMAP = [
[0, 1],
[0, 2],
[1, 0],
[1, 2],
[2, 0],
[2, 1],
[2, 3],
[2, 4],
[3, 2],
[3, 4],
[4, 2],
[4, 3],
]
# directional
TENERIFE_CMAP = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]]

# 16 qubits
RUESCHLIKON_CMAP = [
[1, 0],
[1, 2],
[2, 3],
[3, 4],
[3, 14],
[5, 4],
[6, 5],
[6, 7],
[6, 11],
[7, 10],
[8, 7],
[9, 8],
[9, 10],
[11, 10],
[12, 5],
[12, 11],
[12, 13],
[13, 4],
[13, 14],
[15, 0],
[15, 2],
[15, 14],
]

# 20 qubits
TOKYO_CMAP = [
[0, 1],
[0, 5],
[1, 0],
[1, 2],
[1, 6],
[1, 7],
[2, 1],
[2, 6],
[3, 8],
[4, 8],
[4, 9],
[5, 0],
[5, 6],
[5, 10],
[5, 11],
[6, 1],
[6, 2],
[6, 5],
[6, 7],
[6, 10],
[6, 11],
[7, 1],
[7, 6],
[7, 8],
[7, 12],
[8, 3],
[8, 4],
[8, 7],
[8, 9],
[8, 12],
[8, 13],
[9, 4],
[9, 8],
[10, 5],
[10, 6],
[10, 11],
[10, 15],
[11, 5],
[11, 6],
[11, 10],
[11, 12],
[11, 16],
[11, 17],
[12, 7],
[12, 8],
[12, 11],
[12, 13],
[12, 16],
[13, 8],
[13, 12],
[13, 14],
[13, 18],
[13, 19],
[14, 13],
[14, 18],
[14, 19],
[15, 10],
[15, 16],
[16, 11],
[16, 12],
[16, 15],
[16, 17],
[17, 11],
[17, 16],
[17, 18],
[18, 13],
[18, 14],
[18, 17],
[19, 13],
[19, 14],
]

# 27 qubits
MUMBAI_CMAP = [
[0, 1],
[1, 0],
[1, 2],
[1, 4],
[2, 1],
[2, 3],
[3, 2],
[3, 5],
[4, 1],
[4, 7],
[5, 3],
[5, 8],
[6, 7],
[7, 4],
[7, 6],
[7, 10],
[8, 5],
[8, 9],
[8, 11],
[9, 8],
[10, 7],
[10, 12],
[11, 8],
[11, 14],
[12, 10],
[12, 13],
[12, 15],
[13, 12],
[13, 14],
[14, 11],
[14, 13],
[14, 16],
[15, 12],
[15, 18],
[16, 14],
[16, 19],
[17, 18],
[18, 15],
[18, 17],
[18, 21],
[19, 16],
[19, 20],
[19, 22],
[20, 19],
[21, 18],
[21, 23],
[22, 19],
[22, 25],
[23, 21],
[23, 24],
[24, 23],
[24, 25],
[25, 22],
[25, 24],
[25, 26],
[26, 25],
]
34 changes: 20 additions & 14 deletions test/python/primitives/test_backend_estimator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022.
# (C) Copyright IBM 2022, 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
Expand All @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Tests for Estimator."""
"""Tests for BackendEstimator."""

import unittest
from unittest.mock import patch
Expand All @@ -21,8 +21,8 @@
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import BackendEstimator, EstimatorResult
from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2
from qiskit.providers.fake_provider.fake_backend_v2 import FakeBackendSimple
from qiskit.providers.fake_provider import FakeNairobi, GenericBackendV2
from qiskit.providers.backend_compat import BackendV2Converter
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler import PassManager
from qiskit.utils import optionals
Expand All @@ -31,7 +31,11 @@
from test.python.transpiler._dummy_passes import DummyAP # pylint: disable=wrong-import-order


BACKENDS = [FakeNairobi(), FakeNairobiV2(), FakeBackendSimple()]
BACKENDS = [
FakeNairobi(),
BackendV2Converter(FakeNairobi()),
GenericBackendV2(num_qubits=5, seed=42),
]


class CallbackPass(DummyAP):
Expand Down Expand Up @@ -271,14 +275,14 @@ def test_options(self, backend):
def test_job_size_limit_v2(self):
"""Test BackendEstimator respects job size limit"""

class FakeNairobiLimitedCircuits(FakeNairobiV2):
"""FakeNairobiV2 with job size limit."""
class FakeBackendLimitedCircuits(GenericBackendV2):
"""Generic backend V2 with job size limit."""

@property
def max_circuits(self):
return 1

backend = FakeNairobiLimitedCircuits()
backend = FakeBackendLimitedCircuits(num_qubits=5)
backend.set_options(seed_simulator=123)
qc = RealAmplitudes(num_qubits=2, reps=2)
op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)])
Expand Down Expand Up @@ -387,10 +391,11 @@ def test_layout(self, backend):
estimator = BackendEstimator(backend)
estimator.set_transpile_options(seed_transpiler=15)
value = estimator.run(qc, op, shots=10000).result().values[0]
if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
self.assertEqual(value, -0.916)
if optionals.HAS_AER:
ref_value = -0.9922 if isinstance(backend, GenericBackendV2) else -0.916
else:
self.assertEqual(value, -1)
ref_value = -1
self.assertEqual(value, ref_value)

with self.subTest("final layout test"):
qc = QuantumCircuit(3)
Expand All @@ -402,10 +407,11 @@ def test_layout(self, backend):
estimator = BackendEstimator(backend)
estimator.set_transpile_options(initial_layout=[0, 1, 2], seed_transpiler=15)
value = estimator.run(qc, op, shots=10000).result().values[0]
if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
self.assertEqual(value, -0.8902)
if optionals.HAS_AER:
ref_value = -0.9922 if isinstance(backend, GenericBackendV2) else -0.8902
else:
self.assertEqual(value, -1)
ref_value = -1
self.assertEqual(value, ref_value)

@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
def test_circuit_with_measurement(self):
Expand Down
14 changes: 9 additions & 5 deletions test/python/primitives/test_backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import BackendSampler, SamplerResult
from qiskit.providers import JobStatus
from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2
from qiskit.providers.fake_provider import FakeNairobi, GenericBackendV2
from qiskit.providers.backend_compat import BackendV2Converter
from qiskit.providers.basic_provider import BasicSimulator
from qiskit.transpiler import PassManager
from qiskit.utils import optionals
Expand All @@ -31,7 +32,10 @@
from test.python.transpiler._dummy_passes import DummyAP # pylint: disable=wrong-import-order


BACKENDS = [FakeNairobi(), FakeNairobiV2()]
BACKENDS = [
FakeNairobi(),
BackendV2Converter(FakeNairobi()),
]


class CallbackPass(DummyAP):
Expand Down Expand Up @@ -298,8 +302,8 @@ def test_primitive_job_status_done(self, backend):
def test_primitive_job_size_limit_backend_v2(self):
"""Test primitive respects backend's job size limit."""

class FakeNairobiLimitedCircuits(FakeNairobiV2):
"""FakeNairobiV2 with job size limit."""
class FakeBackendLimitedCircuits(GenericBackendV2):
"""Generic backend V2 with job size limit."""

@property
def max_circuits(self):
Expand All @@ -310,7 +314,7 @@ def max_circuits(self):
qc2 = QuantumCircuit(1)
qc2.x(0)
qc2.measure_all()
sampler = BackendSampler(backend=FakeNairobiLimitedCircuits())
sampler = BackendSampler(backend=FakeBackendLimitedCircuits(num_qubits=5))
result = sampler.run([qc, qc2]).result()
self.assertIsInstance(result, SamplerResult)
self.assertEqual(len(result.quasi_dists), 2)
Expand Down
Loading

0 comments on commit d9b90de

Please sign in to comment.