Skip to content

Commit d9b90de

Browse files
ElePTkevinhartman
andauthored
Update remaining unit tests to use FakeGeneric (#10918)
* 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 <kevin@hart.mn> * 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 <kevin@hart.mn>
1 parent 0364786 commit d9b90de

25 files changed

+719
-380
lines changed

qiskit/providers/fake_provider/generic_backend_v2.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ def _get_calibration_sequence(
251251
name="acquire",
252252
duration=1792,
253253
t0=0,
254-
qubits=list(range(num_qubits)),
255-
memory_slot=list(range(num_qubits)),
254+
qubits=qargs,
255+
memory_slot=qargs,
256256
)
257-
] + [PulseQobjInstruction(name=pulse_library[1], ch=f"m{i}", t0=0) for i in qargs]
257+
] + [PulseQobjInstruction(name=pulse_library[1].name, ch=f"m{i}", t0=0) for i in qargs]
258258
return sequence
259259
if num_qubits == 1:
260260
return [
261-
PulseQobjInstruction(name="fc", ch=f"u{qargs}", t0=0, phase="-P0"),
262-
PulseQobjInstruction(name=pulse_library[0].name, ch=f"d{qargs}", t0=0),
261+
PulseQobjInstruction(name="fc", ch=f"u{qargs[0]}", t0=0, phase="-P0"),
262+
PulseQobjInstruction(name=pulse_library[0].name, ch=f"d{qargs[0]}", t0=0),
263263
]
264264
return [
265265
PulseQobjInstruction(name=pulse_library[1].name, ch=f"d{qargs[0]}", t0=0),

test/python/legacy_cmaps.py

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# This code is part of Qiskit.
2+
#
3+
# (C) Copyright IBM 2024.
4+
#
5+
# This code is licensed under the Apache License, Version 2.0. You may
6+
# obtain a copy of this license in the LICENSE.txt file in the root directory
7+
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8+
#
9+
# Any modifications or derivative works of this code must retain this
10+
# copyright notice, and modified files need to carry a notice indicating
11+
# that they have been altered from the originals.
12+
13+
"""Coupling maps for fake backend generation and transpiler testing."""
14+
15+
# 5 qubits
16+
# bidirectional
17+
BOGOTA_CMAP = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2], [3, 4], [4, 3]]
18+
LIMA_CMAP = [[0, 1], [1, 0], [1, 2], [1, 3], [2, 1], [3, 1], [3, 4], [4, 3]]
19+
YORKTOWN_CMAP = [
20+
[0, 1],
21+
[0, 2],
22+
[1, 0],
23+
[1, 2],
24+
[2, 0],
25+
[2, 1],
26+
[2, 3],
27+
[2, 4],
28+
[3, 2],
29+
[3, 4],
30+
[4, 2],
31+
[4, 3],
32+
]
33+
# directional
34+
TENERIFE_CMAP = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]]
35+
36+
# 16 qubits
37+
RUESCHLIKON_CMAP = [
38+
[1, 0],
39+
[1, 2],
40+
[2, 3],
41+
[3, 4],
42+
[3, 14],
43+
[5, 4],
44+
[6, 5],
45+
[6, 7],
46+
[6, 11],
47+
[7, 10],
48+
[8, 7],
49+
[9, 8],
50+
[9, 10],
51+
[11, 10],
52+
[12, 5],
53+
[12, 11],
54+
[12, 13],
55+
[13, 4],
56+
[13, 14],
57+
[15, 0],
58+
[15, 2],
59+
[15, 14],
60+
]
61+
62+
# 20 qubits
63+
TOKYO_CMAP = [
64+
[0, 1],
65+
[0, 5],
66+
[1, 0],
67+
[1, 2],
68+
[1, 6],
69+
[1, 7],
70+
[2, 1],
71+
[2, 6],
72+
[3, 8],
73+
[4, 8],
74+
[4, 9],
75+
[5, 0],
76+
[5, 6],
77+
[5, 10],
78+
[5, 11],
79+
[6, 1],
80+
[6, 2],
81+
[6, 5],
82+
[6, 7],
83+
[6, 10],
84+
[6, 11],
85+
[7, 1],
86+
[7, 6],
87+
[7, 8],
88+
[7, 12],
89+
[8, 3],
90+
[8, 4],
91+
[8, 7],
92+
[8, 9],
93+
[8, 12],
94+
[8, 13],
95+
[9, 4],
96+
[9, 8],
97+
[10, 5],
98+
[10, 6],
99+
[10, 11],
100+
[10, 15],
101+
[11, 5],
102+
[11, 6],
103+
[11, 10],
104+
[11, 12],
105+
[11, 16],
106+
[11, 17],
107+
[12, 7],
108+
[12, 8],
109+
[12, 11],
110+
[12, 13],
111+
[12, 16],
112+
[13, 8],
113+
[13, 12],
114+
[13, 14],
115+
[13, 18],
116+
[13, 19],
117+
[14, 13],
118+
[14, 18],
119+
[14, 19],
120+
[15, 10],
121+
[15, 16],
122+
[16, 11],
123+
[16, 12],
124+
[16, 15],
125+
[16, 17],
126+
[17, 11],
127+
[17, 16],
128+
[17, 18],
129+
[18, 13],
130+
[18, 14],
131+
[18, 17],
132+
[19, 13],
133+
[19, 14],
134+
]
135+
136+
# 27 qubits
137+
MUMBAI_CMAP = [
138+
[0, 1],
139+
[1, 0],
140+
[1, 2],
141+
[1, 4],
142+
[2, 1],
143+
[2, 3],
144+
[3, 2],
145+
[3, 5],
146+
[4, 1],
147+
[4, 7],
148+
[5, 3],
149+
[5, 8],
150+
[6, 7],
151+
[7, 4],
152+
[7, 6],
153+
[7, 10],
154+
[8, 5],
155+
[8, 9],
156+
[8, 11],
157+
[9, 8],
158+
[10, 7],
159+
[10, 12],
160+
[11, 8],
161+
[11, 14],
162+
[12, 10],
163+
[12, 13],
164+
[12, 15],
165+
[13, 12],
166+
[13, 14],
167+
[14, 11],
168+
[14, 13],
169+
[14, 16],
170+
[15, 12],
171+
[15, 18],
172+
[16, 14],
173+
[16, 19],
174+
[17, 18],
175+
[18, 15],
176+
[18, 17],
177+
[18, 21],
178+
[19, 16],
179+
[19, 20],
180+
[19, 22],
181+
[20, 19],
182+
[21, 18],
183+
[21, 23],
184+
[22, 19],
185+
[22, 25],
186+
[23, 21],
187+
[23, 24],
188+
[24, 23],
189+
[24, 25],
190+
[25, 22],
191+
[25, 24],
192+
[25, 26],
193+
[26, 25],
194+
]

test/python/primitives/test_backend_estimator.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of Qiskit.
22
#
3-
# (C) Copyright IBM 2022.
3+
# (C) Copyright IBM 2022, 2024.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -10,7 +10,7 @@
1010
# copyright notice, and modified files need to carry a notice indicating
1111
# that they have been altered from the originals.
1212

13-
"""Tests for Estimator."""
13+
"""Tests for BackendEstimator."""
1414

1515
import unittest
1616
from unittest.mock import patch
@@ -21,8 +21,8 @@
2121
from qiskit.circuit import QuantumCircuit
2222
from qiskit.circuit.library import RealAmplitudes
2323
from qiskit.primitives import BackendEstimator, EstimatorResult
24-
from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2
25-
from qiskit.providers.fake_provider.fake_backend_v2 import FakeBackendSimple
24+
from qiskit.providers.fake_provider import FakeNairobi, GenericBackendV2
25+
from qiskit.providers.backend_compat import BackendV2Converter
2626
from qiskit.quantum_info import SparsePauliOp
2727
from qiskit.transpiler import PassManager
2828
from qiskit.utils import optionals
@@ -31,7 +31,11 @@
3131
from test.python.transpiler._dummy_passes import DummyAP # pylint: disable=wrong-import-order
3232

3333

34-
BACKENDS = [FakeNairobi(), FakeNairobiV2(), FakeBackendSimple()]
34+
BACKENDS = [
35+
FakeNairobi(),
36+
BackendV2Converter(FakeNairobi()),
37+
GenericBackendV2(num_qubits=5, seed=42),
38+
]
3539

