Skip to content

Commit

Permalink
Add token swapping stage to routing v3 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdilkes authored Oct 26, 2021
1 parent 8ba7968 commit 5b50029
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tket/src/Mapping/MappingManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Mapping/MappingManager.hpp"

#include "OpType/OpTypeFunctions.hpp"
#include "TokenSwapping/main_entry_functions.hpp"

namespace tket {

Expand Down Expand Up @@ -59,7 +60,19 @@ bool MappingManager::route_circuit(
// true => can use held routing method
if (rm.get().check_method(mapping_frontier, this->architecture_)) {
valid_methods = true;
rm.get().routing_method(mapping_frontier, this->architecture_);
unit_map_t partial_permutation =
rm.get().routing_method(mapping_frontier, this->architecture_);

if (partial_permutation.size() > 0) {
std::map<Node, Node> node_map;
for (const auto& x : partial_permutation) {
node_map.insert({Node(x.first), Node(x.second)});
}
for (const std::pair<Node, Node>& swap :
get_swaps(*this->architecture_, node_map)) {
mapping_frontier->add_swap(swap.first, swap.second);
}
}
break;
}
}
Expand Down
53 changes: 53 additions & 0 deletions tket/tests/test_MappingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@

namespace tket {

class TokenSwappingTester : public RoutingMethod {
public:
TokenSwappingTester(){};

bool check_method(
const std::shared_ptr<MappingFrontier>& /*mapping_frontier*/,
const ArchitecturePtr& /*architecture*/) const {
return true;
}

/**
* @param mapping_frontier Contains boundary of routed/unrouted circuit for
* modifying
* @param architecture Architecture providing physical constraints
* @return Logical to Physical mapping at boundary due to modification.
*
*/
unit_map_t routing_method(
std::shared_ptr<MappingFrontier>& /*mapping_frontier*/,
const ArchitecturePtr& /*architecture*/) const {
Node node0("test_node", 0), node1("test_node", 1), node2("test_node", 2);
return {{node0, node1}, {node1, node2}, {node2, node0}};
}
};

SCENARIO("Test MappingManager::route_circuit") {
Node node0("test_node", 0), node1("test_node", 1), node2("test_node", 2);
Architecture arc({{node0, node1}, {node1, node2}});
Expand All @@ -33,5 +58,33 @@ SCENARIO("Test MappingManager::route_circuit") {
REQUIRE_THROWS_AS(
test_mm.route_circuit(circ, test_vrm), MappingManagerError);
}
GIVEN("Method that invokes a permutation from token swapping stage.") {
Circuit circ(3);
std::vector<Qubit> qubits = circ.all_qubits();
circ.add_op<UnitID>(OpType::CX, {qubits[0], qubits[2]});
std::map<UnitID, UnitID> rename_map = {
{qubits[0], node0}, {qubits[1], node1}, {qubits[2], node2}};
circ.rename_units(rename_map);
TokenSwappingTester tst;
std::vector<std::reference_wrapper<RoutingMethod>> test_ts_rm = {tst};
test_mm.route_circuit(circ, test_ts_rm);

std::vector<Command> commands = circ.get_commands();
REQUIRE(commands.size() == 3);
Command c0 = commands[0];
unit_vector_t uid_swap_12 = {node1, node2};
REQUIRE(c0.get_args() == uid_swap_12);
REQUIRE(*c0.get_op_ptr() == *get_op_ptr(OpType::SWAP));

Command c1 = commands[1];
unit_vector_t uid_swap_01 = {node0, node1};
REQUIRE(c1.get_args() == uid_swap_01);
REQUIRE(*c1.get_op_ptr() == *get_op_ptr(OpType::SWAP));

Command c2 = commands[2];
unit_vector_t uid_cx_10 = {node1, node0};
REQUIRE(c2.get_args() == uid_cx_10);
REQUIRE(*c2.get_op_ptr() == *get_op_ptr(OpType::CX));
}
}
} // namespace tket

0 comments on commit 5b50029

Please sign in to comment.