Skip to content

Commit

Permalink
♻️ Refactor NonUnitaryOperation class to simplify target handling (#…
Browse files Browse the repository at this point in the history
…399)

## Description

In the NonUnitaryOperation class, the previous implementation used two
different vectors to manage the targets for Measure and other
operations, which lead to redundancy in the code. This update unifies
the handling of targets by using only one vector (named targets) for all
operations. This simplification enhances the code readability and
maintainability. It also removes the need of several functions like
getTargets() and getUsedQubits(). Several function argument lists have
also been refactored for cleaner and more consistent usage. Further, the
print() function was restructured for better clarity. Overall, these
changes will be beneficial for future development work.

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer authored Aug 16, 2023
1 parent 702369d commit 6c47062
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 74 deletions.
42 changes: 3 additions & 39 deletions include/operations/NonUnitaryOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ namespace qc {

class NonUnitaryOperation final : public Operation {
protected:
std::vector<Qubit>
qubits{}; // vector for the qubits to measure (necessary since std::set
// does not preserve the order of inserted elements)
std::vector<Bit> classics{}; // vector for the classical bits to measure into

std::ostream& printNonUnitary(std::ostream& os, const std::vector<Qubit>& q,
const std::vector<Bit>& c = {},
const Permutation& permutation = {}) const;
void printMeasurement(std::ostream& os, const std::vector<Qubit>& q,
const std::vector<Bit>& c,
const Permutation& permutation) const;
Expand All @@ -27,8 +21,7 @@ class NonUnitaryOperation final : public Operation {
NonUnitaryOperation(std::size_t nq, Qubit qubit, Bit cbit);

// General constructor
NonUnitaryOperation(std::size_t nq, const std::vector<Qubit>& qubitRegister,
OpType op = Reset);
NonUnitaryOperation(std::size_t nq, Targets qubits, OpType op = Reset);

[[nodiscard]] std::unique_ptr<Operation> clone() const override {
if (getType() == qc::Measure) {
Expand All @@ -43,50 +36,21 @@ class NonUnitaryOperation final : public Operation {

[[nodiscard]] bool isNonUnitaryOperation() const override { return true; }

[[nodiscard]] const Targets& getTargets() const override {
if (type == Measure) {
return qubits;
}
return targets;
}
Targets& getTargets() override {
if (type == Measure) {
return qubits;
}
return targets;
}
[[nodiscard]] std::size_t getNtargets() const override {
return getTargets().size();
}

[[nodiscard]] const std::vector<Bit>& getClassics() const { return classics; }
std::vector<Bit>& getClassics() { return classics; }
[[nodiscard]] size_t getNclassics() const { return classics.size(); }

[[nodiscard]] bool actsOn(Qubit i) const override;

[[nodiscard]] bool equals(const Operation& op, const Permutation& perm1,
const Permutation& perm2) const override;
[[nodiscard]] bool equals(const Operation& operation) const override {
return equals(operation, {}, {});
}

std::ostream& print(std::ostream& os) const override {
const auto& qubitArgs = getTargets();
return printNonUnitary(os, qubitArgs, classics);
}
std::ostream& print(std::ostream& os) const override { return print(os, {}); }
std::ostream& print(std::ostream& os,
const Permutation& permutation) const override {
const auto& qubitArgs = getTargets();
return printNonUnitary(os, qubitArgs, classics, permutation);
}
const Permutation& permutation) const override;

void dumpOpenQASM(std::ostream& of, const RegisterNames& qreg,
const RegisterNames& creg) const override;

[[nodiscard]] std::set<Qubit> getUsedQubits() const override {
const auto& ts = getTargets();
return {ts.begin(), ts.end()};
}
};
} // namespace qc
62 changes: 27 additions & 35 deletions src/operations/NonUnitaryOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,43 @@ namespace qc {
NonUnitaryOperation::NonUnitaryOperation(const std::size_t nq,
std::vector<Qubit> qubitRegister,
std::vector<Bit> classicalRegister)
: qubits(std::move(qubitRegister)), classics(std::move(classicalRegister)) {
if (qubits.size() != classics.size()) {
throw std::invalid_argument(
"Sizes of qubit register and classical register do not match.");
}
// i-th qubit to be measured shall be measured into i-th classical register
: classics(std::move(classicalRegister)) {
type = Measure;
nqubits = nq;
targets = std::move(qubitRegister);
Operation::setName();
if (targets.size() != classics.size()) {
throw std::invalid_argument(
"Sizes of qubit register and classical register do not match.");
}
}
NonUnitaryOperation::NonUnitaryOperation(const std::size_t nq,
const Qubit qubit, const Bit cbit) {
const Qubit qubit, const Bit cbit)
: classics({cbit}) {
type = Measure;
nqubits = nq;
qubits.emplace_back(qubit);
classics.emplace_back(cbit);
targets = {qubit};
Operation::setName();
}

// General constructor
NonUnitaryOperation::NonUnitaryOperation(
const std::size_t nq, const std::vector<Qubit>& qubitRegister, OpType op) {
NonUnitaryOperation::NonUnitaryOperation(const std::size_t nq, Targets qubits,
OpType op) {
type = op;
nqubits = nq;
targets = qubitRegister;
targets = std::move(qubits);
std::sort(targets.begin(), targets.end());
Operation::setName();
}

std::ostream& NonUnitaryOperation::printNonUnitary(
std::ostream& os, const std::vector<Qubit>& q, const std::vector<Bit>& c,
const Permutation& permutation) const {
std::ostream& NonUnitaryOperation::print(std::ostream& os,
const Permutation& permutation) const {
switch (type) {
case Measure:
printMeasurement(os, q, c, permutation);
printMeasurement(os, targets, classics, permutation);
break;
case Reset:
printReset(os, q, permutation);
printReset(os, targets, permutation);
break;
default:
break;
Expand All @@ -63,9 +62,8 @@ std::ostream& NonUnitaryOperation::printNonUnitary(
void NonUnitaryOperation::dumpOpenQASM(std::ostream& of,
const RegisterNames& qreg,
const RegisterNames& creg) const {
const auto& qubitArgs = getTargets();
if (isWholeQubitRegister(qreg, qubitArgs.front(), qubitArgs.back())) {
of << toString(type) << " " << qreg[qubitArgs.front()].first;
if (isWholeQubitRegister(qreg, targets.front(), targets.back())) {
of << toString(type) << " " << qreg[targets.front()].first;
if (type == Measure) {
of << " -> ";
assert(isWholeQubitRegister(creg, classics.front(), classics.back()));
Expand All @@ -75,7 +73,7 @@ void NonUnitaryOperation::dumpOpenQASM(std::ostream& of,
return;
}
auto classicsIt = classics.cbegin();
for (const auto& q : qubitArgs) {
for (const auto& q : targets) {
of << toString(type) << " " << qreg[q].second;
if (type == Measure) {
of << " -> " << creg[*classicsIt].second;
Expand All @@ -85,12 +83,6 @@ void NonUnitaryOperation::dumpOpenQASM(std::ostream& of,
}
}

bool NonUnitaryOperation::actsOn(Qubit i) const {
const auto& qubitArgs = getTargets();
return std::any_of(qubitArgs.cbegin(), qubitArgs.cend(),
[&i](const auto& q) { return q == i; });
}

bool NonUnitaryOperation::equals(const Operation& op, const Permutation& perm1,
const Permutation& perm2) const {
if (const auto* nonunitary = dynamic_cast<const NonUnitaryOperation*>(&op)) {
Expand All @@ -100,20 +92,20 @@ bool NonUnitaryOperation::equals(const Operation& op, const Permutation& perm1,

if (getType() == Measure) {
// check number of qubits to be measured
const auto nq1 = qubits.size();
const auto nq2 = nonunitary->qubits.size();
const auto nq1 = targets.size();
const auto nq2 = nonunitary->targets.size();
if (nq1 != nq2) {
return false;
}

// these are just sanity checks and should always be fulfilled
assert(qubits.size() == classics.size());
assert(nonunitary->qubits.size() == nonunitary->classics.size());
assert(targets.size() == classics.size());
assert(nonunitary->targets.size() == nonunitary->classics.size());

std::set<std::pair<Qubit, Bit>> measurements1{};
auto qubitIt1 = qubits.cbegin();
auto qubitIt1 = targets.cbegin();
auto classicIt1 = classics.cbegin();
while (qubitIt1 != qubits.cend()) {
while (qubitIt1 != targets.cend()) {
if (perm1.empty()) {
measurements1.emplace(*qubitIt1, *classicIt1);
} else {
Expand All @@ -124,9 +116,9 @@ bool NonUnitaryOperation::equals(const Operation& op, const Permutation& perm1,
}

std::set<std::pair<Qubit, Bit>> measurements2{};
auto qubitIt2 = nonunitary->qubits.cbegin();
auto qubitIt2 = nonunitary->targets.cbegin();
auto classicIt2 = nonunitary->classics.cbegin();
while (qubitIt2 != nonunitary->qubits.cend()) {
while (qubitIt2 != nonunitary->targets.cend()) {
if (perm2.empty()) {
measurements2.emplace(*qubitIt2, *classicIt2);
} else {
Expand Down

1 comment on commit 6c47062

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ✔️

No problems need attention.

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.