From dc0fb1c944788cf9eef4dad602483ba839ddab71 Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Tue, 25 Jul 2023 00:08:10 +0000 Subject: [PATCH 01/13] Add cudaq::set_random_seed(), turn the python backends tests on, fix bugs there Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 10 +++++- .github/workflows/test_in_devenv.yml | 12 +++++-- python/cudaq/_cudaq.cpp | 3 ++ python/tests/backends/test_IonQ.py | 34 +++++-------------- python/tests/backends/test_Quantinuum.py | 18 ++++------ .../test_Quantinuum_LocalEmulation.py | 33 +++++------------- python/utils/TestingUtils.cpp | 3 +- runtime/cudaq.h | 2 ++ runtime/cudaq/cudaq.cpp | 3 ++ runtime/nvqir/CircuitSimulator.h | 4 +++ runtime/nvqir/NVQIR.cpp | 6 ++++ .../custatevec/CuStateVecCircuitSimulator.cu | 17 ++++++---- runtime/nvqir/qpp/QppCircuitSimulator.cpp | 4 +++ unittests/integration/builder_tester.cpp | 4 +++ 14 files changed, 80 insertions(+), 73 deletions(-) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 77fbbdbee1..2498b977f9 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -169,12 +169,20 @@ jobs: image: wheel_validation:local shell: bash run: | - python${{ inputs.python_version }} -m pytest /tmp/tests/ + python${{ inputs.python_version }} -m pytest --ignore /tmp/tests/backends /tmp/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi + for backendTest in /tmp/tests/backends/*.py; do + python${{ inputs.python_version }} -m pytest $backendTest + pytest_status=$? + if [ ! $pytest_status -eq 0 ]; then + echo "pytest $backendTest status = " $pytest_status + exit 1 + fi + done - name: Validate Python examples run: | diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index 2ceb97ec94..e2389a7228 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -120,13 +120,21 @@ jobs: run: | cd $CUDAQ_REPO_ROOT pip install . --user -vvv - python3 -m pytest python/tests/ + python3 -m pytest --ignore python/tests/backends python/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi - + for backendTest in python/tests/backends/*.py; do + python3 -m pytest $backendTest + pytest_status=$? + if [ ! $pytest_status -eq 0 ]; then + echo "pytest $backendTest status = " $pytest_status + exit 1 + fi + done + - name: Save environment id: env_save if: inputs.export_environment diff --git a/python/cudaq/_cudaq.cpp b/python/cudaq/_cudaq.cpp index 1b48452781..a5e14859ad 100644 --- a/python/cudaq/_cudaq.cpp +++ b/python/cudaq/_cudaq.cpp @@ -45,6 +45,9 @@ PYBIND11_MODULE(_pycudaq, mod) { }, ""); + mod.def("set_random_seed", &cudaq::set_random_seed, + "Provide the seed for backend quantum kernel simulation."); + auto mpiSubmodule = mod.def_submodule("mpi"); mpiSubmodule.def( "initialize", []() { cudaq::mpi::initialize(); }, diff --git a/python/tests/backends/test_IonQ.py b/python/tests/backends/test_IonQ.py index a4ec4b7529..b0e0806280 100644 --- a/python/tests/backends/test_IonQ.py +++ b/python/tests/backends/test_IonQ.py @@ -10,38 +10,23 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -try: - from utils.mock_qpu.ionq import startServer -except: - print("Mock qpu not available, skipping IonQ tests.") - # TODO: Once we remove the general skip below, it should go here. - -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) +from utils.mock_qpu.ionq import startServer # Define the port for the mock server port = 62455 - -def assert_close(want, got, tolerance=1.0e-5) -> bool: - return abs(want - got) < tolerance +def assert_close(got) -> bool: + return got < -1.5 and got > -1.9 @pytest.fixture(scope="session", autouse=True) def startUpMockServer(): - # TODO: Support passing credentials via config file os.environ["IONQ_API_KEY"] = "00000000000000000000000000000000" - credsName = "{}/FakeConfig.config".format(os.environ["HOME"]) - f = open(credsName, "w") - f.write("key: {}\nrefresh: {}\ntime: 0".format("hello", "rtoken")) - f.close() # Set the targeted QPU cudaq.set_target( "ionq", - url="http://localhost:{}".format(port), - credentials=credsName, + url="http://localhost:{}".format(port) ) # Launch the Mock Server @@ -51,10 +36,8 @@ def startUpMockServer(): yield "Running the tests." - # Kill the server, remove the file + # Kill the server p.terminate() - os.remove(credsName) - def test_ionq_sample(): # Create the kernel we'd like to execute on IonQ @@ -119,14 +102,13 @@ def test_ionq_observe(): # Run the observe task on IonQ synchronously res = cudaq.observe(kernel, hamiltonian, 0.59) - want_expectation_value = -1.71 - assert assert_close(want_expectation_value, res.expectation_z(), 1e-2) + assert assert_close(res.expectation_z()) # Launch it asynchronously, enters the job into the queue future = cudaq.observe_async(kernel, hamiltonian, 0.59) # Retrieve the results (since we're on a mock server) res = future.get() - assert assert_close(want_expectation_value, res.expectation_z(), 1e-2) + assert assert_close(res.expectation_z()) # Launch the job async, job goes in the queue, and # we're free to dump the future to file @@ -139,7 +121,7 @@ def test_ionq_observe(): # the results from the term job ids. futureReadIn = cudaq.AsyncObserveResult(futureAsString, hamiltonian) res = futureReadIn.get() - assert assert_close(want_expectation_value, res.expectation_z(), 1e-2) + assert assert_close(res.expectation_z()) # leave for gdb debugging diff --git a/python/tests/backends/test_Quantinuum.py b/python/tests/backends/test_Quantinuum.py index d4abb04c27..bd97bc70e4 100644 --- a/python/tests/backends/test_Quantinuum.py +++ b/python/tests/backends/test_Quantinuum.py @@ -10,32 +10,26 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -try: - from utils.mock_qpu.quantinuum import startServer -except: - print("Mock qpu not available, skipping Quantinuum tests.") - # TODO: Once we remove the general skip below, it should go here. +from utils.mock_qpu.quantinuum import startServer -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) # Define the port for the mock server port = 62454 - def assert_close(got) -> bool: - return got < -1.1 and got > -2.2 + return got < -1.5 and got > -1.9 @pytest.fixture(scope="session", autouse=True) def startUpMockServer(): # We need a Fake Credentials Config file - credsName = '{}/FakeConfig.config'.format(os.environ["HOME"]) + credsName = '{}/QuantinuumFakeConfig.config'.format(os.environ["HOME"]) f = open(credsName, 'w') f.write('key: {}\nrefresh: {}\ntime: 0'.format("hello", "rtoken")) f.close() + cudaq.set_random_seed(13) + # Set the targeted QPU cudaq.set_target('quantinuum', url='http://localhost:{}'.format(port), @@ -67,6 +61,7 @@ def test_quantinuum_sample(): # server. In reality you'd probably not want to # do this with the remote job queue. counts = cudaq.sample(kernel) + counts.dump() assert (len(counts) == 2) assert ('00' in counts) assert ('11' in counts) @@ -115,7 +110,6 @@ def test_quantinuum_observe(): # Run the observe task on quantinuum synchronously res = cudaq.observe(kernel, hamiltonian, .59) - want_expectation_value = -1.71 assert assert_close(res.expectation_z()) # Launch it asynchronously, enters the job into the queue diff --git a/python/tests/backends/test_Quantinuum_LocalEmulation.py b/python/tests/backends/test_Quantinuum_LocalEmulation.py index e1ef233d82..4dbfeb455b 100644 --- a/python/tests/backends/test_Quantinuum_LocalEmulation.py +++ b/python/tests/backends/test_Quantinuum_LocalEmulation.py @@ -10,22 +10,9 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -try: - from utils.mock_qpu.quantinuum import startServer -except: - print("Mock qpu not available, skipping Quantinuum tests.") - # TODO: Once we remove the general skip below, it should go here. -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See also https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) - -# Define the port for the mock server -port = 62448 - - -def assert_close(want, got, tolerance=1.e-5) -> bool: - return abs(want - got) < tolerance +def assert_close(got) -> bool: + return got < -1.5 and got > -1.9 @pytest.fixture(scope="session", autouse=True) @@ -39,19 +26,15 @@ def startUpMockServer(): # Set the targeted QPU cudaq.set_target('quantinuum', emulate='true') - # Launch the Mock Server - p = Process(target=startServer, args=(port,)) - p.start() - time.sleep(1) - yield "Running the tests." - # Kill the server, remove the file - p.terminate() + # remove the file os.remove(credsName) def test_quantinuum_sample(): + cudaq.set_random_seed(13) + # Create the kernel we'd like to execute on Quantinuum kernel = cudaq.make_kernel() qubits = kernel.qalloc(2) @@ -81,6 +64,7 @@ def test_quantinuum_sample(): def test_quantinuum_observe(): + cudaq.set_random_seed(13) # Create the parameterized ansatz kernel, theta = cudaq.make_kernel(float) qreg = kernel.qalloc(2) @@ -94,14 +78,13 @@ def test_quantinuum_observe(): # Run the observe task on quantinuum synchronously res = cudaq.observe(kernel, hamiltonian, .59, shots_count=100000) - want_expectation_value = -1.71 - assert assert_close(want_expectation_value, res.expectation_z(), 1e-1) + assert assert_close(res.expectation_z()) # Launch it asynchronously, enters the job into the queue future = cudaq.observe_async(kernel, hamiltonian, .59, shots_count=100000) # Retrieve the results (since we're on a mock server) res = future.get() - assert assert_close(want_expectation_value, res.expectation_z(), 1e-1) + assert assert_close(res.expectation_z()) # leave for gdb debugging diff --git a/python/utils/TestingUtils.cpp b/python/utils/TestingUtils.cpp index ec8cca8c1d..625c5108df 100644 --- a/python/utils/TestingUtils.cpp +++ b/python/utils/TestingUtils.cpp @@ -8,6 +8,7 @@ #include "TestingUtils.h" #include "common/PluginUtils.h" +#include "cudaq.h" #include "cudaq/platform.h" #include "nvqir/CircuitSimulator.h" #include @@ -15,7 +16,6 @@ #include #include #include - namespace py = pybind11; namespace nvqir { @@ -43,6 +43,7 @@ void bindTestUtils(py::module &mod, LinkedLibraryHolder &holder) { "initialize", [&](std::size_t numQubits, std::size_t numShots) { cudaq::ExecutionContext *context = new cudaq::ExecutionContext("sample", numShots); + cudaq::set_random_seed(13); holder.getSimulator("qpp")->setExecutionContext(context); return std::make_tuple( holder.getSimulator("qpp")->allocateQubits(numQubits), context); diff --git a/runtime/cudaq.h b/runtime/cudaq.h index d0f5205282..154f1fef4a 100644 --- a/runtime/cudaq.h +++ b/runtime/cudaq.h @@ -190,6 +190,8 @@ void unset_noise(); /// @brief Utility function for clearing the shots void clear_shots(const std::size_t nShots); +void set_random_seed(std::size_t seed); + namespace mpi { /// @brief Initialize MPI if available. This function diff --git a/runtime/cudaq/cudaq.cpp b/runtime/cudaq/cudaq.cpp index e0c8f3939d..97b63990d6 100644 --- a/runtime/cudaq/cudaq.cpp +++ b/runtime/cudaq/cudaq.cpp @@ -23,6 +23,7 @@ namespace nvqir { void tearDownBeforeMPIFinalize(); +void setRandomSeed(std::size_t); } namespace cudaq::mpi { @@ -267,6 +268,8 @@ void unset_noise() { auto &platform = cudaq::get_platform(); platform.set_noise(nullptr); } + +void set_random_seed(std::size_t seed) { nvqir::setRandomSeed(seed); } } // namespace cudaq namespace cudaq::support { diff --git a/runtime/nvqir/CircuitSimulator.h b/runtime/nvqir/CircuitSimulator.h index b77ddbfa74..dfcdbf2266 100644 --- a/runtime/nvqir/CircuitSimulator.h +++ b/runtime/nvqir/CircuitSimulator.h @@ -58,6 +58,10 @@ class CircuitSimulator { /// simulation strategies that support noise modeling. virtual void setNoiseModel(cudaq::noise_model &noise) = 0; + virtual void setRandomSeed(std::size_t seed) { + // do nothing + } + /// @brief Compute the expected value of the given spin op /// with respect to the current state, . virtual cudaq::ExecutionResult observe(const cudaq::spin_op &term) = 0; diff --git a/runtime/nvqir/NVQIR.cpp b/runtime/nvqir/NVQIR.cpp index 292ceb0327..daa39c70a3 100644 --- a/runtime/nvqir/NVQIR.cpp +++ b/runtime/nvqir/NVQIR.cpp @@ -81,6 +81,10 @@ CircuitSimulator *getCircuitSimulatorInternal() { return simulator; } +void setRandomSeed(std::size_t seed) { + getCircuitSimulatorInternal()->setRandomSeed(seed); +} + thread_local static bool isBaseProfile = false; void toggleBaseProfile() { isBaseProfile = !isBaseProfile; } @@ -364,6 +368,8 @@ Result *__quantum__qis__mz__to__register(Qubit *q, const char *name) { return b ? ResultOne : ResultZero; } +bool __quantum__qis__read_result__body(Result *r) { return false; } + void __quantum__rt__array_start_record_output() {} void __quantum__rt__array_end_record_output() {} void __quantum__rt__result_record_output(Result *, int8_t *) {} diff --git a/runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cu b/runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cu index 8f76eaf50c..9083389275 100644 --- a/runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cu +++ b/runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cu @@ -45,12 +45,12 @@ namespace { /// @param num_samples /// @param max_value /// @return -static std::vector randomValues(uint64_t num_samples, - double max_value) { +static std::vector randomValues(uint64_t num_samples, double max_value, + std::optional seed) { std::vector rs; rs.reserve(num_samples); std::random_device rd; - std::mt19937 rgen(rd()); + std::mt19937 rgen(seed.value_or(rd())); std::uniform_real_distribution distr(0.0, max_value); for (uint64_t i = 0; i < num_samples; ++i) { rs.emplace_back(distr(rgen)); @@ -134,6 +134,9 @@ protected: /// @brief Count the number of resets. int nResets = 0; + /// @brief User may optionally provide a random seed. + std::optional seed = std::nullopt; + custatevecComputeType_t cuStateVecComputeType = CUSTATEVEC_COMPUTE_64F; cudaDataType_t cuStateVecCudaDataType = CUDA_C_64F; @@ -330,13 +333,15 @@ public: /// The destructor virtual ~CuStateVecCircuitSimulator() = default; + void setRandomSeed(std::size_t randomSeed) override { seed = randomSeed; } + /// @brief Measure operation /// @param qubitIdx /// @return bool measureQubit(const std::size_t qubitIdx) override { const int basisBits[] = {(int)qubitIdx}; int parity; - double rand = randomValues(1, 1.0)[0]; + double rand = randomValues(1, 1.0, seed)[0]; HANDLE_ERROR(custatevecMeasureOnZBasis( handle, deviceStateVector, cuStateVecCudaDataType, nQubitsAllocated, &parity, basisBits, /*N Bits*/ 1, rand, @@ -352,7 +357,7 @@ public: nResets++; const int basisBits[] = {(int)qubitIdx}; int parity; - double rand = randomValues(1, 1.0)[0]; + double rand = randomValues(1, 1.0, seed)[0]; HANDLE_ERROR(custatevecMeasureOnZBasis( handle, deviceStateVector, cuStateVecCudaDataType, nQubitsAllocated, &parity, basisBits, /*N Bits*/ 1, rand, @@ -469,7 +474,7 @@ public: } // Grab some random seed values and create the sampler - auto randomValues_ = randomValues(shots, 1.0); + auto randomValues_ = randomValues(shots, 1.0, seed); custatevecSamplerDescriptor_t sampler; HANDLE_ERROR(custatevecSamplerCreate( handle, deviceStateVector, cuStateVecCudaDataType, nQubitsAllocated, diff --git a/runtime/nvqir/qpp/QppCircuitSimulator.cpp b/runtime/nvqir/qpp/QppCircuitSimulator.cpp index 4f811999e9..1f7bb59ae6 100644 --- a/runtime/nvqir/qpp/QppCircuitSimulator.cpp +++ b/runtime/nvqir/qpp/QppCircuitSimulator.cpp @@ -167,6 +167,10 @@ class QppCircuitSimulator : public nvqir::CircuitSimulatorBase { QppCircuitSimulator() = default; virtual ~QppCircuitSimulator() = default; + void setRandomSeed(std::size_t seed) override { + qpp::RandomDevices::get_instance().get_prng().seed(seed); + } + bool canHandleObserve() override { // Do not compute from matrix if shots based sampling requested if (executionContext && diff --git a/unittests/integration/builder_tester.cpp b/unittests/integration/builder_tester.cpp index 26a8ec8924..131db41df4 100644 --- a/unittests/integration/builder_tester.cpp +++ b/unittests/integration/builder_tester.cpp @@ -213,6 +213,8 @@ CUDAQ_TEST(BuilderTester, checkSimple) { } CUDAQ_TEST(BuilderTester, checkSwap) { + cudaq::set_random_seed(13); + // Simple two-qubit swap. { auto kernel = cudaq::make_kernel(); @@ -247,6 +249,7 @@ CUDAQ_TEST(BuilderTester, checkSwap) { } CUDAQ_TEST(BuilderTester, checkConditional) { + cudaq::set_random_seed(13); auto kernel = cudaq::make_kernel(); auto q = kernel.qalloc(2); kernel.h(q[0]); @@ -351,6 +354,7 @@ CUDAQ_TEST(BuilderTester, checkIsArgStdVec) { } CUDAQ_TEST(BuilderTester, checkKernelControl) { + cudaq::set_random_seed(13); auto [xPrep, qubitIn] = cudaq::make_kernel(); xPrep.x(qubitIn); From dbccd8bd7e2d6459016e9489ceeaed13628c067b Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Tue, 25 Jul 2023 00:18:58 +0000 Subject: [PATCH 02/13] add docs Signed-off-by: Alex McCaskey --- runtime/cudaq.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/cudaq.h b/runtime/cudaq.h index 154f1fef4a..928fb7a80b 100644 --- a/runtime/cudaq.h +++ b/runtime/cudaq.h @@ -190,6 +190,8 @@ void unset_noise(); /// @brief Utility function for clearing the shots void clear_shots(const std::size_t nShots); +/// @brief Set a seed for any random number +/// generators used in backend simulations. void set_random_seed(std::size_t seed); namespace mpi { From 7f9505ae79f4803a911f8c8cfb3cdab1671f4abf Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Tue, 25 Jul 2023 12:37:27 +0000 Subject: [PATCH 03/13] whoops, added function declaration to mpi section Signed-off-by: Alex McCaskey --- runtime/cudaq/cudaq.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtime/cudaq/cudaq.cpp b/runtime/cudaq/cudaq.cpp index 97b63990d6..013378ea37 100644 --- a/runtime/cudaq/cudaq.cpp +++ b/runtime/cudaq/cudaq.cpp @@ -193,6 +193,10 @@ bool cudaq::__internal__::isLibraryMode(const std::string &kernelname) { //===----------------------------------------------------------------------===// +namespace nvqir { +extern void setRandomSeed(std::size_t); +} + namespace cudaq { /// @brief Global boolean that disables From 70ef307aae7ce8aad9fee3fd762124e95c964eaf Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Tue, 25 Jul 2023 12:37:43 +0000 Subject: [PATCH 04/13] whoops, added function declaration to mpi section Signed-off-by: Alex McCaskey --- runtime/cudaq/cudaq.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/cudaq/cudaq.cpp b/runtime/cudaq/cudaq.cpp index 013378ea37..6b48b71a1c 100644 --- a/runtime/cudaq/cudaq.cpp +++ b/runtime/cudaq/cudaq.cpp @@ -194,7 +194,7 @@ bool cudaq::__internal__::isLibraryMode(const std::string &kernelname) { //===----------------------------------------------------------------------===// namespace nvqir { -extern void setRandomSeed(std::size_t); +void setRandomSeed(std::size_t); } namespace cudaq { From 6b63baea2f4d1f0a0106178d9b425f522b79bafb Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Wed, 26 Jul 2023 13:23:17 +0000 Subject: [PATCH 05/13] debugging Signed-off-by: Alex McCaskey --- .github/workflows/test_in_devenv.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index e2389a7228..5cb2563531 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -125,9 +125,11 @@ jobs: if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 - fi + fi + apt-get install -y gdb for backendTest in python/tests/backends/*.py; do - python3 -m pytest $backendTest + export PYTHONPATH=$PYTHONPATH:$PWD + gdb -batch -ex "run" -ex "bt" --args python3 -m pytest $backendTest pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest $backendTest status = " $pytest_status From 1180861f6c69af8c189622686a6a6a2735df9de7 Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Wed, 26 Jul 2023 13:50:57 +0000 Subject: [PATCH 06/13] try to update first Signed-off-by: Alex McCaskey --- .github/workflows/test_in_devenv.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index 5cb2563531..b6cfa0b97b 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -126,7 +126,7 @@ jobs: echo "pytest status = " $pytest_status exit 1 fi - apt-get install -y gdb + apt-get update && apt-get install -y gdb for backendTest in python/tests/backends/*.py; do export PYTHONPATH=$PYTHONPATH:$PWD gdb -batch -ex "run" -ex "bt" --args python3 -m pytest $backendTest From 146ebe394f16722580b20e5a5650c4b2f66a712f Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Wed, 26 Jul 2023 14:07:25 +0000 Subject: [PATCH 07/13] set the pythonpath correctly Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 3 +++ .github/workflows/test_in_devenv.yml | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 2498b977f9..4ad51acecd 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -175,6 +175,9 @@ jobs: echo "pytest status = " $pytest_status exit 1 fi + apt-get update && apt-get install -y git + git clone https://github.com/nvidia/cuda-quantum + export PYTHONPATH=$PYTHONPATH:$PWD/cuda-quantum for backendTest in /tmp/tests/backends/*.py; do python${{ inputs.python_version }} -m pytest $backendTest pytest_status=$? diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index b6cfa0b97b..a4913f1ea7 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -126,10 +126,9 @@ jobs: echo "pytest status = " $pytest_status exit 1 fi - apt-get update && apt-get install -y gdb for backendTest in python/tests/backends/*.py; do export PYTHONPATH=$PYTHONPATH:$PWD - gdb -batch -ex "run" -ex "bt" --args python3 -m pytest $backendTest + python3 -m pytest $backendTest pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest $backendTest status = " $pytest_status From a9c08f0cda849e35afeb997bff4ef503094c0e49 Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Thu, 27 Jul 2023 17:45:44 +0000 Subject: [PATCH 08/13] kick the seg fault can down the road Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 13 +------------ .github/workflows/test_in_devenv.yml | 11 +---------- python/tests/backends/test_IonQ.py | 10 +++++++++- python/tests/backends/test_Quantinuum.py | 11 +++++++++-- .../backends/test_Quantinuum_LocalEmulation.py | 4 ++++ 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 4ad51acecd..77fbbdbee1 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -169,23 +169,12 @@ jobs: image: wheel_validation:local shell: bash run: | - python${{ inputs.python_version }} -m pytest --ignore /tmp/tests/backends /tmp/tests/ + python${{ inputs.python_version }} -m pytest /tmp/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi - apt-get update && apt-get install -y git - git clone https://github.com/nvidia/cuda-quantum - export PYTHONPATH=$PYTHONPATH:$PWD/cuda-quantum - for backendTest in /tmp/tests/backends/*.py; do - python${{ inputs.python_version }} -m pytest $backendTest - pytest_status=$? - if [ ! $pytest_status -eq 0 ]; then - echo "pytest $backendTest status = " $pytest_status - exit 1 - fi - done - name: Validate Python examples run: | diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index a4913f1ea7..36ce88c5a1 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -120,21 +120,12 @@ jobs: run: | cd $CUDAQ_REPO_ROOT pip install . --user -vvv - python3 -m pytest --ignore python/tests/backends python/tests/ + python3 -m pytest python/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi - for backendTest in python/tests/backends/*.py; do - export PYTHONPATH=$PYTHONPATH:$PWD - python3 -m pytest $backendTest - pytest_status=$? - if [ ! $pytest_status -eq 0 ]; then - echo "pytest $backendTest status = " $pytest_status - exit 1 - fi - done - name: Save environment id: env_save diff --git a/python/tests/backends/test_IonQ.py b/python/tests/backends/test_IonQ.py index b0e0806280..516287c0d9 100644 --- a/python/tests/backends/test_IonQ.py +++ b/python/tests/backends/test_IonQ.py @@ -10,7 +10,15 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -from utils.mock_qpu.ionq import startServer +try: + from utils.mock_qpu.ionq import startServer +except: + print("Mock qpu not available, skipping IonQ tests.") + # TODO: Once we remove the general skip below, it should go here. + +pytest.skip( + "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", + allow_module_level=True) # Define the port for the mock server port = 62455 diff --git a/python/tests/backends/test_Quantinuum.py b/python/tests/backends/test_Quantinuum.py index bd97bc70e4..912e687653 100644 --- a/python/tests/backends/test_Quantinuum.py +++ b/python/tests/backends/test_Quantinuum.py @@ -10,8 +10,15 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -from utils.mock_qpu.quantinuum import startServer - +try: + from utils.mock_qpu.quantinuum import startServer +except: + print("Mock qpu not available, skipping Quantinuum tests.") + # TODO: Once we remove the general skip below, it should go here. + +pytest.skip( + "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", + allow_module_level=True) # Define the port for the mock server port = 62454 diff --git a/python/tests/backends/test_Quantinuum_LocalEmulation.py b/python/tests/backends/test_Quantinuum_LocalEmulation.py index 4dbfeb455b..fa2431a673 100644 --- a/python/tests/backends/test_Quantinuum_LocalEmulation.py +++ b/python/tests/backends/test_Quantinuum_LocalEmulation.py @@ -11,6 +11,10 @@ from cudaq import spin from multiprocessing import Process +pytest.skip( + "This file produces a segmentation fault on the CI but not locally. See also https://github.com/NVIDIA/cuda-quantum/issues/303.", + allow_module_level=True) + def assert_close(got) -> bool: return got < -1.5 and got > -1.9 From d548c01cd840533ff8d7934a6bc434a5e1b7d51f Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Thu, 27 Jul 2023 19:48:40 +0000 Subject: [PATCH 09/13] turn the py tests back on Signed-off-by: Alex McCaskey --- python/tests/backends/test_IonQ.py | 10 +--------- python/tests/backends/test_Quantinuum.py | 10 +--------- .../tests/backends/test_Quantinuum_LocalEmulation.py | 3 --- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/python/tests/backends/test_IonQ.py b/python/tests/backends/test_IonQ.py index 516287c0d9..b0e0806280 100644 --- a/python/tests/backends/test_IonQ.py +++ b/python/tests/backends/test_IonQ.py @@ -10,15 +10,7 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -try: - from utils.mock_qpu.ionq import startServer -except: - print("Mock qpu not available, skipping IonQ tests.") - # TODO: Once we remove the general skip below, it should go here. - -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) +from utils.mock_qpu.ionq import startServer # Define the port for the mock server port = 62455 diff --git a/python/tests/backends/test_Quantinuum.py b/python/tests/backends/test_Quantinuum.py index 912e687653..3a174e3655 100644 --- a/python/tests/backends/test_Quantinuum.py +++ b/python/tests/backends/test_Quantinuum.py @@ -10,15 +10,7 @@ import cudaq, pytest, os, time from cudaq import spin from multiprocessing import Process -try: - from utils.mock_qpu.quantinuum import startServer -except: - print("Mock qpu not available, skipping Quantinuum tests.") - # TODO: Once we remove the general skip below, it should go here. - -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) +from utils.mock_qpu.quantinuum import startServer # Define the port for the mock server port = 62454 diff --git a/python/tests/backends/test_Quantinuum_LocalEmulation.py b/python/tests/backends/test_Quantinuum_LocalEmulation.py index fa2431a673..4d654e96d8 100644 --- a/python/tests/backends/test_Quantinuum_LocalEmulation.py +++ b/python/tests/backends/test_Quantinuum_LocalEmulation.py @@ -11,9 +11,6 @@ from cudaq import spin from multiprocessing import Process -pytest.skip( - "This file produces a segmentation fault on the CI but not locally. See also https://github.com/NVIDIA/cuda-quantum/issues/303.", - allow_module_level=True) def assert_close(got) -> bool: return got < -1.5 and got > -1.9 From 75f187dc06593893bd855d921f43daf0e5dec9dc Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Thu, 27 Jul 2023 19:54:15 +0000 Subject: [PATCH 10/13] turn the tests on in ci Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 13 ++++++++++++- .github/workflows/test_in_devenv.yml | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 77fbbdbee1..4ad51acecd 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -169,12 +169,23 @@ jobs: image: wheel_validation:local shell: bash run: | - python${{ inputs.python_version }} -m pytest /tmp/tests/ + python${{ inputs.python_version }} -m pytest --ignore /tmp/tests/backends /tmp/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi + apt-get update && apt-get install -y git + git clone https://github.com/nvidia/cuda-quantum + export PYTHONPATH=$PYTHONPATH:$PWD/cuda-quantum + for backendTest in /tmp/tests/backends/*.py; do + python${{ inputs.python_version }} -m pytest $backendTest + pytest_status=$? + if [ ! $pytest_status -eq 0 ]; then + echo "pytest $backendTest status = " $pytest_status + exit 1 + fi + done - name: Validate Python examples run: | diff --git a/.github/workflows/test_in_devenv.yml b/.github/workflows/test_in_devenv.yml index 36ce88c5a1..07156b2eb5 100644 --- a/.github/workflows/test_in_devenv.yml +++ b/.github/workflows/test_in_devenv.yml @@ -120,12 +120,20 @@ jobs: run: | cd $CUDAQ_REPO_ROOT pip install . --user -vvv - python3 -m pytest python/tests/ + python3 -m pytest --ignore python/tests/backends python/tests/ pytest_status=$? if [ ! $pytest_status -eq 0 ]; then echo "pytest status = " $pytest_status exit 1 fi + for backendTest in python/tests/backends/*.py; do + python3 -m pytest $backendTest + pytest_status=$? + if [ ! $pytest_status -eq 0 ]; then + echo "pytest $backendTest status = " $pytest_status + exit 1 + fi + done - name: Save environment id: env_save From 53d87cc8f12a0c2fce94954370353c72c38b297e Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Thu, 27 Jul 2023 19:54:38 +0000 Subject: [PATCH 11/13] clang format Signed-off-by: Alex McCaskey --- runtime/cudaq/cudaq.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/cudaq/cudaq.cpp b/runtime/cudaq/cudaq.cpp index 6b48b71a1c..9937919440 100644 --- a/runtime/cudaq/cudaq.cpp +++ b/runtime/cudaq/cudaq.cpp @@ -24,7 +24,7 @@ namespace nvqir { void tearDownBeforeMPIFinalize(); void setRandomSeed(std::size_t); -} +} // namespace nvqir namespace cudaq::mpi { From dab90ee4af6d6bc44cd117516307d2e4f6fe8760 Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Thu, 27 Jul 2023 22:35:00 +0000 Subject: [PATCH 12/13] finaggle with the CI Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 4ad51acecd..7909f07782 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -177,7 +177,7 @@ jobs: fi apt-get update && apt-get install -y git git clone https://github.com/nvidia/cuda-quantum - export PYTHONPATH=$PYTHONPATH:$PWD/cuda-quantum + cd cuda-quantum for backendTest in /tmp/tests/backends/*.py; do python${{ inputs.python_version }} -m pytest $backendTest pytest_status=$? From 74c18c75364cc69c60e878f288a86812fcaa2ddf Mon Sep 17 00:00:00 2001 From: Alex McCaskey Date: Fri, 28 Jul 2023 13:56:48 +0000 Subject: [PATCH 13/13] add some installs Signed-off-by: Alex McCaskey --- .github/workflows/python_wheels.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python_wheels.yml b/.github/workflows/python_wheels.yml index 7909f07782..7944cb6a10 100644 --- a/.github/workflows/python_wheels.yml +++ b/.github/workflows/python_wheels.yml @@ -178,6 +178,7 @@ jobs: apt-get update && apt-get install -y git git clone https://github.com/nvidia/cuda-quantum cd cuda-quantum + python${{ inputs.python_version }} -m pip install --user fastapi uvicorn llvmlite for backendTest in /tmp/tests/backends/*.py; do python${{ inputs.python_version }} -m pytest $backendTest pytest_status=$?