diff --git a/pytket/binders/passes.cpp b/pytket/binders/passes.cpp index 0edf619915..3d96b982a9 100644 --- a/pytket/binders/passes.cpp +++ b/pytket/binders/passes.cpp @@ -752,9 +752,12 @@ PYBIND11_MODULE(passes, m) { "FlattenRelabelRegistersPass", &gen_flatten_relabel_registers_pass, "Removes empty Quantum wires from the Circuit and relabels all Qubit to " "a register from passed name. \n\n:param label: Name to relabel " - "remaining Qubit to, default 'q'.\n:return: A pass that removes empty " + "remaining Qubit to, default 'q'.\n:param relabel_classical_expressions: " + "Whether to relabel arguments of expressions held in `ClassicalExpBox`. " + "\n:return: A pass that removes empty " "wires and relabels.", - py::arg("label") = q_default_reg()); + py::arg("label") = q_default_reg(), + py::arg("relabel_classical_expressions") = true); m.def( "RenameQubitsPass", &gen_rename_qubits_pass, diff --git a/pytket/docs/changelog.rst b/pytket/docs/changelog.rst index dab6e73aa0..5e2b38e137 100644 --- a/pytket/docs/changelog.rst +++ b/pytket/docs/changelog.rst @@ -13,6 +13,8 @@ Features: * Add `custom_deserialisation` argument to `BasePass` and `SequencePass` `from_dict` method to support construction of `CustomPass` from json. * Add `timeout` argument to `GreedyPauliSimp`. +* Add option to not relabel `ClassicalExpBox` when calling `rename_units` + and `flatten_registers` Fixes: diff --git a/pytket/pytket/_tket/circuit.pyi b/pytket/pytket/_tket/circuit.pyi index 4cbc158def..f153fd5fd0 100644 --- a/pytket/pytket/_tket/circuit.pyi +++ b/pytket/pytket/_tket/circuit.pyi @@ -2104,9 +2104,11 @@ class Circuit: :param types: the set of operation types of interest :return: the circuit depth with respect to operations matching an element of `types` """ - def flatten_registers(self) -> dict[pytket._tket.unit_id.UnitID, pytket._tket.unit_id.UnitID]: + def flatten_registers(self, relabel_classical_expression: bool = True) -> dict[pytket._tket.unit_id.UnitID, pytket._tket.unit_id.UnitID]: """ Combines all qubits into a single register namespace with the default name, and likewise for bits + + :param relabel_classical_expression: Determines whether python classical expressions held in `ClassicalExpBox` have their expression relabelled to match relabelled Bit. """ def free_symbols(self) -> set[sympy.Symbol]: """ diff --git a/pytket/pytket/_tket/passes.pyi b/pytket/pytket/_tket/passes.pyi index f2efed5973..b928a2e970 100644 --- a/pytket/pytket/_tket/passes.pyi +++ b/pytket/pytket/_tket/passes.pyi @@ -410,11 +410,12 @@ def FlattenRegisters() -> BasePass: """ Merges all quantum and classical registers into their respective default registers with contiguous indexing. """ -def FlattenRelabelRegistersPass(label: str = 'q') -> BasePass: +def FlattenRelabelRegistersPass(label: str = 'q', relabel_classical_expressions: bool = True) -> BasePass: """ Removes empty Quantum wires from the Circuit and relabels all Qubit to a register from passed name. :param label: Name to relabel remaining Qubit to, default 'q'. + :param relabel_classical_expressions: Whether to relabel arguments of expressions held in `ClassicalExpBox`. :return: A pass that removes empty wires and relabels. """ def FullMappingPass(arc: pytket._tket.architecture.Architecture, placer: pytket._tket.placement.Placement, config: typing.Sequence[pytket._tket.mapping.RoutingMethod]) -> BasePass: diff --git a/schemas/compiler_pass_v1.json b/schemas/compiler_pass_v1.json index d36580d3d8..dbd68c9e18 100644 --- a/schemas/compiler_pass_v1.json +++ b/schemas/compiler_pass_v1.json @@ -648,9 +648,10 @@ }, "then": { "required": [ - "label" + "label", + "relabel_classical_registers" ], - "maxProperties": 2 + "maxProperties": 3 } }, { diff --git a/tket/include/tket/Predicates/PassGenerators.hpp b/tket/include/tket/Predicates/PassGenerators.hpp index 59fe71bcfe..f4bcb4a8d4 100644 --- a/tket/include/tket/Predicates/PassGenerators.hpp +++ b/tket/include/tket/Predicates/PassGenerators.hpp @@ -110,7 +110,7 @@ PassPtr gen_clifford_push_through_pass(); * Qubits removed from the Circuit are preserved in the bimap, but not updated * to a new labelling. */ -PassPtr gen_flatten_relabel_registers_pass(const std::string& label); +PassPtr gen_flatten_relabel_registers_pass(const std::string& label, bool relabel_classical_expressions = true); /** * Pass to rename some or all qubits according to the given map. * diff --git a/tket/src/Predicates/CompilerPass.cpp b/tket/src/Predicates/CompilerPass.cpp index 580aa29098..5176f207a6 100644 --- a/tket/src/Predicates/CompilerPass.cpp +++ b/tket/src/Predicates/CompilerPass.cpp @@ -469,7 +469,7 @@ PassPtr deserialise( pp = gen_euler_pass(q, p, s); } else if (passname == "FlattenRelabelRegistersPass") { pp = gen_flatten_relabel_registers_pass( - content.at("label").get()); + content.at("label").get(), content.at("relabel_classical_expressions").get()); } else if (passname == "RoutingPass") { Architecture arc = content.at("architecture").get(); std::vector con = content.at("routing_config"); diff --git a/tket/src/Predicates/PassGenerators.cpp b/tket/src/Predicates/PassGenerators.cpp index d33b83edff..bbf14ab895 100644 --- a/tket/src/Predicates/PassGenerators.cpp +++ b/tket/src/Predicates/PassGenerators.cpp @@ -354,7 +354,7 @@ PassPtr gen_clifford_push_through_pass() { return std::make_shared(precons, t, pc, j); } -PassPtr gen_flatten_relabel_registers_pass(const std::string& label) { +PassPtr gen_flatten_relabel_registers_pass(const std::string& label, bool relabel_classical_expressions) { Transform t = Transform([=](Circuit& circuit, std::shared_ptr maps) { unsigned n_qubits = circuit.n_qubits(); @@ -366,7 +366,7 @@ PassPtr gen_flatten_relabel_registers_pass(const std::string& label) { relabelling_map.insert({all_qubits[i], Qubit(label, i)}); } - circuit.rename_units(relabelling_map); + circuit.rename_units(relabelling_map, relabel_classical_expressions); changed |= update_maps(maps, relabelling_map, relabelling_map); return changed; }); @@ -376,6 +376,7 @@ PassPtr gen_flatten_relabel_registers_pass(const std::string& label) { nlohmann::json j; j["name"] = "FlattenRelabelRegistersPass"; j["label"] = label; + j["relabel_classical_expressions"] = relabel_classical_expressions; return std::make_shared(precons, t, postcons, j); }