Skip to content

Commit

Permalink
Fix issue NVIDIA#2382.
Browse files Browse the repository at this point in the history
Adds a check to see if the execution context is observe to avoid the
bug mentioned in issue NVIDIA#2382. The issue mentions "bugs", so there may
be others.

Add the test case provided.

Signed-off-by: Eric Schweitz <[email protected]>
  • Loading branch information
schweitzpgi committed Nov 19, 2024
1 parent 582094f commit c3ae059
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion runtime/common/BaseRemoteRESTQPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ class BaseRemoteRESTQPU : public cudaq::QPU {
}
}

bool isObserve =
executionContext && executionContext->name == "observe";
for (std::size_t i = 0; i < codes.size(); i++) {
cudaq::ExecutionContext context("sample", localShots);
context.reorderIdx = reorderIdx;
Expand All @@ -688,7 +690,7 @@ class BaseRemoteRESTQPU : public cudaq::QPU {

// If there are multiple codes, this is likely a spin_op.
// If so, use the code name instead of the global register.
if (codes.size() > 1) {
if (isObserve || (codes.size() > 1)) {
results.emplace_back(context.result.to_map(), codes[i].name);
results.back().sequentialData =
context.result.sequential_data();
Expand Down
47 changes: 47 additions & 0 deletions targettests/execution/observe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

// RUN: nvq++ %cpp_std --target oqc --emulate %s -o %t && %t | FileCheck %s

#include <cudaq.h>
#include <cudaq/algorithm.h>

// The example here shows a simple use case for the `cudaq::observe`
// function in computing expected values of provided spin_ops.

struct ansatz {
auto operator()(double theta) __qpu__ {
cudaq::qvector q(2);
x(q[0]);
ry(theta, q[1]);
x<cudaq::ctrl>(q[1], q[0]);
}
};

int main() {
// Build up your spin op algebraically
using namespace cudaq::spin;
cudaq::spin_op h = i(0) * z(1); // <-- MAKE THIS CHANGE HERE
// cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) +
// .21829 * z(0) - 6.125 * z(1);

// Make repeatable for shots-based emulation
cudaq::set_random_seed(13);

// Observe takes the kernel, the spin_op, and the concrete
// parameters for the kernel
double energy = cudaq::observe(ansatz{}, h, .59);
printf("Attention.\n");
printf("Energy is %.16lf\n", energy);
printf("At ease.\n");
return 0;
}

// CHECK-LABEL: Attention
// CHECK-NOT: Energy is 0.000000
// CHECK-LABEL: At ease

0 comments on commit c3ae059

Please sign in to comment.