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
126 lines (105 loc) · 4.57 KB
/
EvolveResult.h
File metadata and controls
126 lines (105 loc) · 4.57 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/****************************************************************-*- 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 {
protected:
// The final state after the time evolution.
state final_state;
// The state after each step in the evolution, if computed.
std::optional<std::vector<state>> intermediate_states = {};
// The computed expectation values at the end of the evolution.
std::optional<std::vector<observe_result>> final_expectation_values = {};
// The computed expectation values for each step of the evolution.
std::optional<std::vector<std::vector<observe_result>>> expectation_values =
{};
// The result of sampling of an analog Hamiltonian simulation on a QPU
std::optional<sample_result> sampling_result = {};
public:
evolve_result(state state) : final_state(state) {}
evolve_result(state state, const std::vector<observe_result> &expectations)
: final_state(state),
final_expectation_values(
std::make_optional<std::vector<observe_result>>(expectations)) {}
evolve_result(state state, const std::vector<double> &expectations)
: final_state(state) {
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));
}
final_expectation_values = result;
}
evolve_result(const std::vector<state> &states)
: final_state(getLastStateIfValid(states)),
intermediate_states(std::make_optional<std::vector<state>>(states)) {}
evolve_result(const std::vector<state> &states,
const std::vector<std::vector<observe_result>> &expectations)
: final_state(getLastStateIfValid(states)),
intermediate_states(std::make_optional<std::vector<state>>(states)),
final_expectation_values(
std::make_optional<std::vector<observe_result>>(
expectations.back())),
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)
: final_state(getLastStateIfValid(states)),
intermediate_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);
}
final_expectation_values = result.back();
expectation_values = result;
}
evolve_result(const sample_result &sr)
: final_state(nullptr), sampling_result(sr) {}
state get_final_state() {
/// TODO: Check for valid state
// if (final_state){
// throw std::runtime_error("No state available for this result");
// }
return final_state;
}
std::optional<std::vector<state>> get_intermediate_states() {
return intermediate_states;
}
std::optional<std::vector<observe_result>> get_final_expectation_values() {
return final_expectation_values;
}
const std::optional<std::vector<std::vector<observe_result>>> &
get_expectation_values() const {
return expectation_values;
}
std::optional<sample_result> get_sampling_result() { return sampling_result; }
private:
state getLastStateIfValid(const std::vector<state> &states) {
if (states.empty())
throw std::runtime_error(
"Cannot create evolve_result with an empty list of states.");
return states.back();
}
};
} // namespace cudaq