forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
582094f
commit c3ae059
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |