Skip to content

Commit 727c6f8

Browse files
committed
Override print method
1 parent 0bd1f15 commit 727c6f8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

include/mqt-core/ir/operations/IfElseOperation.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class IfElseOperation final : public Operation {
9696
return equals(op, {}, {});
9797
}
9898

99+
virtual std::ostream& print(std::ostream& os, const Permutation& permutation,
100+
std::size_t prefixWidth,
101+
std::size_t nqubits) const override;
102+
99103
void dumpOpenQASM(std::ostream& of, const QubitIndexToRegisterMap& qubitMap,
100104
const BitIndexToRegisterMap& bitMap, std::size_t indent,
101105
bool openQASM3) const override;

src/ir/operations/IfElseOperation.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <cstddef>
2121
#include <cstdint>
2222
#include <functional>
23+
#include <iomanip>
24+
#include <iostream>
2325
#include <memory>
2426
#include <ostream>
2527
#include <stdexcept>
@@ -125,6 +127,28 @@ IfElseOperation& IfElseOperation::operator=(const IfElseOperation& op) {
125127
return *this;
126128
}
127129

130+
std::ostream&
131+
IfElseOperation::print(std::ostream& os, const Permutation& permutation,
132+
[[maybe_unused]] const std::size_t prefixWidth,
133+
const std::size_t nqubits) const {
134+
if (thenOp) {
135+
thenOp->print(os, permutation, prefixWidth, nqubits);
136+
}
137+
138+
os << " " << "\033[1m\033[35m";
139+
if (controlRegister.has_value()) {
140+
assert(!controlBit.has_value());
141+
os << controlRegister->getName() << " == " << expectedValueRegister;
142+
}
143+
if (controlBit.has_value()) {
144+
assert(!controlRegister.has_value());
145+
os << (expectedValueBit ? "!" : "") << "c[" << controlBit.value() << "]";
146+
}
147+
os << "\033[0m";
148+
149+
return os;
150+
}
151+
128152
bool IfElseOperation::equals(const Operation& operation,
129153
const Permutation& perm1,
130154
const Permutation& perm2) const {
@@ -197,7 +221,9 @@ void IfElseOperation::dumpOpenQASM(std::ostream& of,
197221
of << controlRegister->getName() << ' '
198222
<< getInvertedComparisonKind(comparisonKind) << ' '
199223
<< expectedValueRegister;
200-
} else if (controlBit.has_value()) {
224+
}
225+
if (controlBit.has_value()) {
226+
assert(!controlRegister.has_value());
201227
of << (expectedValueBit ? "!" : "") << bitMap.at(*controlBit).second;
202228
}
203229
of << ") ";
@@ -219,10 +245,12 @@ std::size_t std::hash<qc::IfElseOperation>::operator()(
219245
qc::hashCombine(seed, std::hash<qc::Operation>{}(*op.getElseOp()));
220246
}
221247
if (const auto& reg = op.getControlRegister(); reg.has_value()) {
248+
assert(!op.getControlBit().has_value());
222249
qc::hashCombine(seed, std::hash<qc::ClassicalRegister>{}(reg.value()));
223250
qc::hashCombine(seed, op.getExpectedValueRegister());
224251
}
225252
if (const auto& bit = op.getControlBit(); bit.has_value()) {
253+
assert(!op.getControlRegister().has_value());
226254
qc::hashCombine(seed, bit.value());
227255
qc::hashCombine(seed, static_cast<std::size_t>(op.getExpectedValueBit()));
228256
}

0 commit comments

Comments
 (0)