Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hypothesis tests for data operators. #1957

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b9bf808
Add hypothesis strategies for qutip data objects.
hodgestar Jul 16, 2022
b7aea8b
Add hypothesis test for operators.
hodgestar Jul 16, 2022
2a22493
Ignore hypothesis folder.
hodgestar Jul 16, 2022
cec89b9
Add hypothesis to test dependencies.
hodgestar Jul 16, 2022
da1ca97
Add assert_allclose and note helpers. Adjust qobj_np strategy to corr…
hodgestar Aug 6, 2022
57f0ee7
Update tests to use new strategy helpers.
hodgestar Aug 6, 2022
c85c38c
Add dtype information to notes.
hodgestar Aug 6, 2022
1d74da8
Add utilities for treating infs as nans and silencing numpy numerical…
hodgestar Aug 6, 2022
9a78395
In addition test, ignore arithmetic warnings and treat inf as nans.
hodgestar Aug 6, 2022
48ca9ab
In subtraction test, ignore arithmetic warnings and treat inf as nans.
hodgestar Aug 6, 2022
486b9db
Update remaining tests to strategy helpers and add test for iszero.
hodgestar Aug 6, 2022
a5b6120
Fix bug in iszero that caused nan's to be considered zero.
hodgestar Aug 6, 2022
4a4f59c
Remove incorrect fast path in mul_csr.
hodgestar Aug 7, 2022
0e80dbf
Add a strategy for generating suitably shaped datas.
hodgestar Aug 7, 2022
38b4130
Add a test for comparing the equality of differently shaped data.
hodgestar Aug 7, 2022
81a5a64
Use qobj_shaped_datas to correctly specify the shapes of datas for ma…
hodgestar Aug 7, 2022
ea6ed3a
Move utilities to the end of the strategies file.
hodgestar Aug 7, 2022
4fc01f2
Add a nicer shared-strategy based helper for generating compatible sh…
hodgestar Aug 7, 2022
d55ebd0
Remove debugging print.
hodgestar Aug 7, 2022
c4f0d35
Fix atol used for expected result in iszero tests.
hodgestar Aug 7, 2022
67aa5cf
Slightly improve formatting of Qobj and Data notes.
hodgestar Aug 7, 2022
2aee70c
Slightly loosen absolute tolerance on matmul test.
hodgestar Aug 7, 2022
78970fe
Merge branch 'dev.major' into feature/add-hypothesis-tests-for-data-o…
hodgestar Aug 19, 2022
7379dfc
Set tolerance when testing iszero.
hodgestar Aug 19, 2022
acf138f
Merge branch 'master' into feature/add-hypothesis-tests-for-data-oper…
hodgestar Apr 11, 2023
e0b8d5e
Merge branch 'master' into feature/add-hypothesis-tests-for-data-oper…
hodgestar Apr 11, 2023
888c411
Add missing 'except *' to Data.trace methods.
hodgestar Apr 11, 2023
7ab5dce
Fix typo and remove TODO.
hodgestar Apr 11, 2023
b48f786
Add tests for trace, adjoint, conj, transpose and copy.
hodgestar Apr 11, 2023
8de0220
Tighten up some tolerance settings.
hodgestar Apr 11, 2023
bda1ed2
Add a strategy specifically for use in testing matmul which is partic…
hodgestar Apr 12, 2023
3394850
Add decorator for asserting that a test raises an exception on specif…
hodgestar Apr 12, 2023
ed8d589
Allow CSR scalar multiplication and division to raise exceptions for …
hodgestar Apr 12, 2023
99c9310
Raise errors when multiplying CSR matrices by non-finite scalars.
hodgestar Apr 12, 2023
0313a6f
Add towncrier entry.
hodgestar Apr 12, 2023
1d56725
Add a link to the documentation on Hypothesis' note function.
hodgestar Apr 12, 2023
0835021
Shorten overly long docstring line.
hodgestar Apr 12, 2023
5d6c0b4
Wrap numpy .trace() with ignore_arithmetic_warnings to avoid overflow…
hodgestar Apr 12, 2023
cb9f849
Use numpy.zeros(a.shape) instead of numpy.zeros_like(a.to_array()).
hodgestar Apr 12, 2023
133677f
Explicitly set rtol to 0 when determining expected value of equality …
hodgestar Apr 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ benchmark/benchmark_data.js
*-tasks.txt
*compiled_coeff*
result_images/

.hypothesis/
83 changes: 83 additions & 0 deletions qutip/tests/core/data/test_operators_hypothesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import numpy

