diff --git a/python/runtime/cudaq/algorithms/py_draw.cpp b/python/runtime/cudaq/algorithms/py_draw.cpp index a12b492c726..5ab546d8a0a 100644 --- a/python/runtime/cudaq/algorithms/py_draw.cpp +++ b/python/runtime/cudaq/algorithms/py_draw.cpp @@ -125,7 +125,9 @@ path, i.e., the trace, of the provided `kernel`. # Output # ╭───╮╭──────────╮ # q0 : ┤ h ├┤ ry(0.59) ├ - # ╰───╯╰──────────╯)#"); + # ╰───╯╰──────────╯ + +Note: This function is only available when using simulator backends.)#"); } } // namespace cudaq diff --git a/python/tests/backends/test_Quantinuum_kernel.py b/python/tests/backends/test_Quantinuum_kernel.py index 5ad31be69f6..8b2fbe84d1e 100644 --- a/python/tests/backends/test_Quantinuum_kernel.py +++ b/python/tests/backends/test_Quantinuum_kernel.py @@ -244,6 +244,20 @@ def test(): assert not '10' in counts +def test_draw(): + + @cudaq.kernel + def kernel(): + q = cudaq.qvector(2) + h(q[0]) + x.ctrl(q[0], q[1]) + mz(q) + + # Test here is that this does not raise an exception + result = cudaq.draw(kernel) + assert result is '' + + # leave for gdb debugging if __name__ == "__main__": loc = os.path.abspath(__file__) diff --git a/runtime/cudaq/algorithms/draw.h b/runtime/cudaq/algorithms/draw.h index b119ebd07da..f62d9f5555e 100644 --- a/runtime/cudaq/algorithms/draw.h +++ b/runtime/cudaq/algorithms/draw.h @@ -8,10 +8,10 @@ #pragma once -#include - #include "common/ExecutionContext.h" #include "cudaq/platform.h" +#include +#include namespace cudaq { @@ -32,9 +32,13 @@ cudaq::Trace traceFromKernel(KernelFunctor &&kernel, Args &&...args) { // Get the platform. auto &platform = cudaq::get_platform(); - // This can only be done in simulation - if (!platform.is_simulator()) - throw std::runtime_error("Cannot use draw on a physical QPU."); + // This is not supported on hardware backends, but we don't want callers to + // crash on unhandled exceptions. + if (!platform.is_simulator()) { + std::cerr << "Warning: `draw` can only be used with a simulator platform. " + << "Returning an empty trace." << std::endl; + return Trace(); + } // Create an execution context, indicate this is for tracing the execution // path @@ -107,6 +111,7 @@ std::string extractTraceLatex(KernelFunctor &&kernel) { /// */ /// \endcode /// +/// @note This function is only available when using simulator backends. // clang-format on #if CUDAQ_USE_STD20