Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python/runtime/cudaq/algorithms/py_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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
14 changes: 14 additions & 0 deletions python/tests/backends/test_Quantinuum_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,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__)
Expand Down
15 changes: 10 additions & 5 deletions runtime/cudaq/algorithms/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#pragma once

#include <concepts>

#include "common/ExecutionContext.h"
#include "cudaq/platform.h"
#include <concepts>
#include <iostream>

namespace cudaq {

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading