Skip to content

Commit

Permalink
Decompose boxes inside conditionals (#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-alec authored Jan 22, 2025
1 parent 3ba3291 commit c69721d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pytket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def requirements(self):
self.requires("pybind11_json/0.2.15")
self.requires("symengine/0.13.0")
self.requires("tkassert/0.3.4@tket/stable")
self.requires("tket/1.3.63@tket/stable")
self.requires("tket/1.3.64@tket/stable")
self.requires("tklog/0.3.3@tket/stable")
self.requires("tkrng/0.3.3@tket/stable")
self.requires("tktokenswap/0.3.9@tket/stable")
Expand Down
1 change: 1 addition & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Features:
Fixes:

* Make `CliffordCircuitPredicate` accept circuits containing measurements.
* Make `DecomposeBoxes` pass recurse into conditional boxes.

1.39.0 (January 2025)
---------------------
Expand Down
17 changes: 17 additions & 0 deletions pytket/tests/predicates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,23 @@ def test_get_gate_set() -> None:
assert CliffordPushThroughMeasures().get_gate_set() is None


def test_decompose_inside_conditionals() -> None:
# https://github.com/CQCL/tket/issues/1583
cbox1 = CircBox(Circuit(1).H(0))
cbox0 = CircBox(Circuit(1, 1).add_circbox(cbox1, [Qubit(0)], condition=Bit(0)))
c = Circuit(1, 2).add_circbox(cbox0, [Qubit(0), Bit(0)], condition=Bit(1))
DecomposeBoxes().apply(c)
cmds = c.get_commands()
assert len(cmds) == 1
cmd = cmds[0]
op0 = cmd.op
assert isinstance(op0, Conditional)
op1 = op0.op
assert isinstance(op1, Conditional)
op2 = op1.op
assert op2.type == OpType.H


if __name__ == "__main__":
test_predicate_generation()
test_compilation_unit_generation()
Expand Down
2 changes: 1 addition & 1 deletion tket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TketConan(ConanFile):
name = "tket"
version = "1.3.63"
version = "1.3.64"
package_type = "library"
license = "Apache 2"
homepage = "https://github.com/CQCL/tket"
Expand Down
5 changes: 4 additions & 1 deletion tket/include/tket/Circuit/Circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,10 @@ class Circuit {
*
* @return whether the vertex holds a box or a conditional box
*/
bool substitute_box_vertex(Vertex &vert, VertexDeletion vertex_deletion);
bool substitute_box_vertex(
Vertex &vert, VertexDeletion vertex_deletion,
const std::unordered_set<OpType> &excluded_types = {},
const std::unordered_set<std::string> &excluded_opgroups = {});

/**
* Recursively replace each \ref Box operation by applying \ref
Expand Down
8 changes: 6 additions & 2 deletions tket/src/Circuit/macro_manipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ Circuit Circuit::conditional_circuit(
}

bool Circuit::substitute_box_vertex(
Vertex& vert, VertexDeletion vertex_deletion) {
Vertex& vert, VertexDeletion vertex_deletion,
const std::unordered_set<OpType>& excluded_types,
const std::unordered_set<std::string>& excluded_opgroups) {
Op_ptr op = get_Op_ptr_from_Vertex(vert);
bool conditional = op->get_type() == OpType::Conditional;
if (conditional) {
Expand All @@ -702,6 +704,7 @@ bool Circuit::substitute_box_vertex(
if (op->get_type() == OpType::ClassicalExpBox) return false;
const Box& b = static_cast<const Box&>(*op);
Circuit replacement = *b.to_circuit();
replacement.decompose_boxes_recursively(excluded_types, excluded_opgroups);
replacement.flatten_registers();
if (conditional) {
substitute_conditional(
Expand All @@ -725,7 +728,8 @@ bool Circuit::decompose_boxes_recursively(
if (v_opgroup && excluded_opgroups.contains(v_opgroup.value())) {
continue;
}
if (substitute_box_vertex(v, VertexDeletion::No)) {
if (substitute_box_vertex(
v, VertexDeletion::No, excluded_types, excluded_opgroups)) {
bin.push_back(v);
success = true;
}
Expand Down

0 comments on commit c69721d

Please sign in to comment.