-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2019-2021 Cambridge Quantum Computing | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "Transform.hpp" | ||
|
||
namespace tket { | ||
|
||
// For a gate vertex and an edge, traverse back to find the associated input vertex. | ||
Vertex get_input_from_vertex_edge(Circuit &circ, const Vertex ¤t_vertex, const Edge ¤t_outedge) { | ||
Edge current_e = current_outedge; | ||
Vertex current_v = current_vertex; | ||
while (true) { | ||
if (is_initial_q_type(circ.get_OpType_from_Vertex(current_v))) { | ||
return current_v; | ||
} | ||
std::tie(current_v, current_e) = circ.get_prev_pair(current_v, current_e); | ||
} | ||
} | ||
// Assume the circuit only contains CZ gates as the two qubit gate | ||
Transform Transform::reorder_cz(const ArchitecturePtr& architecture) { | ||
return Transform([architecture](Circuit &circ) { | ||
bool success = false; | ||
BGL_FORALL_VERTICES(vert, circ.dag, DAG) { | ||
if (circ.get_OpType_from_Vertex(vert) == OpType::CZ) | ||
{ | ||
// Check if this operation is valid | ||
Vertex input_1 = get_input_from_vertex_edge(circ, vert, circ.get_nth_out_edge(vert, 0)); | ||
Vertex input_2 = get_input_from_vertex_edge(circ, vert, circ.get_nth_out_edge(vert, 1)); | ||
UnitID q_1 = circ.get_id_from_in(input_1); | ||
UnitID q_2 = circ.get_id_from_in(input_2); | ||
if (architecture->valid_operation({Node(q_1), Node(q_2)})) { | ||
// Remove the vertex | ||
circ.remove_vertex( | ||
vert, Circuit::GraphRewiring::Yes, | ||
Circuit::VertexDeletion::No); | ||
// Get input edges for in_q and out_q | ||
Edge edge_1 = circ.get_nth_out_edge(input_1, 0); | ||
Edge edge_2 = circ.get_nth_out_edge(input_2, 0); | ||
// Move the gate to the front | ||
circ.rewire(vert, {edge_1, edge_2}, {EdgeType::Quantum, EdgeType::Quantum}); | ||
success = true; | ||
} | ||
} | ||
} | ||
return success; | ||
}); | ||
} | ||
} // namespace tket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <catch2/catch.hpp> | ||
#include "Transformations/Transform.hpp" | ||
|
||
namespace tket { | ||
SCENARIO("Test Transform::reorder_cz") { | ||
std::vector<Node> nodes = {Node("test_node", 0), Node("test_node", 1), | ||
Node("test_node", 2), Node("node_test", 3)}; | ||
|
||
// n0 -- n1 -- n2 -- n3 | ||
Architecture architecture( | ||
{{nodes[0], nodes[1]}, | ||
{nodes[1], nodes[2]}, | ||
{nodes[2], nodes[3]}}); | ||
ArchitecturePtr shared_arc = std::make_shared<Architecture>(architecture); | ||
Circuit circ(4); | ||
std::vector<Qubit> qubits = circ.all_qubits(); | ||
// Physically invalid operations | ||
circ.add_op<UnitID>(OpType::CZ, {qubits[0], qubits[2]}); | ||
circ.add_op<UnitID>(OpType::CZ, {qubits[0], qubits[3]}); | ||
// Physically valid operations | ||
circ.add_op<UnitID>(OpType::CZ, {qubits[0], qubits[1]}); | ||
circ.add_op<UnitID>(OpType::CZ, {qubits[2], qubits[3]}); | ||
std::map<UnitID, UnitID> rename_map = { | ||
{qubits[0], nodes[0]}, {qubits[1], nodes[1]}, {qubits[2], nodes[2]}, | ||
{qubits[3], nodes[3]}}; | ||
circ.rename_units(rename_map); | ||
std::cout<<circ; | ||
Transform::reorder_cz(shared_arc).apply(circ); | ||
std::cout<<circ; | ||
std::vector<Command> commands = circ.get_commands(); | ||
Command c0 = commands[0]; | ||
Command c1 = commands[1]; | ||
REQUIRE(shared_arc->valid_operation({Node(c0.get_args()[0]), Node(c0.get_args()[1])})); | ||
REQUIRE(shared_arc->valid_operation({Node(c1.get_args()[0]), Node(c1.get_args()[1])})); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters