Skip to content

Commit

Permalink
Count all 2q vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-cqc committed Jul 20, 2023
1 parent af9d69a commit ad33df3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
10 changes: 5 additions & 5 deletions pytket/binders/circuit/Circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,19 @@ void init_circuit(py::module &m) {
"element of `types`",
py::arg("types"))
.def(
"depth_by_2q_gates", &Circuit::depth_by_2q_gates,
"depth_2q", &Circuit::depth_2q,
"Returns the number of vertices in the longest path through the "
"sub-DAG consisting of vertices representing 2-qubit quantum gates."
"Note that box types and conditionals are not included."
"sub-DAG consisting of vertices with 2 quantum wires,"
"excluding vertices representing barrier operations."
"\n\n>>> c = Circuit(3)"
"\n>>> c.CZ(0,1)"
"\n>>> c.Z(0)"
"\n>>> c.Z(1)"
"\n>>> c.ZZMax(1,2)"
"\n>>> c.CX(1,2)"
"\n>>> c.depth_by_2q_gates()"
"\n>>> c.depth_2q()"
"\n3"
"\n:return: the circuit depth with respect to 2-qubit quantum gates.")
"\n:return: the circuit depth with respect to 2-qubit operations.")
.def(
"_to_graphviz_file", &Circuit::to_graphviz_file,
"Saves a visualisation of a circuit's DAG to a \".dot\" file",
Expand Down
2 changes: 1 addition & 1 deletion pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Unreleased

Minor new features:

* Add circuit method ``depth_by_2q_gates``
* Add circuit method ``depth_2q``


1.17.1 (July 2023)
Expand Down
2 changes: 1 addition & 1 deletion pytket/tests/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ def test_depth() -> None:
assert c.depth_by_type({OpType.CX, OpType.H}) == 6
assert c.depth_by_type({OpType.CZ, OpType.H}) == 6
assert c.depth_by_type(set()) == 0
assert c.depth_by_2q_gates() == 6
assert c.depth_2q_gates() == 6


def test_op_dagger_transpose() -> None:
Expand Down
9 changes: 5 additions & 4 deletions tket/include/tket/Circuit/Circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,14 +1185,15 @@ class Circuit {
unsigned depth_by_types(const OpTypeSet &_types) const;

/**
* Depth of circuit restricting to 2-qubit quantum gates.
* Depth of circuit restricting to 2-qubit operations, excluding barriers.
*
* This is the number of vertices in the longest path through the sub-DAG
* consisting of vertices representing 2-qubit operations.
* This is the number of vertices in the longest path through the
* sub-DAG consisting of vertices with 2 quantum wires,
* excluding vertices representing barrier operations.
*
* @return depth
*/
unsigned depth_by_2q_gates() const;
unsigned depth_2q() const;

std::map<Vertex, unit_set_t> vertex_unit_map() const;
std::map<Vertex, unsigned> vertex_depth_map() const;
Expand Down
4 changes: 2 additions & 2 deletions tket/src/Circuit/macro_circ_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,10 @@ unsigned Circuit::depth_by_types(const OpTypeSet& _types) const {
return count;
}

unsigned Circuit::depth_by_2q_gates() const {
unsigned Circuit::depth_2q() const {
unsigned count = 0;
std::function<bool(Op_ptr)> skip_func = [&](Op_ptr op) {
return (op->n_qubits() != 2 || !is_gate_type(op->get_type()));
return (op->n_qubits() != 2 || op->get_type() == OpType::Barrier);
};
Circuit::SliceIterator slice_iter(*this, skip_func);
if (!(*slice_iter).empty()) count++;
Expand Down
10 changes: 5 additions & 5 deletions tket/test/src/Circuit/test_Circ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,15 +1326,15 @@ SCENARIO("Test depth_by_type method") {
REQUIRE(circ.depth_by_types({OpType::CX, OpType::CY}) == 2);
}
}
SCENARIO("Test depth_by_2q_gates method") {
SCENARIO("Test depth_2q_gates method") {
GIVEN("2q OpTypes") {
Circuit circ(3, 1);
circ.add_op<unsigned>(OpType::CX, {0, 1});
circ.add_op<unsigned>(OpType::Z, {0});
circ.add_op<unsigned>(OpType::Z, {1});
circ.add_op<unsigned>(OpType::CZ, {1, 0});
circ.add_conditional_gate<unsigned>(OpType::CY, {}, {1, 2}, {0}, 0);
REQUIRE(circ.depth_by_2q_gates() == 2);
REQUIRE(circ.depth_2q() == 3);
}
GIVEN("Boxes") {
Circuit circ(3);
Expand All @@ -1343,22 +1343,22 @@ SCENARIO("Test depth_by_2q_gates method") {
CircBox cbox(inner);
circ.add_box(cbox, {0, 1});
circ.add_op<unsigned>(OpType::Z, {1});
REQUIRE(circ.depth_by_2q_gates() == 0);
REQUIRE(circ.depth_2q() == 1);
}
GIVEN("Multi-q gates") {
Circuit circ(5, 2);
circ.add_op<unsigned>(OpType::CnX, {0, 1, 2, 4, 3});
circ.add_op<unsigned>(OpType::CnX, {0, 2});
circ.add_op<unsigned>(OpType::CnX, {2, 4, 3});
circ.add_op<unsigned>(OpType::CnX, {3, 4});
REQUIRE(circ.depth_by_2q_gates() == 2);
REQUIRE(circ.depth_2q() == 2);
}
GIVEN("A circuit with other causal links") {
Circuit circ(4);
circ.add_op<unsigned>(OpType::CX, {0, 1});
circ.add_barrier({0, 2});
circ.add_op<unsigned>(OpType::CZ, {2, 3});
REQUIRE(circ.depth_by_2q_gates() == 2);
REQUIRE(circ.depth_2q() == 2);
}
}

Expand Down

0 comments on commit ad33df3

Please sign in to comment.