Skip to content

Commit

Permalink
update flattenrelabelregisters
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdilkes committed Nov 8, 2024
1 parent 8cda115 commit 809d385
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
7 changes: 5 additions & 2 deletions pytket/binders/passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 3 additions & 1 deletion pytket/pytket/_tket/circuit.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down
3 changes: 2 additions & 1 deletion pytket/pytket/_tket/passes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions schemas/compiler_pass_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,10 @@
},
"then": {
"required": [
"label"
"label",
"relabel_classical_registers"
],
"maxProperties": 2
"maxProperties": 3
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion tket/include/tket/Predicates/PassGenerators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tket/src/Predicates/CompilerPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>());
content.at("label").get<std::string>(), content.at("relabel_classical_expressions").get<bool>());
} else if (passname == "RoutingPass") {
Architecture arc = content.at("architecture").get<Architecture>();
std::vector<RoutingMethodPtr> con = content.at("routing_config");
Expand Down
5 changes: 3 additions & 2 deletions tket/src/Predicates/PassGenerators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ PassPtr gen_clifford_push_through_pass() {
return std::make_shared<StandardPass>(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<unit_bimaps_t> maps) {
unsigned n_qubits = circuit.n_qubits();
Expand All @@ -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;
});
Expand All @@ -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<StandardPass>(precons, t, postcons, j);
}

Expand Down

0 comments on commit 809d385

Please sign in to comment.