Skip to content

Commit a64be7e

Browse files
authored
infra: refactor pytest configuration into the pyproject file (#1040)
1 parent ecdf4d9 commit a64be7e

File tree

3 files changed

+107
-96
lines changed

3 files changed

+107
-96
lines changed

pyproject.toml

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
2+
[tool.pytest.ini_options]
3+
xfail_strict = true
4+
# https://pytest-xdist.readthedocs.io/en/latest/known-limitations.html
5+
addopts = "--verbose -n logical --durations=0 --durations-min=1 --dist worksteal"
6+
testpaths = ["test/unit_tests",]
7+
# Issue #557 in `pytest-cov` (currently v4.x) has not moved for a while now,
8+
# but once a resolution has been adopted we can drop this "ignore".
9+
# Ref: https://github.com/pytest-dev/pytest-cov/issues/557
10+
filterwarnings = [
11+
"ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning",
12+
]
13+
norecursedirs = [
14+
".tox",
15+
".git",
16+
"*/migrations/*",
17+
"*/static/*",
18+
"docs",
19+
"venv",
20+
"*/{{cookiecutter.project_slug}}/*",
21+
]
22+
123
[tool.ruff]
224
target-version = "py39"
325
line-length = 100

setup.cfg

-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
[aliases]
22
test=pytest
33

4-
[tool:pytest]
5-
xfail_strict = true
6-
# https://pytest-xdist.readthedocs.io/en/latest/known-limitations.html
7-
addopts =
8-
--verbose -n logical --durations=0 --durations-min=1 --dist worksteal
9-
testpaths = test/unit_tests
10-
filterwarnings=
11-
# Issue #557 in `pytest-cov` (currently v4.x) has not moved for a while now,
12-
# but once a resolution has been adopted we can drop this "ignore".
13-
# Ref: https://github.com/pytest-dev/pytest-cov/issues/557
14-
ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning
15-
164

test/integ_tests/gate_model_device_testing_utils.py

+85-84
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,21 @@ def result_types_tensor_x_y_testing(device: Device, run_kwargs: dict[str, Any]):
287287
varphi = -0.543
288288
obs = Observable.X() @ Observable.Y()
289289
obs_targets = [0, 2]
290+
expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
291+
expected_var = (
292+
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
293+
- np.cos(2 * (theta - phi))
294+
- np.cos(2 * (theta + phi))
295+
+ 2 * np.cos(2 * theta)
296+
+ 2 * np.cos(2 * phi)
297+
+ 14
298+
) / 16
299+
expected_eigs = get_pauli_eigenvalues(1)
290300
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
291301
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
292302
for task in tasks:
293303
result = device.run(task, **run_kwargs).result()
294304

295-
expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
296-
expected_var = (
297-
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
298-
- np.cos(2 * (theta - phi))
299-
- np.cos(2 * (theta + phi))
300-
+ 2 * np.cos(2 * theta)
301-
+ 2 * np.cos(2 * phi)
302-
+ 14
303-
) / 16
304-
expected_eigs = get_pauli_eigenvalues(1)
305-
306305
assert_variance_expectation_sample_result(
307306
result, shots, expected_var, expected_mean, expected_eigs
308307
)
@@ -315,15 +314,15 @@ def result_types_tensor_z_z_testing(device: Device, run_kwargs: dict[str, Any]):
315314
varphi = -0.543
316315
obs = Observable.Z() @ Observable.Z()
317316
obs_targets = [0, 2]
317+
expected_mean = 0.849694136476246
318+
expected_var = 0.27801987443788634
319+
expected_eigs = get_pauli_eigenvalues(1)
320+
318321
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
319322
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
320323
for task in tasks:
321324
result = device.run(task, **run_kwargs).result()
322325

323-
expected_mean = 0.849694136476246
324-
expected_var = 0.27801987443788634
325-
expected_eigs = get_pauli_eigenvalues(1)
326-
327326
assert_variance_expectation_sample_result(
328327
result, shots, expected_var, expected_mean, expected_eigs
329328
)
@@ -343,15 +342,15 @@ def result_types_tensor_hermitian_hermitian_testing(device: Device, run_kwargs:
343342
])
344343
obs = Observable.Hermitian(matrix1) @ Observable.Hermitian(matrix2)
345344
obs_targets = [0, 1, 2]
345+
expected_mean = -4.30215023196904
346+
expected_var = 370.71292282796804
347+
expected_eigs = np.array([-70.90875406, -31.04969387, 0, 3.26468993, 38.693758])
348+
346349
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
347350
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
348351
for task in tasks:
349352
result = device.run(task, **run_kwargs).result()
350353

