Skip to content

Commit 94be11b

Browse files
committed
some clean up/fixes in operators.h
Signed-off-by: Bettina Heim <[email protected]>
1 parent b74a9a6 commit 94be11b

File tree

4 files changed

+221
-269
lines changed

4 files changed

+221
-269
lines changed

runtime/cudaq/operator/elementary_operator.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ namespace cudaq {
1818
elementary_operator::elementary_operator(std::string operator_id,
1919
std::vector<int> degrees)
2020
: id(operator_id), degrees(degrees) {}
21-
elementary_operator::elementary_operator(const elementary_operator &other)
22-
: m_ops(other.m_ops), expected_dimensions(other.expected_dimensions),
23-
degrees(other.degrees), id(other.id) {}
24-
elementary_operator::elementary_operator(elementary_operator &other)
25-
: m_ops(other.m_ops), expected_dimensions(other.expected_dimensions),
26-
degrees(other.degrees), id(other.id) {}
2721

2822
elementary_operator elementary_operator::identity(int degree) {
2923
std::string op_id = "identity";
3024
std::vector<int> degrees = {degree};
3125
auto op = elementary_operator(op_id, degrees);
3226
// A dimension of -1 indicates this operator can act on any dimension.
3327
op.expected_dimensions[degree] = -1;
34-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
28+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
3529
auto func = [&](std::map<int, int> dimensions,
3630
std::map<std::string, std::complex<double>> _none) {
3731
int degree = op.degrees[0];
@@ -57,7 +51,7 @@ elementary_operator elementary_operator::zero(int degree) {
5751
auto op = elementary_operator(op_id, degrees);
5852
// A dimension of -1 indicates this operator can act on any dimension.
5953
op.expected_dimensions[degree] = -1;
60-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
54+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
6155
auto func = [&](std::map<int, int> dimensions,
6256
std::map<std::string, std::complex<double>> _none) {
6357
// Need to set the degree via the op itself because the
@@ -83,7 +77,7 @@ elementary_operator elementary_operator::annihilate(int degree) {
8377
auto op = elementary_operator(op_id, degrees);
8478
// A dimension of -1 indicates this operator can act on any dimension.
8579
op.expected_dimensions[degree] = -1;
86-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
80+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
8781
auto func = [&](std::map<int, int> dimensions,
8882
std::map<std::string, std::complex<double>> _none) {
8983
auto degree = op.degrees[0];
@@ -108,7 +102,7 @@ elementary_operator elementary_operator::create(int degree) {
108102
auto op = elementary_operator(op_id, degrees);
109103
// A dimension of -1 indicates this operator can act on any dimension.
110104
op.expected_dimensions[degree] = -1;
111-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
105+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
112106
auto func = [&](std::map<int, int> dimensions,
113107
std::map<std::string, std::complex<double>> _none) {
114108
auto degree = op.degrees[0];
@@ -133,7 +127,7 @@ elementary_operator elementary_operator::position(int degree) {
133127
auto op = elementary_operator(op_id, degrees);
134128
// A dimension of -1 indicates this operator can act on any dimension.
135129
op.expected_dimensions[degree] = -1;
136-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
130+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
137131
auto func = [&](std::map<int, int> dimensions,
138132
std::map<std::string, std::complex<double>> _none) {
139133
auto degree = op.degrees[0];
@@ -160,7 +154,7 @@ elementary_operator elementary_operator::momentum(int degree) {
160154
auto op = elementary_operator(op_id, degrees);
161155
// A dimension of -1 indicates this operator can act on any dimension.
162156
op.expected_dimensions[degree] = -1;
163-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
157+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
164158
auto func = [&](std::map<int, int> dimensions,
165159
std::map<std::string, std::complex<double>> _none) {
166160
auto degree = op.degrees[0];
@@ -189,7 +183,7 @@ elementary_operator elementary_operator::number(int degree) {
189183
auto op = elementary_operator(op_id, degrees);
190184
// A dimension of -1 indicates this operator can act on any dimension.
191185
op.expected_dimensions[degree] = -1;
192-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
186+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
193187
auto func = [&](std::map<int, int> dimensions,
194188
std::map<std::string, std::complex<double>> _none) {
195189
auto degree = op.degrees[0];
@@ -214,7 +208,7 @@ elementary_operator elementary_operator::parity(int degree) {
214208
auto op = elementary_operator(op_id, degrees);
215209
// A dimension of -1 indicates this operator can act on any dimension.
216210
op.expected_dimensions[degree] = -1;
217-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
211+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
218212
auto func = [&](std::map<int, int> dimensions,
219213
std::map<std::string, std::complex<double>> _none) {
220214
auto degree = op.degrees[0];
@@ -240,7 +234,7 @@ elementary_operator::displace(int degree, std::complex<double> amplitude) {
240234
auto op = elementary_operator(op_id, degrees);
241235
// A dimension of -1 indicates this operator can act on any dimension.
242236
op.expected_dimensions[degree] = -1;
243-
if (op.m_ops.find(op_id) == op.m_ops.end()) {
237+
if (elementary_operator::m_ops.find(op_id) == elementary_operator::m_ops.end()) {
244238
auto func = [&](std::map<int, int> dimensions,
245239
std::map<std::string, std::complex<double>> _none) {
246240
auto degree = op.degrees[0];
@@ -277,11 +271,11 @@ elementary_operator::squeeze(int degree, std::complex<double> amplitude) {
277271
complex_matrix elementary_operator::to_matrix(
278272
const std::map<int, int> dimensions,
279273
const std::map<std::string, std::complex<double>> parameters) const {
280-
if (m_ops.find(id) == m_ops.end())
274+
if (elementary_operator::m_ops.find(id) == elementary_operator::m_ops.end())
281275
throw std::runtime_error(
282276
fmt::format("No operator found with this ID '{}'", id));
283277

284-
return m_ops.at(id).generator(dimensions, parameters);
278+
return elementary_operator::m_ops.at(id).generator(dimensions, parameters);
285279
}
286280

287281
template <typename TEval>

runtime/cudaq/operator/matrix_arithmetics.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ matrix_arithmetics::canonicalize(const cudaq::complex_matrix &op_matrix,
104104
}
105105

106106
Evaluated matrix_arithmetics::evaluate(const Operator &op) const {
107-
// cudaq::complex_matrix matrix = op.to_matrix(dimensions, op.parameters);
108-
// // Replace with degrees from operator
109-
// std::vector<int> degrees = op.degrees;
110-
// return Evaluated(degrees, matrix);
111-
112107
return std::visit(
113108
[this](auto &&arg) -> Evaluated {
114109
using T = std::decay_t<decltype(arg)>;
@@ -118,17 +113,11 @@ Evaluated matrix_arithmetics::evaluate(const Operator &op) const {
118113
std::vector<int> degrees = arg.degrees;
119114
return Evaluated(degrees, matrix);
120115
} else if constexpr (std::is_same_v<T, cudaq::scalar_operator>) {
121-
// Handle the scalar operator
122-
if (arg.has_val()) {
123-
// double scalar_value = arg.get_val();
124-
int dim = dimensions.at(arg.degrees[0]);
125-
cudaq::complex_matrix matrix =
126-
cudaq::complex_matrix::identity(dim, dim);
127-
return Evaluated(arg.degrees, matrix);
128-
} else {
129-
throw std::runtime_error(
130-
"Dynamic scalar operator evaluation not implemented.");
131-
}
116+
// FIXME: this is not correct!
117+
int dim = dimensions.at(arg.degrees[0]);
118+
cudaq::complex_matrix matrix =
119+
cudaq::complex_matrix::identity(dim, dim);
120+
return Evaluated(arg.degrees, matrix);
132121
} else {
133122
throw std::runtime_error(
134123
"Unknown operator type in CudaqOperatorVariant.");

runtime/cudaq/operator/operator_sum.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,6 @@ std::vector<int> operator_sum::degrees() const {
9595
return std::vector<int>(unique_degrees.begin(), unique_degrees.end());
9696
}
9797

98-
/// Aggregate parameters of all terms.
99-
/*
100-
std::map<std::string, std::string> operator_sum::parameters() const {
101-
std::map<std::string, std::string> param_map;
102-
103-
for (const auto &term : m_terms) {
104-
for (const auto &op_variant : term.get_terms()) {
105-
std::visit(
106-
[&param_map](auto &op) {
107-
const auto &op_params = op.parameters;
108-
param_map.insert(op_params.begin(), op_params.end());
109-
},
110-
op_variant);
111-
}
112-
}
113-
114-
return param_map;
115-
}
116-
*/
117-
11898
/// Check if the operator sum acts as a spin operator.
11999
bool operator_sum::_is_spinop() const {
120100
return std::all_of(
@@ -209,16 +189,6 @@ operator_sum operator_sum::operator+=(const operator_sum &other) {
209189
// return operator_sum(product_terms);
210190
// }
211191

212-
/// FIXME:
213-
// operator_sum operator_sum::operator/(const operator_sum &other) const {
214-
// std::vector<product_operator> divided_terms;
215-
// for (const auto &term : m_terms) {
216-
// divided_terms.push_back(term / other);
217-
// }
218-
219-
// return operator_sum(divided_terms);
220-
// }
221-
222192
operator_sum operator_sum::operator+(const scalar_operator &other) const {
223193
std::vector<product_operator> combined_terms = m_terms;
224194
combined_terms.push_back(product_operator({other}));

0 commit comments

Comments
 (0)