Skip to content

Commit accd645

Browse files
authored
[rt] Minor clean up of BasicExecutionManager (#1176)
1 parent e5252ad commit accd645

File tree

2 files changed

+35
-71
lines changed

2 files changed

+35
-71
lines changed

runtime/cudaq/qis/managers/BasicExecutionManager.h

+34-66
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
******************************************************************************/
88

99
#include "common/ExecutionContext.h"
10-
#include "common/Logger.h"
1110
#include "cudaq/qis/execution_manager.h"
1211

13-
#include <complex>
14-
#include <functional>
15-
#include <map>
16-
#include <queue>
17-
#include <stack>
18-
1912
namespace cudaq {
2013

2114
/// @brief The `BasicExecutionManager` provides a common base class for
@@ -41,30 +34,25 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
4134
std::vector<cudaq::QuditInfo>, spin_op>;
4235

4336
/// @brief `typedef` for a queue of instructions
44-
using InstructionQueue = std::queue<Instruction>;
37+
using InstructionQueue = std::vector<Instruction>;
4538

46-
/// @brief The current execution context, e.g. sampling
47-
/// or observation
39+
/// @brief The current execution context, e.g. sampling or observation
4840
cudaq::ExecutionContext *executionContext = nullptr;
4941

50-
/// @brief Store qudits for delayed deletion under
51-
/// certain execution contexts
42+
/// @brief Store qudits for delayed deletion under certain execution contexts
5243
std::vector<QuditInfo> contextQuditIdsForDeletion;
5344

5445
/// @brief The current queue of operations to execute
5546
InstructionQueue instructionQueue;
5647

57-
/// @brief When adjoint operations are requested
58-
/// we can store them here for delayed execution
59-
std::stack<InstructionQueue> adjointQueueStack;
48+
/// @brief When adjoint operations are requested we can store them here for
49+
/// delayed execution
50+
std::vector<InstructionQueue> adjointQueueStack;
6051

61-
/// @brief When we are in a control region, we
62-
/// need to store extra control qudit ids.
52+
/// @brief When we are in a control region, we need to store extra control
53+
/// qudit ids.
6354
std::vector<std::size_t> extraControlIds;
6455

65-
/// @brief Flag to indicate we are in an adjoint region
66-
bool inAdjointRegion = false;
67-
6856
/// @brief Subtype-specific qudit allocation method
6957
virtual void allocateQudit(const QuditInfo &q) = 0;
7058

@@ -78,16 +66,15 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
7866
/// all qudits in the vector.
7967
virtual void deallocateQudits(const std::vector<QuditInfo> &qudits) = 0;
8068

81-
/// @brief Subtype-specific handler for when
82-
// the execution context changes
69+
/// @brief Subtype-specific handler for when the execution context changes
8370
virtual void handleExecutionContextChanged() = 0;
8471

85-
/// @brief Subtype-specific handler for when the
86-
/// current execution context has ended.
72+
/// @brief Subtype-specific handler for when the current execution context has
73+
/// ended.
8774
virtual void handleExecutionContextEnded() = 0;
8875

89-
/// @brief Subtype-specific method for affecting the
90-
/// execution of a queued instruction.
76+
/// @brief Subtype-specific method for affecting the execution of a queued
77+
/// instruction.
9178
virtual void executeInstruction(const Instruction &inst) = 0;
9279

9380
/// @brief Subtype-specific method for performing qudit measurement.
@@ -97,7 +84,6 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
9784
virtual void measureSpinOp(const cudaq::spin_op &op) = 0;
9885

9986
/// @brief Subtype-specific method for performing qudit reset.
100-
/// @param q Qudit to reset
10187
virtual void resetQudit(const QuditInfo &q) = 0;
10288

10389
public:
@@ -107,9 +93,7 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
10793
void setExecutionContext(cudaq::ExecutionContext *_ctx) override {
10894
executionContext = _ctx;
10995
handleExecutionContextChanged();
110-
while (!instructionQueue.empty()) {
111-
instructionQueue.pop();
112-
}
96+
instructionQueue.clear();
11397
}
11498

11599
void resetExecutionContext() override {
@@ -161,44 +145,30 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
161145
contextQuditIdsForDeletion.push_back(qid);
162146
}
163147

164-
void startAdjointRegion() override { adjointQueueStack.emplace(); }
148+
void startAdjointRegion() override { adjointQueueStack.emplace_back(); }
165149

166150
void endAdjointRegion() override {
167-
// Get the top queue and remove it
168-
auto adjointQueue = adjointQueueStack.top();
169-
adjointQueueStack.pop();
170-
171-
// Reverse it
172-
[](InstructionQueue &q) {
173-
std::stack<Instruction> s;
174-
while (!q.empty()) {
175-
s.push(q.front());
176-
q.pop();
177-
}
178-
while (!s.empty()) {
179-
q.push(s.top());
180-
s.pop();
181-
}
182-
}(adjointQueue);
151+
assert(!adjointQueueStack.empty() && "There must be at least one queue");
183152

184-
while (!adjointQueue.empty()) {
185-
auto front = adjointQueue.front();
186-
adjointQueue.pop();
187-
if (adjointQueueStack.empty()) {
188-
instructionQueue.push(front);
189-
} else {
190-
adjointQueueStack.top().push(front);
191-
}
192-
}
153+
auto adjointQueue = std::move(adjointQueueStack.back());
154+
adjointQueueStack.pop_back();
155+
156+
// Select the queue to which these instructions will be added.
157+
InstructionQueue *queue = adjointQueueStack.empty()
158+
? &instructionQueue
159+
: &(adjointQueueStack.back());
160+
161+
std::reverse(adjointQueue.begin(), adjointQueue.end());
162+
for (auto &instruction : adjointQueue)
163+
queue->push_back(instruction);
193164
}
194165

195166
void startCtrlRegion(const std::vector<std::size_t> &controls) override {
196-
for (auto &c : controls)
167+
for (auto c : controls)
197168
extraControlIds.push_back(c);
198169
}
199170

200171
void endCtrlRegion(const std::size_t n_controls) override {
201-
// extra_control_qubits.erase(end - n_controls, end);
202172
extraControlIds.resize(extraControlIds.size() - n_controls);
203173
}
204174

@@ -239,20 +209,18 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
239209

240210
if (!adjointQueueStack.empty()) {
241211
// Add to the adjoint instruction queue
242-
adjointQueueStack.top().emplace(std::make_tuple(
243-
mutable_name, mutable_params, mutable_controls, mutable_targets, op));
212+
adjointQueueStack.back().emplace_back(
213+
mutable_name, mutable_params, mutable_controls, mutable_targets, op);
244214
return;
245215
}
246216

247217
// Add to the instruction queue
248-
instructionQueue.emplace(std::make_tuple(std::move(mutable_name),
249-
mutable_params, mutable_controls,
250-
mutable_targets, op));
218+
instructionQueue.emplace_back(std::move(mutable_name), mutable_params,
219+
mutable_controls, mutable_targets, op);
251220
}
252221

253222
void synchronize() override {
254-
while (!instructionQueue.empty()) {
255-
auto instruction = instructionQueue.front();
223+
for (auto &instruction : instructionQueue) {
256224
if (isInTracerMode()) {
257225
auto [gateName, params, controls, targets, op] = instruction;
258226
std::vector<std::size_t> controlIds;
@@ -264,8 +232,8 @@ class BasicExecutionManager : public cudaq::ExecutionManager {
264232
} else {
265233
executeInstruction(instruction);
266234
}
267-
instructionQueue.pop();
268235
}
236+
instructionQueue.clear();
269237
}
270238

271239
int measure(const cudaq::QuditInfo &target) override {

runtime/cudaq/qis/managers/photonics/PhotonicsExecutionManager.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55
* This source code and the accompanying materials are made available under *
66
* the terms of the Apache License 2.0 which accompanies this distribution. *
77
*******************************************************************************/
8+
#include "common/Logger.h"
89
#include "cudaq/qis/managers/BasicExecutionManager.h"
9-
#include "cudaq/qis/qudit.h"
1010
#include "cudaq/spin_op.h"
1111
#include "cudaq/utils/cudaq_utils.h"
12-
#include "photonics_qis.h"
1312
#include "qpp.h"
1413
#include <cmath>
1514
#include <complex>
1615
#include <cstring>
1716
#include <functional>
1817
#include <iostream>
19-
#include <map>
20-
#include <queue>
2118
#include <sstream>
22-
#include <stack>
2319

2420
namespace cudaq {
2521

0 commit comments

Comments
 (0)