351-
expected_mean = -4.30215023196904
352-
expected_var = 370.71292282796804
353-
expected_eigs = np.array([-70.90875406, -31.04969387, 0, 3.26468993, 38.693758])
354-
355354
assert_variance_expectation_sample_result(
356355
result, shots, expected_var, expected_mean, expected_eigs
357356
)
@@ -364,21 +363,20 @@ def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: dict[str, Any]
364363
varphi = -0.543
365364
obs = Observable.Z() @ Observable.H() @ Observable.Y()
366365
obs_targets = [0, 1, 2]
366+
expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
367+
expected_var = (
368+
3
369+
+ np.cos(2 * phi) * np.cos(varphi) ** 2
370+
- np.cos(2 * theta) * np.sin(varphi) ** 2
371+
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
372+
) / 4
373+
expected_eigs = get_pauli_eigenvalues(1)
374+
367375
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
368376
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
369377
for task in tasks:
370378
result = device.run(task, **run_kwargs).result()
371379

372-
expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(
373-
2
374-
)
375-
expected_var = (
376-
3
377-
+ np.cos(2 * phi) * np.cos(varphi) ** 2
378-
- np.cos(2 * theta) * np.sin(varphi) ** 2
379-
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
380-
) / 4
381-
expected_eigs = get_pauli_eigenvalues(1)
382380
assert_variance_expectation_sample_result(
383381
result, shots, expected_var, expected_mean, expected_eigs
384382
)
@@ -397,50 +395,51 @@ def result_types_tensor_z_hermitian_testing(device: Device, run_kwargs: dict[str
397395
])
398396
obs = Observable.Z() @ Observable.Hermitian(array)
399397
obs_targets = [0, 1, 2]
398+
expected_mean = 0.5 * (
399+
-6 * np.cos(theta) * (np.cos(varphi) + 1)
400+
- 2 * np.sin(varphi) * (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi))
401+
+ 3 * np.cos(varphi) * np.sin(phi)
402+
+ np.sin(phi)
403+
)
404+
expected_var = (
405+
1057
406+
- np.cos(2 * phi)
407+
+ 12 * (27 + np.cos(2 * phi)) * np.cos(varphi)
408+
- 2 * np.cos(2 * varphi) * np.sin(phi) * (16 * np.cos(phi) + 21 * np.sin(phi))
409+
+ 16 * np.sin(2 * phi)
410+
- 8 * (-17 + np.cos(2 * phi) + 2 * np.sin(2 * phi)) * np.sin(varphi)
411+
- 8 * np.cos(2 * theta) * (3 + 3 * np.cos(varphi) + np.sin(varphi)) ** 2
412+
- 24 * np.cos(phi) * (np.cos(phi) + 2 * np.sin(phi)) * np.sin(2 * varphi)
413+
- 8
414+
* np.cos(theta)
415+
* (
416+
4
417+
* np.cos(phi)
418+
* (
419+
4
420+
+ 8 * np.cos(varphi)
421+
+ np.cos(2 * varphi)
422+
- (1 + 6 * np.cos(varphi)) * np.sin(varphi)
423+
)
424+
+ np.sin(phi)
425+
* (
426+
15
427+
+ 8 * np.cos(varphi)
428+
- 11 * np.cos(2 * varphi)
429+
+ 42 * np.sin(varphi)
430+
+ 3 * np.sin(2 * varphi)
431+
)
432+
)
433+
) / 16
434+
435+
z_array = np.diag([1, -1])
436+
expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))
437+
400438
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
401439
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
402440
for task in tasks:
403441
result = device.run(task, **run_kwargs).result()
404442

