Skip to content

Commit

Permalink
Add output function stubs to NVQIR.cpp. (#2561)
Browse files Browse the repository at this point in the history
* Add output function stubs to NVQIR.cpp.

These need to output to something other than std::cout. Using
std::cout temporarily...

Signed-off-by: Eric Schweitz <[email protected]>

* Replace cout with an outputLog string in the ExecutionContext.

Signed-off-by: Eric Schweitz <[email protected]>

* Turn into a template to avoid copies of code.

Signed-off-by: Eric Schweitz <[email protected]>

---------

Signed-off-by: Eric Schweitz <[email protected]>
  • Loading branch information
schweitzpgi authored Jan 31, 2025
1 parent fbc74e2 commit d53ced2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions runtime/common/ExecutionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class ExecutionContext {
/// calculation on simulation backends that support trajectory simulation.
std::optional<std::size_t> numberTrajectories;

/// A string containing the output logging of a kernel launched with
/// cudaq::run.
std::string outputLog;

/// @brief The Constructor, takes the name of the context
/// @param n The name of the context
ExecutionContext(const std::string n) : name(n) {}
Expand Down
35 changes: 34 additions & 1 deletion runtime/nvqir/NVQIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "cudaq/spin_op.h"
#include <cmath>
#include <complex>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -208,6 +209,18 @@ std::unique_ptr<std::complex<To>[]> convertToComplex(From *data,

using namespace nvqir;

template <typename VAL>
void quantumRTGenericRecordOutput(const char *type, VAL val,
const char *label) {
auto *circuitSimulator = nvqir::getCircuitSimulatorInternal();
auto *currentContext = circuitSimulator->getExecutionContext();
std::stringstream ss{currentContext->outputLog, std::ios::app};
ss << "OUTPUT\t" << type << "\t" << val << '\t';
if (label)
ss << label;
ss << '\n';
}

extern "C" {

void print_i64(const char *msg, std::size_t i) { printf(msg, i); }
Expand Down Expand Up @@ -404,6 +417,26 @@ void __quantum__rt__deallocate_all(const std::size_t numQubits,
nvqir::getCircuitSimulatorInternal()->deallocateQubits(qubits);
}

void __quantum__rt__bool_record_output(bool val, const char *label) {
quantumRTGenericRecordOutput("BOOL", (val ? "true" : "false"), label);
}

void __quantum__rt__integer_record_output(std::int64_t val, const char *label) {
quantumRTGenericRecordOutput("INT", val, label);
}

void __quantum__rt__double_record_output(double val, const char *label) {
quantumRTGenericRecordOutput("DOUBLE", val, label);
}

void __quantum__rt__tuple_record_output(std::uint64_t len, const char *label) {
quantumRTGenericRecordOutput("TUPLE", len, label);
}

void __quantum__rt__array_record_output(std::uint64_t len, const char *label) {
quantumRTGenericRecordOutput("ARRAY", len, label);
}

#define ONE_QUBIT_QIS_FUNCTION(GATENAME) \
void QIS_FUNCTION_NAME(GATENAME)(Qubit * qubit) { \
auto targetIdx = qubitToSizeT(qubit); \
Expand Down Expand Up @@ -679,7 +712,7 @@ Result *__quantum__qis__measure__body(Array *pauli_arr, Array *qubits) {
ScopedTraceWithContext("NVQIR::observe_measure_body");

auto *circuitSimulator = nvqir::getCircuitSimulatorInternal();
auto currentContext = circuitSimulator->getExecutionContext();
auto *currentContext = circuitSimulator->getExecutionContext();

// Some backends may better handle the observe task.
// Let's give them that opportunity.
Expand Down

0 comments on commit d53ced2

Please sign in to comment.