Skip to content

Commit 040c9c8

Browse files
authored
Fix checking for deprecation warnings to use assertWarns() (#14145)
Fix the deprecation warning check to use the correct `assertWarns()` method rather than `assertRaises()` that is used to test for exceptions. This fixes the test suite when it is run without `-Werror`.
1 parent d0aa100 commit 040c9c8

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

test/python/circuit/test_scheduled_circuit.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_schedule_circuit_when_backend_tells_dt(self):
5151
backend = GenericBackendV2(2, seed=42)
5252

5353
sc = transpile(qc, backend, scheduling_method="alap", layout_method="trivial")
54-
with self.assertRaises(DeprecationWarning):
54+
with self.assertWarns(DeprecationWarning):
5555
self.assertEqual(sc.duration, 451095)
5656
self.assertEqual(sc.unit, "dt")
5757
self.assertEqual(sc.data[0].operation.name, "delay")
@@ -78,7 +78,7 @@ def test_schedule_circuit_when_transpile_option_tells_dt(self):
7878
seed_transpiler=20,
7979
)
8080
target_durations = self.backend_with_dt.target.durations()
81-
with self.assertRaises(DeprecationWarning):
81+
with self.assertWarns(DeprecationWarning):
8282
self.assertAlmostEqual(sc.duration, (450450 + target_durations.get("sx", 0)))
8383
self.assertEqual(sc.unit, "dt")
8484
self.assertEqual(sc.data[0].operation.name, "delay")
@@ -104,7 +104,7 @@ def test_schedule_circuit_in_sec_when_no_one_tells_dt(self):
104104
)
105105
target_durations = self.backend_with_dt.target.durations()
106106

107-
with self.assertRaises(DeprecationWarning):
107+
with self.assertWarns(DeprecationWarning):
108108
self.assertAlmostEqual(sc.duration, (450450 + target_durations.get("sx", 0)) * self.dt)
109109
self.assertEqual(sc.unit, "s")
110110
self.assertEqual(sc.data[0].operation.name, "delay")
@@ -129,7 +129,7 @@ def test_transpile_single_delay_circuit(self):
129129
qc = QuantumCircuit(1)
130130
qc.delay(1234, 0)
131131
sc = transpile(qc, backend=self.backend_with_dt, scheduling_method="alap")
132-
with self.assertRaises(DeprecationWarning):
132+
with self.assertWarns(DeprecationWarning):
133133
self.assertEqual(sc.duration, 1234)
134134
self.assertEqual(sc.data[0].operation.name, "delay")
135135
self.assertEqual(sc.data[0].operation.duration, 1234)
@@ -146,7 +146,7 @@ def test_transpile_t1_circuit(self):
146146
expected_scheduled = (
147147
target_durations.get("x", 1) + 4500 + target_durations.get("measure", 1)
148148
)
149-
with self.assertRaises(DeprecationWarning):
149+
with self.assertWarns(DeprecationWarning):
150150
self.assertEqual(scheduled.duration, expected_scheduled)
151151

152152
def test_transpile_delay_circuit_with_backend(self):
@@ -158,7 +158,7 @@ def test_transpile_delay_circuit_with_backend(self):
158158
qc, backend=self.backend_with_dt, scheduling_method="alap", layout_method="trivial"
159159
)
160160
target_durations = self.backend_with_dt.target.durations()
161-
with self.assertRaises(DeprecationWarning):
161+
with self.assertWarns(DeprecationWarning):
162162
self.assertEqual(scheduled.duration, target_durations.get("cx", (0, 1)) + 450)
163163

164164
def test_transpile_circuit_with_custom_instruction(self):
@@ -191,14 +191,14 @@ def test_transpile_circuit_with_custom_instruction(self):
191191
target=target,
192192
dt=1e-2,
193193
)
194-
with self.assertRaises(DeprecationWarning):
194+
with self.assertWarns(DeprecationWarning):
195195
self.assertEqual(scheduled.duration, 1500)
196196

