Skip to content

Commit

Permalink
infra: refactor pytest configuration into the pyproject file
Browse files Browse the repository at this point in the history
  • Loading branch information
Coull committed Oct 23, 2024
1 parent 36e32f6 commit b9abb7e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 98 deletions.
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
[tool.pytest.ini_options]
xfail_strict = true
# https://pytest-xdist.readthedocs.io/en/latest/known-limitations.html
addopts = "--verbose -n logical --durations=0 --durations-min=1 --dist worksteal"
testpaths = ["test/unit_tests",]
# Issue #557 in `pytest-cov` (currently v4.x) has not moved for a while now,
# but once a resolution has been adopted we can drop this "ignore".
# Ref: https://github.com/pytest-dev/pytest-cov/issues/557
filterwarnings = [
"ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning",
]
norecursedirs = [
".tox",
".git",
"*/migrations/*",
"*/static/*",
"docs",
"venv",
"*/{{cookiecutter.project_slug}}/*",
]

[tool.black]
line-length = 100
12 changes: 0 additions & 12 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
[aliases]
test=pytest

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

[isort]
line_length = 100
multi_line_output = 3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
install_requires=[
"amazon-braket-schemas>=1.21.3",
"amazon-braket-default-simulator>=1.26.0",
"oqpy~=0.3.5",
"oqpy",
"backoff",
"boltons",
"boto3>=1.28.53",
Expand Down
2 changes: 1 addition & 1 deletion src/braket/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def add_circuit(
if target_mapping and target is not None:
raise TypeError("Only one of 'target_mapping' or 'target' can be supplied.")
elif target is not None:
keys = sorted(circuit.qubits)
keys = circuit.qubits
values = target
target_mapping = dict(zip(keys, values))

Expand Down
169 changes: 85 additions & 84 deletions test/integ_tests/gate_model_device_testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,21 @@ def result_types_tensor_x_y_testing(device: Device, run_kwargs: dict[str, Any]):
varphi = -0.543
obs = Observable.X() @ Observable.Y()
obs_targets = [0, 2]
expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
expected_var = (
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
- np.cos(2 * (theta - phi))
- np.cos(2 * (theta + phi))
+ 2 * np.cos(2 * theta)
+ 2 * np.cos(2 * phi)
+ 14
) / 16
expected_eigs = get_pauli_eigenvalues(1)
circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
expected_var = (
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
- np.cos(2 * (theta - phi))
- np.cos(2 * (theta + phi))
+ 2 * np.cos(2 * theta)
+ 2 * np.cos(2 * phi)
+ 14
) / 16
expected_eigs = get_pauli_eigenvalues(1)

assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -315,15 +314,15 @@ def result_types_tensor_z_z_testing(device: Device, run_kwargs: dict[str, Any]):
varphi = -0.543
obs = Observable.Z() @ Observable.Z()
obs_targets = [0, 2]
expected_mean = 0.849694136476246
expected_var = 0.27801987443788634
expected_eigs = get_pauli_eigenvalues(1)

circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = 0.849694136476246
expected_var = 0.27801987443788634
expected_eigs = get_pauli_eigenvalues(1)

assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -345,15 +344,15 @@ def result_types_tensor_hermitian_hermitian_testing(device: Device, run_kwargs:
)
obs = Observable.Hermitian(matrix1) @ Observable.Hermitian(matrix2)
obs_targets = [0, 1, 2]
expected_mean = -4.30215023196904
expected_var = 370.71292282796804
expected_eigs = np.array([-70.90875406, -31.04969387, 0, 3.26468993, 38.693758])

circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = -4.30215023196904
expected_var = 370.71292282796804
expected_eigs = np.array([-70.90875406, -31.04969387, 0, 3.26468993, 38.693758])

assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -366,21 +365,20 @@ def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: dict[str, Any]
varphi = -0.543
obs = Observable.Z() @ Observable.H() @ Observable.Y()
obs_targets = [0, 1, 2]
expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
expected_var = (
3
+ np.cos(2 * phi) * np.cos(varphi) ** 2
- np.cos(2 * theta) * np.sin(varphi) ** 2
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
) / 4
expected_eigs = get_pauli_eigenvalues(1)

circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(
2
)
expected_var = (
3
+ np.cos(2 * phi) * np.cos(varphi) ** 2
- np.cos(2 * theta) * np.sin(varphi) ** 2
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
) / 4
expected_eigs = get_pauli_eigenvalues(1)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -401,50 +399,51 @@ def result_types_tensor_z_hermitian_testing(device: Device, run_kwargs: dict[str
)
obs = Observable.Z() @ Observable.Hermitian(array)
obs_targets = [0, 1, 2]
expected_mean = 0.5 * (
-6 * np.cos(theta) * (np.cos(varphi) + 1)
- 2 * np.sin(varphi) * (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi))
+ 3 * np.cos(varphi) * np.sin(phi)
+ np.sin(phi)
)
expected_var = (
1057
- np.cos(2 * phi)
+ 12 * (27 + np.cos(2 * phi)) * np.cos(varphi)
- 2 * np.cos(2 * varphi) * np.sin(phi) * (16 * np.cos(phi) + 21 * np.sin(phi))
+ 16 * np.sin(2 * phi)
- 8 * (-17 + np.cos(2 * phi) + 2 * np.sin(2 * phi)) * np.sin(varphi)
- 8 * np.cos(2 * theta) * (3 + 3 * np.cos(varphi) + np.sin(varphi)) ** 2
- 24 * np.cos(phi) * (np.cos(phi) + 2 * np.sin(phi)) * np.sin(2 * varphi)
- 8
* np.cos(theta)
* (
4
* np.cos(phi)
* (
4
+ 8 * np.cos(varphi)
+ np.cos(2 * varphi)
- (1 + 6 * np.cos(varphi)) * np.sin(varphi)
)
+ np.sin(phi)
* (
15
+ 8 * np.cos(varphi)
- 11 * np.cos(2 * varphi)
+ 42 * np.sin(varphi)
+ 3 * np.sin(2 * varphi)
)
)
) / 16

z_array = np.diag([1, -1])
expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))

circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = 0.5 * (
-6 * np.cos(theta) * (np.cos(varphi) + 1)
- 2 * np.sin(varphi) * (np.cos(theta) + np.sin(phi) - 2 * np.cos(phi))
+ 3 * np.cos(varphi) * np.sin(phi)
+ np.sin(phi)
)
expected_var = (
1057
- np.cos(2 * phi)
+ 12 * (27 + np.cos(2 * phi)) * np.cos(varphi)
- 2 * np.cos(2 * varphi) * np.sin(phi) * (16 * np.cos(phi) + 21 * np.sin(phi))
+ 16 * np.sin(2 * phi)
- 8 * (-17 + np.cos(2 * phi) + 2 * np.sin(2 * phi)) * np.sin(varphi)
- 8 * np.cos(2 * theta) * (3 + 3 * np.cos(varphi) + np.sin(varphi)) ** 2
- 24 * np.cos(phi) * (np.cos(phi) + 2 * np.sin(phi)) * np.sin(2 * varphi)
- 8
* np.cos(theta)
* (
4
* np.cos(phi)
* (
4
+ 8 * np.cos(varphi)
+ np.cos(2 * varphi)
- (1 + 6 * np.cos(varphi)) * np.sin(varphi)
)
+ np.sin(phi)
* (
15
+ 8 * np.cos(varphi)
- 11 * np.cos(2 * varphi)
+ 42 * np.sin(varphi)
+ 3 * np.sin(2 * varphi)
)
)
) / 16

z_array = np.diag([1, -1])
expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -465,15 +464,16 @@ def result_types_tensor_y_hermitian_testing(device: Device, run_kwargs: dict[str
)
obs = Observable.Y() @ Observable.Hermitian(array)
obs_targets = [0, 1, 2]
expected_mean = 1.4499810303182408
expected_var = 74.03174647518193
y_array = np.array([[0, -1j], [1j, 0]])
expected_eigs = np.linalg.eigvalsh(np.kron(y_array, array))

circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = 1.4499810303182408
expected_var = 74.03174647518193
y_array = np.array([[0, -1j], [1j, 0]])
expected_eigs = np.linalg.eigvalsh(np.kron(y_array, array))
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)
Expand All @@ -498,6 +498,19 @@ def result_types_noncommuting_testing(device: Device, run_kwargs: dict[str, Any]
obs2_targets = [0, 2]
obs3 = Observable.Y() @ Observable.Hermitian(array)
obs3_targets = [0, 1, 2]
expected_mean1 = np.sin(theta) * np.sin(phi) * np.sin(varphi)
expected_var1 = (
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
- np.cos(2 * (theta - phi))
- np.cos(2 * (theta + phi))
+ 2 * np.cos(2 * theta)
+ 2 * np.cos(2 * phi)
+ 14
) / 16

expected_mean2 = 0.849694136476246
expected_mean3 = 1.4499810303182408

circuit = (
get_result_types_three_qubit_circuit(theta, phi, varphi, obs1, obs1_targets, shots)
.expectation(obs2, obs2_targets)
Expand All @@ -507,18 +520,6 @@ def result_types_noncommuting_testing(device: Device, run_kwargs: dict[str, Any]
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean1 = np.sin(theta) * np.sin(phi) * np.sin(varphi)
expected_var1 = (
8 * np.sin(theta) ** 2 * np.cos(2 * varphi) * np.sin(phi) ** 2
- np.cos(2 * (theta - phi))
- np.cos(2 * (theta + phi))
+ 2 * np.cos(2 * theta)
+ 2 * np.cos(2 * phi)
+ 14
) / 16

expected_mean2 = 0.849694136476246
expected_mean3 = 1.4499810303182408
assert np.allclose(result.values[0], expected_var1)
assert np.allclose(result.values[1], expected_mean1)
assert np.allclose(result.values[2], expected_mean2)
Expand Down

0 comments on commit b9abb7e

Please sign in to comment.