Add doctest workflow - #838
Conversation
…qtypes, and expand incrementally throughout the code base as flagged issues are resolved
…press CI output to avoid doctest failures
|
Thanks @renezander90 . Should we start adding |
|
@PietropaoloFrisoni Feel free to edit the example docstring example in #830 if you think it needs something additional. we likely won't be turning on doctest for quite a while. I was planning to take a look at this myself after QCE. Thankfully, Rene jumped on this :) |
|
@renezander90 Since you started looking into this, could you check if With |
|
It is recommended to use a combination of Using only The main issue is that
For example, this is weaker as a regression test: .. jupyter-execute::
qf = QuantumFloat(3, -1)
qf[:] = 2.5
print(qf)It verifies that the code runs, but an incorrect printed value may not fail the build in a meaningful way because the output is generated automatically. This is stronger: .. jupyter-execute::
qf = QuantumFloat(3, -1)
qf[:] = 2.5
assert qf.get_measurement() == {2.5: 1.0}My recommendation remains: Using only |
|
@PietropaoloFrisoni Use >>> print("start")
>>> print()
>>> print("end")
start
<BLANKLINE>
endDo not use it for formatting the docstring or separating examples: >>> x = 1
>>> x + 1
2That blank line separates two doctest interactions; it is not output. Also do not use it when the output has no blank line: >>> print("start")
start
>>> print("end")
endSo the rule is: use |
|
It should even appear in the rendered |
Actually, I would like to turn it on as soon as possible. We could first keep it restricted to Regarding the necessary formatting changes, this can be done quite fast with AI. Only bugs or more fundamental problems have to be resolved manually (e.g. the arithmetic bugs I discovered when using doctest on the |
|
@renezander90 If that's the case, before turning it on, could you compare
If you say so! I am a bit cautious that the LLM could make unnecessary changes that I would fail to catch. |
|
Some recommendation on dealing with randomness: Recommendation for nondeterministic examplesThe goal should be to keep examples natural while avoiding assertions about values that are inherently unstable.
Deterministic output>>> qf = QuantumFloat(3, -1)
>>> qf[:] = 2.5
>>> qf.get_measurement()
{2.5: 1.0}Floating-point resultsInstead of maintaining a fragile decimal representation: >>> computed_value = calculate_value()
>>> abs(computed_value - expected_value) < 1e-8
TrueThis keeps the example readable while checking the meaningful behavior. Variable formattingFor generated names or partially unstable textual output: >>> print(circuit) # doctest: +ELLIPSIS
QuantumCircuit: ...Use this only when the omitted portion is genuinely irrelevant. Do not hide the actual result behind For insignificant whitespace differences: >>> print(description) # doctest: +NORMALIZE_WHITESPACE
A description with normalized whitespace.Probabilistic quantum resultsAvoid showing one exact measurement result when several outcomes are valid: >>> qv = QuantumVariable(1)
>>> h(qv)
>>> measurement = measure(qv)
>>> set(measurement).issubset({"0", "1"})
True
>>> sum(measurement.values())
1.0For statistical behavior, use a sufficiently large number of shots and test a broad property rather than exact frequencies. Detailed statistical checks are usually better placed in unit tests than in doctests. Random or unsuitable examples
>>> random_result() # doctest: +SKIP
0.731842...The result is displayed as illustrative documentation, but the code is not executed or checked. Add prose making this clear:
>>> run_on_hardware_backend() # doctest: +SKIP
...Use it sparingly, because skipped examples provide no automated correctness guarantee. The resulting policy is: |
Description
Implementation strategy: run only on src/qrisp/qtypes, and expand incrementally throughout the code base as flagged issues are resolved.
Related Issues
Closes #
Related to #830
Type of Change
Breaking Change?
If yes, describe the impact and migration path:
What was changed?
How was it tested?
Screenshots / Output (if applicable)
Checklist
changelog-dev.rstReviewer Notes