Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/na/nasp/Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cstddef>
#include <cstdint>
#include <ir/QuantumComputation.hpp>
#include <memory>
#include <nlohmann/json_fwd.hpp>
#include <optional>
Expand Down Expand Up @@ -467,6 +468,36 @@ class NASolver {
uint16_t newNumQubits, uint16_t newNumStages,
std::optional<uint16_t> newNumTransfers = std::nullopt,
bool mindOpsOrder = false, bool shieldIdleQubits = true) -> Result;

/**
* @brief Get the list of entangling operations that the solver takes as
* input.
*
* @details The solver only considers the entangling operations of a circuit.
* For that it receives a list of qubit pairs that represent each one
* entangling operation. This function generates this list from a given
* QuantumComputation and a FullOpType that specifies the entangling
* operation.
*
* @warning This function expects a QuantumComputation that was used as input
* for the NASolver. Additionally, this function assumes the quantum circuit
* represented by the QuantumComputation to be of the following form:
* First, all qubits are initialized in the |+> state by applying a Hadamard
* gate to each qubit. Then, a set of entangling gates (CZ) is applied to the
* qubits. Finally, hadamard gates are applied to some qubits. Unfortunately,
* the function cannot deal with arbitrary quantum circuits as the NASolver
* cannot do either.
*
* @param circ
* @param opType
* @param ctrls
* @param quiet
* @return
*/
[[nodiscard]] static auto
getOpsForSolver(const qc::QuantumComputation& circ, qc::OpType opType,
std::size_t ctrls, bool quiet = false)
-> std::vector<std::pair<qc::Qubit, qc::Qubit>>;
};

struct ExprHash {
Expand Down
57 changes: 0 additions & 57 deletions include/na/nasp/SolverFactory.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/na/nasp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if(NOT TARGET ${MQT_QMAP_NASP_TARGET_NAME})
target_link_libraries(
${MQT_QMAP_NASP_TARGET_NAME}
PUBLIC MQT::CoreNA nlohmann_json::nlohmann_json z3::z3lib
PRIVATE MQT::CoreCircuitOptimizer MQT::ProjectOptions MQT::ProjectWarnings MQT::NALAC)
PRIVATE MQT::CoreDS MQT::CoreCircuitOptimizer MQT::ProjectOptions MQT::ProjectWarnings)

add_library(MQT::NASP ALIAS ${MQT_QMAP_NASP_TARGET_NAME})
endif()
28 changes: 28 additions & 0 deletions src/na/nasp/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include "Definitions.hpp"

#include <algorithm>
#include <circuit_optimizer/CircuitOptimizer.hpp>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <ir/QuantumComputation.hpp>
#include <memory>
#include <nlohmann/json.hpp>
#include <numeric>
Expand Down Expand Up @@ -764,4 +766,30 @@ auto NASolver::Result::json() const -> nlohmann::json {
auto NASolver::Result::operator==(const Result& other) const -> bool {
return sat == other.sat && stages == other.stages;
}
auto NASolver::getOpsForSolver(const qc::QuantumComputation& circ,
const qc::OpType opType, const std::size_t ctrls,
const bool quiet)
-> std::vector<std::pair<unsigned int, unsigned int>> {
auto flattened = circ;
qc::CircuitOptimizer::flattenOperations(flattened);
std::vector<std::pair<unsigned int, unsigned int>> ops;
ops.reserve(flattened.size());
for (const auto& op : flattened) {
if (op->getType() == opType && op->getNcontrols() == ctrls) {
const auto& operands = op->getUsedQubits();
if (operands.size() != 2) {
std::stringstream ss;
ss << "Operation " << op->getName() << " does not have two operands.";
throw std::invalid_argument(ss.str());
}
ops.emplace_back(*operands.cbegin(), *operands.rbegin());
} else if (!quiet) {
std::stringstream ss;
ss << "Operation " << op->getName() << " is not of type " << opType
<< " or does not have " << ctrls << " controls.";
throw std::invalid_argument(ss.str());
}
}
return ops;
}
} // namespace na
114 changes: 0 additions & 114 deletions src/na/nasp/SolverFactory.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions src/python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "na/NAComputation.hpp"
#include "na/nasp/CodeGenerator.hpp"
#include "na/nasp/Solver.hpp"
#include "na/nasp/SolverFactory.hpp"
#include "qasm3/Importer.hpp"
#include "sc/Architecture.hpp"
#include "sc/Mapper.hpp"
Expand Down Expand Up @@ -1020,7 +1019,7 @@ of the abstraction from the 2D grid used for the solver must be provided again.
std::transform(opTypeLowerStr.begin(), opTypeLowerStr.end(),
opTypeLowerStr.begin(),
[](unsigned char c) { return std::tolower(c); });
return na::SolverFactory::getOpsForSolver(
return na::NASolver::getOpsForSolver(
qc, qc::opTypeFromString(operationType), numControls, quiet);
},
"qc"_a, "operation_type"_a = "Z", "num_operands"_a = 1, "quiet"_a = true,
Expand Down
3 changes: 1 addition & 2 deletions test/na/nasp/test_codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "na/NAComputation.hpp"
#include "na/nasp/CodeGenerator.hpp"
#include "na/nasp/Solver.hpp"
#include "na/nasp/SolverFactory.hpp"
#include "qasm3/Importer.hpp"

#include <cstdint>
Expand All @@ -26,7 +25,7 @@ TEST(CodeGenerator, Generate) {
// storage zone at the top and at the bottom
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand Down
17 changes: 8 additions & 9 deletions test/na/nasp/test_solver.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ir/QuantumComputation.hpp"
#include "ir/operations/OpType.hpp"
#include "na/nasp/Solver.hpp"
#include "na/nasp/SolverFactory.hpp"
#include "qasm3/Importer.hpp"

#include <algorithm>
Expand All @@ -18,7 +17,7 @@ TEST(Solver, SteaneDoubleSidedStorage) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand All @@ -32,7 +31,7 @@ TEST(Solver, ShorDoubleSidedStorage) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand All @@ -46,7 +45,7 @@ TEST(Solver, Surface3DoubleSidedStorage) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand All @@ -61,7 +60,7 @@ TEST(Solver, SteaneBottomStorage) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 0, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto resultUnsat =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand Down Expand Up @@ -99,7 +98,7 @@ TEST(Solver, NoShieldingFixedOrder) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 0, 7);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 3,
Expand All @@ -113,7 +112,7 @@ TEST(Solver, FixedTransfer) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result = solver.solve(
pairs, static_cast<uint16_t>(circ.getNqubits()), 5, 2, false, true);
Expand All @@ -126,7 +125,7 @@ TEST(Solver, Unsat) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 3,
Expand Down Expand Up @@ -159,7 +158,7 @@ TEST(Solver, JSONRoundTrip) {
// create solver
na::NASolver solver(3, 7, 2, 3, 2, 2, 2, 2, 2, 4);
// get operations for solver
const auto& pairs = na::SolverFactory::getOpsForSolver(circ, qc::Z, 1, true);
const auto& pairs = na::NASolver::getOpsForSolver(circ, qc::Z, 1, true);
// solve
const auto result =
solver.solve(pairs, static_cast<uint16_t>(circ.getNqubits()), 4,
Expand Down
Loading
Loading