OpenFermion-Cirq icon indicating copy to clipboard operation
OpenFermion-Cirq copied to clipboard

Test that code in Jupyter notebooks gives correct output

Open kevinsung opened this issue 7 years ago • 9 comments
trafficstars

We should have something like snippets_test.py from Cirq.

kevinsung avatar May 27 '18 19:05 kevinsung

I've found that this currently does not work because of things like

print(operator)

where operator is a SymbolicOperator from OpenFermion. The reason is that SymbolicOperator.__str__ iterates over the terms dictionary and the order of iteration is unpredictable in versions of Python less than 3.7. This causes Jupyter notebooks run in different environments (e.g. my computer vs. Travis's server) to have different outputs. One solution to this is to have SymbolicOperator store terms as an OrderedDict.

kevinsung avatar Jul 09 '18 01:07 kevinsung

I don't think we want these to be OrderedDicts. Why do you care if it prints terms in a different order? Just for testing purposes? That is not a good reason to make this fairly substantial change to perhaps the most important data structure in OpenFermion.

babbush avatar Jul 09 '18 01:07 babbush

Okay, then what do you think about having SymbolicOperator print its terms in some canonical order? I think this would be useful anyway for visually comparing different operators. I don't think we need to worry about the cost of sorting the terms because if your operator is large enough where it matters then you probably don't want to be printing it out anyway.

kevinsung avatar Jul 09 '18 01:07 kevinsung

That sounds like a better idea. I'm not opposed to it but I also don't view this is a priority.

babbush avatar Jul 09 '18 01:07 babbush

I recommend sorting the terms in __str__ before you output them. That's what I always do (e.g. see XmonSimulateTrialResult in cirq). It makes the method actually testable.

Strilanc avatar Jul 09 '18 06:07 Strilanc

The test was removed in #158 so this is an issue again. Not a high priority for me right now though.

kevinsung avatar Jul 14 '18 06:07 kevinsung

This is not a super high priority, but its pretty important because otherwise future changes to the library will definitely break the tutorials without us noticing. Is there a problem with what I did in OpenFermion? Not fancy enough for you? lol

babbush avatar Jul 14 '18 06:07 babbush

The way OpenFermion currently tests notebooks doesn't test that the output is correct. I guess it's better than nothing though. I can easily add a test that simply runs the code though without checking the output though.

kevinsung avatar Jul 14 '18 06:07 kevinsung

Well very often the sort of changes we're most worried about are changes to the interface that cause very obvious execution failures rather than a subtle change to the output. So this is definitely better than nothing.

babbush avatar Jul 14 '18 06:07 babbush