from hypothesis import given, strategies as st

from qutip.core import data as _data
from qutip.tests import strategies as qst


@given(qst.qobj_np(), qst.qobj_dtypes())
def test_data_create(np_array, dtype):
if len(np_array.shape) == 1:
np_array = numpy.atleast_2d(np_array).transpose()
else:
np_array = numpy.atleast_2d(np_array)
data = _data.to(dtype, _data.create(np_array))
numpy.testing.assert_allclose(
data.to_array(), numpy.atleast_2d(np_array), rtol=1e-15
)


@given(qst.qobj_datas())
def test_data_neg_operator(data):
neg = -data
numpy.testing.assert_allclose(
neg.to_array(), -data.to_array(), rtol=1e-15
)


same_shape = st.shared(qst.qobj_shapes())

@given(qst.qobj_datas(shape=same_shape), qst.qobj_datas(shape=same_shape))
def test_data_add_operator(a, b):
result = a + b
numpy.testing.assert_allclose(
result.to_array(), a.to_array() + b.to_array(), rtol=1e-15
)
hodgestar marked this conversation as resolved.
Show resolved Hide resolved


@given(qst.qobj_datas(shape=same_shape), qst.qobj_datas(shape=same_shape))
def test_data_minus_operator(a, b):
result = a - b
numpy.testing.assert_allclose(
result.to_array(), a.to_array() - b.to_array(), rtol=1e-15
)


@given(qst.qobj_datas(shape=same_shape), qst.qobj_datas(shape=same_shape))
def test_data_matmul_operator(a, b):
result = a @ b
numpy.testing.assert_allclose(
result.to_array(), a.to_array() @ b.to_array(), rtol=1e-15
)
hodgestar marked this conversation as resolved.
Show resolved Hide resolved


@given(st.complex_numbers(), qst.qobj_datas(shape=same_shape))
def test_data_scalar_multiplication_left_operator(x, a):
result = x * a
numpy.testing.assert_allclose(
result.to_array(), x * a.to_array(), rtol=1e-15
)


@given(qst.qobj_datas(shape=same_shape), st.complex_numbers())
def test_data_scalar_multiplication_right_operator(a, x):
result = a * x
numpy.testing.assert_allclose(
result.to_array(), a.to_array() * x, rtol=1e-15
)

@given(qst.qobj_datas(shape=same_shape), st.complex_numbers())
def test_data_scalar_division_operator(a, x):
result = a / x
numpy.testing.assert_allclose(
result.to_array(), a.to_array() / x, rtol=1e-15
)


@given(qst.qobj_datas(shape=same_shape), qst.qobj_datas(shape=same_shape))
def test_data_equality_operator(a, b):
result = (a == b)
assert result == numpy.allclose(
a.to_array(), b.to_array(), rtol=1e-15
)
hodgestar marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 51 additions & 0 deletions qutip/tests/strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" Hypothesis strategies for QuTiP. """

import numpy

from hypothesis import strategies as st
from hypothesis.extra import numpy as npst

from qutip.core import data as _data


def qobj_dtypes():
"""
A strategy for Qobj data-layer dtypes.
"""
dtype_list = sorted(_data.to.dtypes, key=lambda t: t.__name__)
return st.sampled_from(dtype_list)


def qobj_shapes():
"""
A strategy for Qobj data-layer shapes.
"""
return npst.array_shapes(max_dims=2)


def qobj_np(shape=qobj_shapes()):
"""
A strategy for returning Qobj compatible numpy arrays.
"""
return npst.arrays(shape=shape, dtype=numpy.complex128)


@st.composite
def qobj_datas(draw, shape=qobj_shapes(), dtype=qobj_dtypes()):
"""
A strategy for returning Qobj data-layer instances.

Parameters
----------
shape : strategy
A strategy to produce the array shapes. Defaults to `qobj_shapes()`.
dtype : strategy
A strategy to produce the array QuTiP dtypes. Defaults to
`qobj_dtypes()`.
"""
# TODO: In future it might be good to have flags like unitary,
# hermitian, ket, dm, oper, etc to restrict the kinds of
# objects produced.
dtype = draw(dtype)
data = draw(qobj_np(shape=shape))
return _data.to(dtype, _data.create(data))
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ semidefinite =
tests =
pytest>=5.2
pytest-rerunfailures
hypothesis
ipython =
ipython
; This uses ConfigParser's string interpolation to include all the above
Expand Down