forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvolveResult.h
More file actions
94 lines (78 loc) · 3.54 KB
/
EvolveResult.h
File metadata and controls
94 lines (78 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2025 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. *
******************************************************************************/
#pragma once
#include "common/ObserveResult.h"
#include "cudaq/qis/state.h"
#include <memory>
#include <optional>
namespace cudaq {
/// @brief The evolve_result encapsulates all data generated from a
/// cudaq"::"evolve call. This includes information about the state
/// and any computed expectation values during and after evolution,
/// depending on the arguments passed to the call.
class evolve_result {
public:
// The state of the system. If only final state is retained, this vector will
// have exactly one element.
std::optional<std::vector<state>> states = std::nullopt;
// The computed expectation values. If only final expectation values are
// retained, this vector will have exactly one element.
std::optional<std::vector<std::vector<observe_result>>> expectation_values =
std::nullopt;
// The result of sampling of an analog Hamiltonian simulation on a QPU
std::optional<sample_result> sampling_result = std::nullopt;
// Construct from single final state.
evolve_result(state state) {}
// Construct from single final observe result.
evolve_result(state state, const std::vector<observe_result> &expectations) {
if (!expectation_values.has_value()) {
expectation_values =
std::make_optional<std::vector<std::vector<observe_result>>>();
}
expectation_values->emplace_back(expectations);
}
evolve_result(state state, const std::vector<double> &expectations) {
std::vector<observe_result> result;
const spin_op emptyOp(
std::unordered_map<spin_op::spin_op_term, std::complex<double>>{});
for (auto e : expectations) {
result.push_back(observe_result(e, emptyOp));
}
if (!expectation_values.has_value()) {
expectation_values =
std::make_optional<std::vector<std::vector<observe_result>>>();
}
expectation_values->emplace_back(result);
}
// Construct from system states.
evolve_result(const std::vector<state> &states) {}
// Construct from intermediate system states and observe results.
evolve_result(const std::vector<state> &states,
const std::vector<std::vector<observe_result>> &expectations)
: states(std::make_optional<std::vector<state>>(states)),
expectation_values(
std::make_optional<std::vector<std::vector<observe_result>>>(
expectations)) {}
evolve_result(const std::vector<state> &states,
const std::vector<std::vector<double>> &expectations)
: states(std::make_optional<std::vector<state>>(states)) {
std::vector<std::vector<observe_result>> result;
const spin_op emptyOp(
std::unordered_map<spin_op::spin_op_term, std::complex<double>>{});
for (const auto &vec : expectations) {
std::vector<observe_result> subResult;
for (auto e : vec) {
subResult.push_back(observe_result(e, emptyOp));
}
result.push_back(subResult);
}
expectation_values = result;
}
evolve_result(const sample_result &sr) : sampling_result(sr) {}
};
} // namespace cudaq