Skip to content

Commit 61543f8

Browse files
authored
♻️ Simplify output permutation computation in circuit import (#264)
Simplifies the logic for how the output permutation is derived when importing circuits. Introduces a slight optimization where idle qubits are always marked as garbage; even when the qubit appears in the output permutation. Signed-off-by: Lukas Burgholzer <[email protected]>
1 parent 6319481 commit 61543f8

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/QuantumComputation.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,25 @@ namespace qc {
190190
}
191191
}
192192

193-
// if the output permutation is still empty, we assume the identity (i.e., it is equal to the initial layout)
194-
if (outputPermutation.empty()) {
195-
for (Qubit i = 0; i < nqubits; ++i) {
196-
// only add to output permutation if the qubit is actually acted upon
197-
if (!isIdleQubit(i)) {
198-
outputPermutation.insert({i, initialLayout.at(i)});
199-
}
200-
}
201-
}
193+
const bool buildOutputPermutation = outputPermutation.empty();
194+
for (const auto& [physicalIn, logicalIn]: initialLayout) {
195+
const bool isIdle = isIdleQubit(physicalIn);
202196

203-
// allow for incomplete output permutation -> mark rest as garbage
204-
for (const auto& in: initialLayout) {
205-
bool isOutput = false;
206-
for (const auto& out: outputPermutation) {
207-
if (in.second == out.second) {
208-
isOutput = true;
209-
break;
210-
}
197+
// if no output permutation was found, build it from the initial layout
198+
if (buildOutputPermutation && !isIdle) {
199+
outputPermutation.insert({physicalIn, logicalIn});
211200
}
201+
202+
// if the qubit is not an output, mark it as garbage
203+
const bool isOutput = std::any_of(outputPermutation.begin(), outputPermutation.end(),
204+
[&logicalIn = logicalIn](const auto& p) { return p.second == logicalIn; });
212205
if (!isOutput) {
213-
setLogicalQubitGarbage(in.second);
206+
setLogicalQubitGarbage(logicalIn);
207+
}
208+
209+
// if the qubit is an ancillary and idle, mark it as garbage
210+
if (logicalQubitIsAncillary(logicalIn) && isIdle) {
211+
setLogicalQubitGarbage(logicalIn);
214212
}
215213
}
216214
}

0 commit comments

Comments
 (0)