Skip to content

Commit ae6f34f

Browse files
authored
🏗️ Remove SolverFactory to entirely isolate NALAC (#609)
## Description NASP had still a dependency on NALAC. The only part where this was used was the solver factory. Sine this was not super useful anyways, the solver factory is removed together with the dependency on NALAC. ## Checklist: - [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.
2 parents f56efbf + ece67e9 commit ae6f34f

File tree

9 files changed

+92
-486
lines changed

9 files changed

+92
-486
lines changed

include/na/nasp/Solver.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "Definitions.hpp"
4+
#include "ir/QuantumComputation.hpp"
45

56
#include <cstddef>
67
#include <cstdint>
@@ -467,6 +468,36 @@ class NASolver {
467468
uint16_t newNumQubits, uint16_t newNumStages,
468469
std::optional<uint16_t> newNumTransfers = std::nullopt,
469470
bool mindOpsOrder = false, bool shieldIdleQubits = true) -> Result;
471+
472+
/**
473+
* @brief Get the list of entangling operations that the solver takes as
474+
* input.
475+
*
476+
* @details The solver only considers the entangling operations of a circuit.
477+
* For that it receives a list of qubit pairs that represent each one
478+
* entangling operation. This function generates this list from a given
479+
* QuantumComputation and a FullOpType that specifies the entangling
480+
* operation.
481+
*
482+
* @warning This function expects a QuantumComputation that was used as input
483+
* for the NASolver. Additionally, this function assumes the quantum circuit
484+
* represented by the QuantumComputation to be of the following form:
485+
* First, all qubits are initialized in the |+> state by applying a Hadamard
486+
* gate to each qubit. Then, a set of entangling gates (CZ) is applied to the
487+
* qubits. Finally, hadamard gates are applied to some qubits. Unfortunately,
488+
* the function cannot deal with arbitrary quantum circuits as the NASolver
489+
* cannot do either.
490+
*
491+
* @param circ
492+
* @param opType
493+
* @param ctrls
494+
* @param quiet
495+
* @return
496+
*/
497+
[[nodiscard]] static auto
498+
getOpsForSolver(const qc::QuantumComputation& circ, qc::OpType opType,
499+
std::size_t ctrls, bool quiet = false)
500+
-> std::vector<std::pair<qc::Qubit, qc::Qubit>>;
470501
};
471502

472503
struct ExprHash {

include/na/nasp/SolverFactory.hpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/na/nasp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if(NOT TARGET ${MQT_QMAP_NASP_TARGET_NAME})
1212
target_link_libraries(
1313
${MQT_QMAP_NASP_TARGET_NAME}
1414
PUBLIC MQT::CoreNA nlohmann_json::nlohmann_json z3::z3lib
15-
PRIVATE MQT::CoreCircuitOptimizer MQT::ProjectOptions MQT::ProjectWarnings MQT::NALAC)
15+
PRIVATE MQT::CoreDS MQT::CoreCircuitOptimizer MQT::ProjectOptions MQT::ProjectWarnings)
1616

1717
add_library(MQT::NASP ALIAS ${MQT_QMAP_NASP_TARGET_NAME})
1818
endif()

src/na/nasp/Solver.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "na/nasp/Solver.hpp"
22

33
#include "Definitions.hpp"
4+
#include "circuit_optimizer/CircuitOptimizer.hpp"
5+
#include "ir/QuantumComputation.hpp"
46

57
#include <algorithm>
68
#include <cstddef>
@@ -764,4 +766,30 @@ auto NASolver::Result::json() const -> nlohmann::json {
764766
auto NASolver::Result::operator==(const Result& other) const -> bool {
765767
return sat == other.sat && stages == other.stages;
766768
}
769+
auto NASolver::getOpsForSolver(const qc::QuantumComputation& circ,
770+
const qc::OpType opType, const std::size_t ctrls,
771+
const bool quiet)
772+
-> std::vector<std::pair<unsigned int, unsigned int>> {
773+
auto flattened = circ;
774+
qc::CircuitOptimizer::flattenOperations(flattened);
775+
std::vector<std::pair<unsigned int, unsigned int>> ops;
776+
ops.reserve(flattened.size());
777+
for (const auto& op : flattened) {
778+
if (op->getType() == opType && op->getNcontrols() == ctrls) {
779+
const auto& operands = op->getUsedQubits();
780+
if (operands.size() != 2) {
781+
std::stringstream ss;
782+
ss << "Operation " << op->getName() << " does not have two operands.";
783+
throw std::invalid_argument(ss.str());
784+
}
785+
ops.emplace_back(*operands.cbegin(), *operands.rbegin());
786+
} else if (!quiet) {
787+
std::stringstream ss;
788+
ss << "Operation " << op->getName() << " is not of type " << opType
789+
<< " or does not have " << ctrls << " controls.";
790+
throw std::invalid_argument(ss.str());
791+
}
792+
}
793+
return ops;
794+
}
767795
} // namespace na

src/na/nasp/SolverFactory.cpp

Lines changed: 0 additions & 114 deletions
This file was deleted.

src/python/bindings.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "na/NAComputation.hpp"
1919
#include "na/nasp/CodeGenerator.hpp"
2020
#include "na/nasp/Solver.hpp"
21-
#include "na/nasp/SolverFactory.hpp"
2221
#include "qasm3/Importer.hpp"
2322
#include "sc/Architecture.hpp"
2423
#include "sc/Mapper.hpp"
@@ -1020,7 +1019,7 @@ of the abstraction from the 2D grid used for the solver must be provided again.
10201019
std::transform(opTypeLowerStr.begin(), opTypeLowerStr.end(),
10211020
opTypeLowerStr.begin(),
10221021
[](unsigned char c) { return std::tolower(c); });
1023-
return na::SolverFactory::getOpsForSolver(
1022+
return na::NASolver::getOpsForSolver(
10241023
qc, qc::opTypeFromString(operationType), numControls, quiet);
10251024
},
10261025
"qc"_a, "operation_type"_a = "Z", "num_operands"_a = 1, "quiet"_a = true,

test/na/nasp/test_codegenerator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "na/NAComputation.hpp"
44
#include "na/nasp/CodeGenerator.hpp"
55
#include "na/nasp/Solver.hpp"
6-
#include "na/nasp/SolverFactory.hpp"
76
#include "qasm3/Importer.hpp"
87

98
#include <cstdint>
@@ -26,7 +25,7 @@ TEST(CodeGenerator, Generate) {
2625
// storage zone at the top and at the bottom
2726
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
2827
// get operations for solver
29-
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
28+
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
3029
// solve
3130
const auto result =
3231
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,

0 commit comments

Comments
 (0)