405-
expected_mean = 0.5 * (
406-
-6 * np.cos(theta) * (np.cos(varphi) + 1)
407-
- 2 * np.sin(varphi) * (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi))
408-
+ 3 * np.cos(varphi) * np.sin(phi)
409-
+ np.sin(phi)
410-
)
411-
expected_var = (
412-
1057
413-
- np.cos(2 * phi)
414-
+ 12 * (27 + np.cos(2 * phi)) * np.cos(varphi)
415-
- 2 * np.cos(2 * varphi) * np.sin(phi) * (16 * np.cos(phi) + 21 * np.sin(phi))
416-
+ 16 * np.sin(2 * phi)
417-
- 8 * (-17 + np.cos(2 * phi) + 2 * np.sin(2 * phi)) * np.sin(varphi)
418-
- 8 * np.cos(2 * theta) * (3 + 3 * np.cos(varphi) + np.sin(varphi)) ** 2
419-
- 24 * np.cos(phi) * (np.cos(phi) + 2 * np.sin(phi)) * np.sin(2 * varphi)
420-
- 8
421-
* np.cos(theta)
422-
* (
423-
4
424-
* np.cos(phi)
425-
* (
426-
4
427-
+ 8 * np.cos(varphi)
428-
+ np.cos(2 * varphi)
429-
- (1 + 6 * np.cos(varphi)) * np.sin(varphi)
430-
)
431-
+ np.sin(phi)
432-
* (
433-
15
434-
+ 8 * np.cos(varphi)
435-
- 11 * np.cos(2 * varphi)
436-
+ 42 * np.sin(varphi)
437-
+ 3 * np.sin(2 * varphi)
438-
)
439-
)
440-
) / 16
441-
442-
z_array = np.diag([1, -1])
443-
expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))
444443
assert_variance_expectation_sample_result(
445444
result, shots, expected_var, expected_mean, expected_eigs
446445
)
@@ -459,15 +458,16 @@ def result_types_tensor_y_hermitian_testing(device: Device, run_kwargs: dict[str
459458
])
460459
obs = Observable.Y() @ Observable.Hermitian(array)
461460
obs_targets = [0, 1, 2]
461+
expected_mean = 1.4499810303182408
462+
expected_var = 74.03174647518193
463+
y_array = np.array([[0, -1j], [1j, 0]])
464+
expected_eigs = np.linalg.eigvalsh(np.kron(y_array, array))
465+
462466
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
463467
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
464468
for task in tasks:
465469
result = device.run(task, **run_kwargs).result()
466470

467-
expected_mean = 1.4499810303182408
468-
expected_var = 74.03174647518193
469-
y_array = np.array([[0, -1j], [1j, 0]])
470-
expected_eigs = np.linalg.eigvalsh(np.kron(y_array, array))
471471
assert_variance_expectation_sample_result(
472472
result, shots, expected_var, expected_mean, expected_eigs
473473
)
@@ -490,6 +490,19 @@ def result_types_noncommuting_testing(device: Device, run_kwargs: dict[str, Any]
490490
obs2_targets = [0, 2]
491491
obs3 = Observable.Y() @ Observable.Hermitian(array)
492492
obs3_targets = [0, 1, 2]
493+
expected_mean1 = np.sin(theta) * np.sin(phi) * np.sin(varphi)
494+
expected_var1 = (
495+
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
496+
- np.cos(2 * (theta - phi))
497+
- np.cos(2 * (theta + phi))
498+
+ 2 * np.cos(2 * theta)
499+
+ 2 * np.cos(2 * phi)
500+
+ 14
501+
) / 16
502+
503+
expected_mean2 = 0.849694136476246
504+
expected_mean3 = 1.4499810303182408
505+
493506
circuit = (
494507
get_result_types_three_qubit_circuit(theta, phi, varphi, obs1, obs1_targets, shots)
495508
.expectation(obs2, obs2_targets)
@@ -499,18 +512,6 @@ def result_types_noncommuting_testing(device: Device, run_kwargs: dict[str, Any]
499512
for task in tasks:
500513
result = device.run(task, **run_kwargs).result()
501514

502-
expected_mean1 = np.sin(theta) * np.sin(phi) * np.sin(varphi)
503-
expected_var1 = (
504-
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
505-
- np.cos(2 * (theta - phi))
506-
- np.cos(2 * (theta + phi))
507-
+ 2 * np.cos(2 * theta)
508-
+ 2 * np.cos(2 * phi)
509-
+ 14
510-
) / 16
511-
512-
expected_mean2 = 0.849694136476246
513-
expected_mean3 = 1.4499810303182408
514515
assert np.allclose(result.values[0], expected_var1)
515516
assert np.allclose(result.values[1], expected_mean1)
516517
assert np.allclose(result.values[2], expected_mean2)

0 commit comments

Comments
 (0)