197197
def test_transpile_delay_circuit_with_dt_but_without_scheduling_method(self):
198198
qc = QuantumCircuit(1)
199199
qc.delay(100, 0, unit="ns")
200200
transpiled = transpile(qc, backend=self.backend_with_dt)
201-
with self.assertRaises(DeprecationWarning):
201+
with self.assertWarns(DeprecationWarning):
202202
self.assertEqual(transpiled.duration, None) # not scheduled
203203
self.assertEqual(transpiled.data[0].operation.duration, 450) # unit is converted ns -> dt
204204

@@ -208,7 +208,7 @@ def test_transpile_delay_circuit_without_scheduling_method_or_durs(self):
208208
qc.delay(500, 1)
209209
qc.cx(0, 1)
210210
not_scheduled = transpile(qc)
211-
with self.assertRaises(DeprecationWarning):
211+
with self.assertWarns(DeprecationWarning):
212212
self.assertEqual(not_scheduled.duration, None)
213213

214214
def test_raise_error_if_transpile_with_scheduling_method_but_without_durations(self):
@@ -227,7 +227,7 @@ def test_invalidate_schedule_circuit_if_new_instruction_is_appended(self):
227227
scheduled = transpile(qc, backend=self.backend_with_dt, scheduling_method="alap")
228228
# append a gate to a scheduled circuit
229229
scheduled.h(0)
230-
with self.assertRaises(DeprecationWarning):
230+
with self.assertWarns(DeprecationWarning):
231231
self.assertEqual(scheduled.duration, None)
232232

233233
def test_unit_seconds_when_using_backend_durations(self):
@@ -240,7 +240,7 @@ def test_unit_seconds_when_using_backend_durations(self):
240240
qc, backend=self.backend_with_dt, scheduling_method="alap", layout_method="trivial"
241241
)
242242
target_durations = self.backend_with_dt.target.durations()
243-
with self.assertRaises(DeprecationWarning):
243+
with self.assertWarns(DeprecationWarning):
244244
self.assertEqual(scheduled.duration, target_durations.get("cx", (0, 1)) + 500)
245245

246246
def test_per_qubit_durations(self):
@@ -300,7 +300,7 @@ def test_convert_duration_to_dt(self):
300300
circ.measure_all()
301301

302302
circuit_dt = transpile(circ, backend, scheduling_method="asap")
303-
with self.assertRaises(DeprecationWarning):
303+
with self.assertWarns(DeprecationWarning):
304304
# reference duration and unit in dt
305305
ref_duration = circuit_dt.duration
306306
ref_unit = circuit_dt.unit
@@ -490,7 +490,7 @@ def test_change_dt_in_transpile(self):
490490
backend=GenericBackendV2(1, basis_gates=["x"], seed=2, dt=self.dt),
491491
scheduling_method="asap",
492492
)
493-
with self.assertRaises(DeprecationWarning):
493+
with self.assertWarns(DeprecationWarning):
494494
org_duration = scheduled.duration
495495
# halve dt in sec = double duration in dt
496496
scheduled = transpile(
@@ -499,7 +499,7 @@ def test_change_dt_in_transpile(self):
499499
scheduling_method="asap",
500500
dt=self.dt / 2,
501501
)
502-
with self.assertRaises(DeprecationWarning):
502+
with self.assertWarns(DeprecationWarning):
503503
self.assertEqual(scheduled.duration, org_duration * 2)
504504

505505
@data("asap", "alap")
@@ -526,7 +526,7 @@ def test_can_transpile_circuits_after_assigning_parameters(self):
526526
qc.measure(0, 0)
527527
qc = qc.assign_parameters({idle_dur: 0.1})
528528
circ = transpile(qc, self.backend_with_dt)
529-
with self.assertRaises(DeprecationWarning):
529+
with self.assertWarns(DeprecationWarning):
530530
self.assertEqual(circ.duration, None) # not scheduled
531531
self.assertEqual(circ.data[1].operation.duration, 450) # converted in dt
532532

