Skip to content

Commit

Permalink
Reorder CZ circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-cqc committed Dec 10, 2021
1 parent 4c92d8d commit 9d67720
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions tket/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ set(TKET_SOURCES
${TKET_TRANSFORM_DIR}/MeasurePass.cpp
${TKET_TRANSFORM_DIR}/ContextualReduction.cpp
${TKET_TRANSFORM_DIR}/ThreeQubitSquash.cpp
${TKET_TRANSFORM_DIR}/CZReordering.cpp

# Routing
${TKET_ROUTING_DIR}/Qubit_Placement.cpp
Expand Down
59 changes: 59 additions & 0 deletions tket/src/Transformations/CZReordering.cpp
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 &current_vertex, const Edge &current_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
5 changes: 5 additions & 0 deletions tket/src/Transformations/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ class Transform {
*/
static Transform simplify_measured();

///////////////
// Reordering CZ//
///////////////
static Transform reorder_cz(const ArchitecturePtr& architecture);

///////////////////////////////////////////////
// Minor components for other Transform passes//
///////////////////////////////////////////////
Expand Down
37 changes: 37 additions & 0 deletions tket/tests/test_ReorderingCZ.cpp
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])}));

}
}
1 change: 1 addition & 0 deletions tket/tests/tkettestsfiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(TEST_SOURCES
${TKET_TESTS_DIR}/tests_main.cpp
${TKET_TESTS_DIR}/testutil.cpp
${TKET_TESTS_DIR}/CircuitsForTesting.cpp
${TKET_TESTS_DIR}/test_ReorderingCZ.cpp
${TKET_TESTS_DIR}/Utils/test_MatrixAnalysis.cpp
${TKET_TESTS_DIR}/Utils/test_CosSinDecomposition.cpp
${TKET_TESTS_DIR}/Graphs/EdgeSequence.cpp
Expand Down

0 comments on commit 9d67720

Please sign in to comment.