Skip to content

Commit f57cb37

Browse files
* Throw a warning for use of unsupported draw API on hardware targets.
Signed-off-by: Pradnya Khalate <[email protected]>
1 parent 5fad63e commit f57cb37

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

python/runtime/cudaq/algorithms/py_draw.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ path, i.e., the trace, of the provided `kernel`.
122122
# Output
123123
# ╭───╮╭──────────╮
124124
# q0 : ┤ h ├┤ ry(0.59) ├
125-
# ╰───╯╰──────────╯)#");
125+
# ╰───╯╰──────────╯
126+
127+
Note: This function is only available when using simulator backends.)#");
126128
}
127129

128130
} // namespace cudaq

python/tests/backends/test_Quantinuum_kernel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ def test():
243243
assert not '10' in counts
244244

245245

246+
def test_draw():
247+
248+
@cudaq.kernel
249+
def kernel():
250+
q = cudaq.qvector(2)
251+
h(q[0])
252+
x.ctrl(q[0], q[1])
253+
mz(q)
254+
255+
# Test here is that this does not raise an exception
256+
result = cudaq.draw(kernel)
257+
assert result is ''
258+
259+
246260
# leave for gdb debugging
247261
if __name__ == "__main__":
248262
loc = os.path.abspath(__file__)

runtime/cudaq/algorithms/draw.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#pragma once
1010

11-
#include <concepts>
12-
1311
#include "common/ExecutionContext.h"
1412
#include "cudaq/platform.h"
13+
#include <concepts>
14+
#include <iostream>
1515

1616
namespace cudaq {
1717

@@ -32,9 +32,13 @@ cudaq::Trace traceFromKernel(KernelFunctor &&kernel, Args &&...args) {
3232
// Get the platform.
3333
auto &platform = cudaq::get_platform();
3434

35-
// This can only be done in simulation
36-
if (!platform.is_simulator())
37-
throw std::runtime_error("Cannot use draw on a physical QPU.");
35+
// This is not supported on hardware backends, but we don't want callers to
36+
// crash on unhandled exceptions.
37+
if (!platform.is_simulator()) {
38+
std::cerr << "Warning: `draw` can only be used with a simulator platform. "
39+
<< "Returning an empty trace." << std::endl;
40+
return Trace();
41+
}
3842

3943
// Create an execution context, indicate this is for tracing the execution
4044
// path
@@ -107,6 +111,7 @@ std::string extractTraceLatex(KernelFunctor &&kernel) {
107111
/// */
108112
/// \endcode
109113
///
114+
/// @note This function is only available when using simulator backends.
110115
// clang-format on
111116

112117
#if CUDAQ_USE_STD20

0 commit comments

Comments
 (0)