@@ -549,7 +549,7 @@ def test_can_transpile_circuits_with_unbounded_parameters(self):
549549
qc.measure(0, 0)
550550
# not assign parameter
551551
circ = transpile(qc, self.backend_with_dt)
552-
with self.assertRaises(DeprecationWarning):
552+
with self.assertWarns(DeprecationWarning):
553553
self.assertEqual(circ.duration, None) # not scheduled
554554
self.assertEqual(circ.data[1].operation.unit, "dt") # converted in dt
555555
self.assertEqual(
@@ -565,7 +565,7 @@ def test_can_schedule_circuits_with_bounded_parameters(self, scheduling_method):
565565
qc.measure(0, 0)
566566
qc = qc.assign_parameters({idle_dur: 0.1})
567567
circ = transpile(qc, self.backend_with_dt, scheduling_method=scheduling_method)
568-
with self.assertRaises(DeprecationWarning):
568+
with self.assertWarns(DeprecationWarning):
569569
self.assertIsNotNone(circ.duration) # scheduled
570570

571571
@data("asap", "alap")

test/python/compiler/test_transpiler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ def test_circuit_with_delay(self, optimization_level):
13181318
seed_transpiler=42,
13191319
)
13201320

1321-
with self.assertRaises(DeprecationWarning):
1321+
with self.assertWarns(DeprecationWarning):
13221322
self.assertEqual(out.unit, "dt")
13231323
self.assertEqual(out.duration, 1200)
13241324

@@ -1356,7 +1356,7 @@ def test_circuit_with_delay_expr_duration(self, optimization_level):
13561356
seed_transpiler=42,
13571357
)
13581358

1359-
with self.assertRaises(DeprecationWarning):
1359+
with self.assertWarns(DeprecationWarning):
13601360
self.assertEqual(out.unit, "dt")
13611361
self.assertEqual(out.duration, 1200)
13621362

@@ -1649,7 +1649,7 @@ def test_scheduling_instruction_constraints_backend(self):
16491649
scheduling_method="alap",
16501650
layout_method="trivial",
16511651
)
1652-
with self.assertRaises(DeprecationWarning):
1652+
with self.assertWarns(DeprecationWarning):
16531653
self.assertEqual(scheduled.duration, 9010)
16541654

16551655
def test_scheduling_instruction_constraints(self):
@@ -1674,7 +1674,7 @@ def test_scheduling_instruction_constraints(self):
16741674
scheduling_method="alap",
16751675
layout_method="trivial",
16761676
)
1677-
with self.assertRaises(DeprecationWarning):
1677+
with self.assertWarns(DeprecationWarning):
16781678
self.assertEqual(scheduled.duration, 9010)
16791679

16801680
def test_scheduling_dt_constraints(self):
@@ -1687,12 +1687,12 @@ def test_scheduling_dt_constraints(self):
16871687
qc.x(0)
16881688
qc.measure(0, 0)
16891689
scheduled = transpile(qc, backend=backend_v2, scheduling_method="asap")
1690-
with self.assertRaises(DeprecationWarning):
1690+
with self.assertWarns(DeprecationWarning):
16911691
original_duration = scheduled.duration
16921692

16931693
# halve dt in sec = double duration in dt
16941694
scheduled = transpile(qc, backend=backend_v2, scheduling_method="asap", dt=original_dt / 2)
1695-
with self.assertRaises(DeprecationWarning):
1695+
with self.assertWarns(DeprecationWarning):
16961696
self.assertEqual(scheduled.duration, original_duration * 2)
16971697

16981698
@data(1, 2, 3)

test/python/dagcircuit/test_dagcircuit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def test_copy_empty_like(self):
498498
self.assertEqual(self.dag.qubits, result_dag.qubits)
499499
self.assertEqual(self.dag.cregs, result_dag.cregs)
500500
self.assertEqual(self.dag.qregs, result_dag.qregs)
501-
with self.assertRaises(DeprecationWarning):
501+
with self.assertWarns(DeprecationWarning):
502502
self.assertEqual(self.dag.duration, result_dag.duration)
503503
self.assertEqual(self.dag.unit, result_dag.unit)
504504

0 commit comments

Comments
 (0)