unitaryfund / mitiq

Mitiq is an open source toolkit for implementing error mitigation techniques on most current intermediate-scale quantum computers.
https://mitiq.readthedocs.io
GNU General Public License v3.0
363 stars 160 forks source link

Robust testing (e.g. randomness) #132

Closed nathanshammah closed 4 years ago

nathanshammah commented 4 years ago

As pointed out in #88 there can be issues related to testing (locally and in CI) randomness in mitiq. In https://github.com/unitaryfund/mitiq/pull/88#issuecomment-613135104 one issue seems related to seeds.

There are various options that can be considered, such as setting the seeds for testing.

stick to pytest and be creative

A quick fix in testing may be to add randomly generated data that then is pointed at or fix seeds, https://github.com/unitaryfund/mitiq/pull/88#issuecomment-613410866. Pros: fast (?) Cons: technical debt.

pytest-randomly plugin

pytest-randomly is a pytest plugin that addresses this kind of issues. It allows to control random.seed, rather than numpy.random.seed.
Pros: pytest plugin, supports doctest. Cons: not super popular, not designed for numpy

property-based testing with Hypothesis

Hypothesis is a library that aims at changing the way tests are designed, allegedly: it should go from testing an instance to designing a test that applies to a domain of instances (property-based testing). It is not super clear to me right now.
It contains various randomness-related features, including a seed function.

Pros: sounds powerful and clever, popular and growing, well documented, more robust even beyond this randomness problems. Cons: radical change of testing framework (?), steep learning curve (?), overkill (?).

I also admit I used nose until recently / used with pytest tests thought for nose, without taking advantage of pytest full power.

andreamari commented 4 years ago

I have probably found how to fix the seed problem with our tests. This is just a quick solution to fix our problems and doesn't preclude investigating also the alternative proposals of @nathanshammah.

I locally tested this approach in test_factories.py of the PR #88, it solved the problem of the failing test.