3640

3741
class CallbackPass(DummyAP):
@@ -271,14 +275,14 @@ def test_options(self, backend):
271275
def test_job_size_limit_v2(self):
272276
"""Test BackendEstimator respects job size limit"""
273277

274-
class FakeNairobiLimitedCircuits(FakeNairobiV2):
275-
"""FakeNairobiV2 with job size limit."""
278+
class FakeBackendLimitedCircuits(GenericBackendV2):
279+
"""Generic backend V2 with job size limit."""
276280

277281
@property
278282
def max_circuits(self):
279283
return 1
280284

281-
backend = FakeNairobiLimitedCircuits()
285+
backend = FakeBackendLimitedCircuits(num_qubits=5)
282286
backend.set_options(seed_simulator=123)
283287
qc = RealAmplitudes(num_qubits=2, reps=2)
284288
op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)])
@@ -387,10 +391,11 @@ def test_layout(self, backend):
387391
estimator = BackendEstimator(backend)
388392
estimator.set_transpile_options(seed_transpiler=15)
389393
value = estimator.run(qc, op, shots=10000).result().values[0]
390-
if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
391-
self.assertEqual(value, -0.916)
394+
if optionals.HAS_AER:
395+
ref_value = -0.9922 if isinstance(backend, GenericBackendV2) else -0.916
392396
else:
393-
self.assertEqual(value, -1)
397+
ref_value = -1
398+
self.assertEqual(value, ref_value)
394399

395400
with self.subTest("final layout test"):
396401
qc = QuantumCircuit(3)
@@ -402,10 +407,11 @@ def test_layout(self, backend):
402407
estimator = BackendEstimator(backend)
403408
estimator.set_transpile_options(initial_layout=[0, 1, 2], seed_transpiler=15)
404409
value = estimator.run(qc, op, shots=10000).result().values[0]
405-
if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
406-
self.assertEqual(value, -0.8902)
410+
if optionals.HAS_AER:
411+
ref_value = -0.9922 if isinstance(backend, GenericBackendV2) else -0.8902
407412
else:
408-
self.assertEqual(value, -1)
413+
ref_value = -1
414+
self.assertEqual(value, ref_value)
409415

410416
@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
411417
def test_circuit_with_measurement(self):

test/python/primitives/test_backend_sampler.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from qiskit.circuit.library import RealAmplitudes
2323
from qiskit.primitives import BackendSampler, SamplerResult
2424
from qiskit.providers import JobStatus
25-
from qiskit.providers.fake_provider import FakeNairobi, FakeNairobiV2
25+
from qiskit.providers.fake_provider import FakeNairobi, GenericBackendV2
26+
from qiskit.providers.backend_compat import BackendV2Converter
2627
from qiskit.providers.basic_provider import BasicSimulator
2728
from qiskit.transpiler import PassManager
2829
from qiskit.utils import optionals
@@ -31,7 +32,10 @@
3132
from test.python.transpiler._dummy_passes import DummyAP # pylint: disable=wrong-import-order
3233

3334

34-
BACKENDS = [FakeNairobi(), FakeNairobiV2()]
35+
BACKENDS = [
36+
FakeNairobi(),
37+
BackendV2Converter(FakeNairobi()),
38+
]
3539

3640

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

301-
class FakeNairobiLimitedCircuits(FakeNairobiV2):
302-
"""FakeNairobiV2 with job size limit."""
305+
class FakeBackendLimitedCircuits(GenericBackendV2):
306+
"""Generic backend V2 with job size limit."""
303307

304308
@property
305309
def max_circuits(self):
@@ -310,7 +314,7 @@ def max_circuits(self):
310314
qc2 = QuantumCircuit(1)
311315
qc2.x(0)
312316
qc2.measure_all()
313-
sampler = BackendSampler(backend=FakeNairobiLimitedCircuits())
317+
sampler = BackendSampler(backend=FakeBackendLimitedCircuits(num_qubits=5))
314318
result = sampler.run([qc, qc2]).result()
315319
self.assertIsInstance(result, SamplerResult)
316320
self.assertEqual(len(result.quasi_dists), 2)

0 commit comments

Comments
 (0)