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

make python examples using_qiskit* and other examples run as a test #970

Closed
jaygambetta opened this issue Sep 29, 2018 · 9 comments · Fixed by #2559
Closed

make python examples using_qiskit* and other examples run as a test #970

jaygambetta opened this issue Sep 29, 2018 · 9 comments · Fixed by #2559
Assignees
Labels
type: qa Issues and PRs that relate to testing and code quality

Comments

@jaygambetta
Copy link
Member

What is the expected enhancement?

Since the using_qiskit are to be use case examples we need to make them part of the tests. We would have found out that job.done was used and I'm hoping user did note make examples based on this when we make this release.

@jaygambetta
Copy link
Member Author

wait until #968 is done.

@delapuente delapuente added the type: qa Issues and PRs that relate to testing and code quality label Oct 1, 2018
@mtreinish
Copy link
Member

Do you mean the examples in examples/python? Or is using_qiskit something else?

If it is what's in examples/python this felt like it would be fairly simple to execute and a good idea. So I wrote a quick PoC (which assumes it's running in test/python) for this:

import os
import subprocess
import sys

import ddt

from . import common

examples_dir = os.path.abspath(os.path.join(
    os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
                 'examples'),
    'python'))
examples = [x for x in os.listdir(examples_dir) if x.endswith('.py')]
#print(examples)

@ddt.ddt
class TestPythonExamples(common.QiskitTestCase):

    @ddt.data(*examples)
    def test_example(self, example):
        """Execute the example pythonf file and pass if it returns 0"""
        print(example)
        example_path = os.path.join(examples_dir, example)
        cmd = [sys.executable, example_path]
        run_example = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
        stdout, stderr = run_example.communicate()
        error_string = "Running example %s failed with return code %s\n" % (
            example, run_example.returncode)
        error_string += "\tstdout:%s\n\tstderr: %s" % (stdout, stderr)
        self.assertEqual(run_example.returncode, 0, error_string)

The advantage of testing it this way is that whenever we add (or remove) a new example we don't need to update anything on the testing side it will automatically run.

But after testing this it raised a few questions. First, several of the examples there expect to be able to use the IBMQ provider to talk to the api/backends how did we want to handle that? My first thoughts were to make a blacklist and/or tag those examples somehow (maybe a subdir) and skip those based on the run online tests flag. The other issue it raised is that the visualization example is meant to be run interactively. It calls image show to open up the circuit in a new window which holds the process open, so the test doesn't finish until someone actively closes the window. Should we skip that file or just modify the example to not open a new window.The last question I had was do you think just executing the examples in a subprocess and seeing if they work or not sufficient? Or do you think we should we build targeted tests that run each of the examples from inside the same process as the test runner and probe the internals of the examples?

@jaygambetta
Copy link
Member Author

I think this should not be part of Terra but the mega test. My main point is in the last update things were changing and the examples were not. Since these define how different users use qiskit we need to know when they break. They are more like a full integration test.

I just feel we keep talking about it but I want a full integration to have a green light that says tests pass.

@mtreinish
Copy link
Member

mtreinish commented Oct 11, 2018

Sure, where the test lives doesn't actually matter too much, especially if it's like what I pasted before. That would just need the relative path updated to work from anywhere. Although, if we do have it in an external test suite we will have to fix that examples/ in terra isn't actually in the sdist so it may not be present if it's just pip installed (but we can probably solve that by just adding it to the manifest file I think).

@jaygambetta
Copy link
Member Author

I agree. I am not sure what the best thing is to do. I let you decide. the workflow is more how do we make sure that things dont break for Aer<->Terra<->IBMQ and standard user tasks.

@delapuente
Copy link
Contributor

For the record, @diego-plan9 wrote https://github.com/Qiskit/qiskit-tutorial/blob/master/utils/test/test_tutorials.py to help testing the tutorials. Perhaps we should include it as part of the regular tests.

@jaygambetta
Copy link
Member Author

Maybe once we finish separateing the tutorial from ones that teach quantum using qiskit from those that teach qiskit. This is going to take a long time (your welcome to help there) so I would much rather a solution using these examples.

@jaygambetta
Copy link
Member Author

Also for the record I know it exists but because we don’t make the all tutorials qiskit 0.6 as many are community contributions and we don’t have the bandwidth to keep all updated we decided not to do it.

@jaygambetta jaygambetta changed the title make python examples using_qiskit* run as a test make python examples using_qiskit* and other examples run as a test Dec 16, 2018
@ajavadia
Copy link
Member

@mtreinish is there any update on this? I think what you pasted above works in the sense that it makes sure no error is raised while executing the examples. Later on we may want to test some correctness aspects as well, but for now just need to make sure they're not broken.

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jun 11, 2019
This commit adds a new test module that will execute all the example
scripts to verify they still work as we make changes. However, a number
of example scripts rely on using the ibmq service which is an optional
component now. To make filtering the example scripts between those which
don't need ibmq from those that need ibmq this also moves all the ibmq
examples into a self contained subdir in examples/python. Those examples
which require ibmq are run conditionally as online_tests. For example
scripts which were running on both local simulators and ibmq these are
split into 2 files one for just simulation and the other in ibmq that
uses the ibmq backends.

Fixes Qiskit#970
mtreinish added a commit that referenced this issue Jun 17, 2019
* Add tests that run example python scripts

This commit adds a new test module that will execute all the example
scripts to verify they still work as we make changes. However, a number
of example scripts rely on using the ibmq service which is an optional
component now. To make filtering the example scripts between those which
don't need ibmq from those that need ibmq this also moves all the ibmq
examples into a self contained subdir in examples/python. Those examples
which require ibmq are run conditionally as online_tests. For example
scripts which were running on both local simulators and ibmq these are
split into 2 files one for just simulation and the other in ibmq that
uses the ibmq backends.

Fixes #970

* Fix whitespace in ghz

* Fix whitespace in qft

* Fix lint

* bit0 should not be a tuple

* Decode subprocess stdout and stderr

* Mark online tests as slow, since they run on devices

* Pivot to use subtest instead of ddt

* Update ibmq example based on rebase changes

* Add skips for windows/appveyor

The text drawer isn't working correctly on windows/appveyor. Until that
unrelated issue is fixed it will block this. So this commit adds a skip
on windows envs until issue #2616 is fixed.

* Remove outdated comment
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this issue Jul 30, 2019
* Add tests that run example python scripts

This commit adds a new test module that will execute all the example
scripts to verify they still work as we make changes. However, a number
of example scripts rely on using the ibmq service which is an optional
component now. To make filtering the example scripts between those which
don't need ibmq from those that need ibmq this also moves all the ibmq
examples into a self contained subdir in examples/python. Those examples
which require ibmq are run conditionally as online_tests. For example
scripts which were running on both local simulators and ibmq these are
split into 2 files one for just simulation and the other in ibmq that
uses the ibmq backends.

Fixes Qiskit#970

* Fix whitespace in ghz

* Fix whitespace in qft

* Fix lint

* bit0 should not be a tuple

* Decode subprocess stdout and stderr

* Mark online tests as slow, since they run on devices

* Pivot to use subtest instead of ddt

* Update ibmq example based on rebase changes

* Add skips for windows/appveyor

The text drawer isn't working correctly on windows/appveyor. Until that
unrelated issue is fixed it will block this. So this commit adds a skip
on windows envs until issue Qiskit#2616 is fixed.

* Remove outdated comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: qa Issues and PRs that relate to testing and code quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants