Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trials and threads to GreedyPauliSimp #1676

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pytket/binders/passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,19 @@ PYBIND11_MODULE(passes, m) {
"\n:param allow_zzphase: If set to True, allows the algorithm to "
"implement 2-qubit rotations using ZZPhase gates when deemed "
"optimal. Defaults to False."
"\n:param timeout: Sets maximum out of time spent finding solution."
"\n:param thread_timeout: Sets maximum out of time spent finding a "
"single solution in one thread."
"\n:param only_reduce: Only returns modified circuit if it has "
"fewer two-qubit gates."
"\n:param trials: Sets maximum number of found solutions."
"\n:param threads: Sets maximum number of threads used when finding "
"solutions in parallel."
"\n:return: a pass to perform the simplification",
py::arg("discount_rate") = 0.7, py::arg("depth_weight") = 0.3,
py::arg("max_lookahead") = 500, py::arg("max_tqe_candidates") = 500,
py::arg("seed") = 0, py::arg("allow_zzphase") = false,
py::arg("timeout") = 100, py::arg("only_reduce") = false);
py::arg("thread_timeout") = 100, py::arg("only_reduce") = false,
py::arg("trials") = 1, py::arg("threads") = 1);
m.def(
"PauliSquash", &PauliSquash,
"Applies :py:meth:`PauliSimp` followed by "
Expand Down
9 changes: 7 additions & 2 deletions pytket/binders/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,17 @@ PYBIND11_MODULE(transform, m) {
"\n:param allow_zzphase: If set to True, allows the algorithm to "
"implement 2-qubit rotations using ZZPhase gates when deemed "
"optimal. Defaults to False."
"\n:param timeout: Sets maximum out of time spent finding solution."
"\n:param thread_timeout: Sets maximum out of time spent finding a "
"single solution in one thread."
"\n:param trials: Sets maximum number of found solutions."
"\n:param threads: Sets maximum number of threads used when finding "
"solutions in parallel."
"\n:return: a pass to perform the simplification",
py::arg("discount_rate") = 0.7, py::arg("depth_weight") = 0.3,
py::arg("max_tqe_candidates") = 500, py::arg("max_lookahead") = 500,
py::arg("seed") = 0, py::arg("allow_zzphase") = false,
py::arg("timeout") = 100)
py::arg("thread_timeout") = 100, py::arg("trials") = 1,
py::arg("threads") = 1)
.def_static(
"ZZPhaseToRz", &Transforms::ZZPhase_to_Rz,
"Fixes all ZZPhase gate angles to [-1, 1) half turns.")
Expand Down
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.14")
self.requires("symengine/0.13.0")
self.requires("tkassert/0.3.4@tket/stable")
self.requires("tket/1.3.46@tket/stable")
self.requires("tket/1.3.47@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
4 changes: 2 additions & 2 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Features:
conditions.
* Add `custom_deserialisation` argument to `BasePass` and `SequencePass`
`from_dict` method to support construction of `CustomPass` from json.
* Add `timeout` argument to `GreedyPauliSimp`.
* Add `only_reduce` argument to `GreedyPauliSimp`.
* Add `thread_timeout`, `only_reduce`, `threads` and `trials` arguments
to `GreedyPauliSimp`.
* Add option to not relabel `ClassicalExpBox` when calling `rename_units`
and `flatten_registers`
* Implement `dagger()` and `transpose()` for `CustomGate`.
Expand Down
4 changes: 3 additions & 1 deletion pytket/tests/passes_serialisation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ def nonparam_predicate_dict(name: str) -> Dict[str, Any]:
"max_tqe_candidates": 100,
"seed": 2,
"allow_zzphase": True,
"timeout": 5000,
"thread_timeout": 5000,
"only_reduce": False,
"trials": 1,
"threads": 1,
}
),
# lists must be sorted by OpType value
Expand Down
6 changes: 3 additions & 3 deletions pytket/tests/predicates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,9 @@ def test_greedy_pauli_synth() -> None:
rega[0], regb[0]
).SWAP(regb[1], rega[0])
d = circ.copy()
pss = GreedyPauliSimp(0.5, 0.5)
assert not GreedyPauliSimp(0.5, 0.5, timeout=0, only_reduce=False).apply(d)
assert pss.apply(d)
assert not GreedyPauliSimp(0.5, 0.5, thread_timeout=0, only_reduce=True).apply(d)
assert GreedyPauliSimp(0.5, 0.5, thread_timeout=10, trials=5, threads=3).apply(d)

assert np.allclose(circ.get_unitary(), d.get_unitary())
assert d.name == "test"
# test gateset
Expand Down
20 changes: 15 additions & 5 deletions schemas/compiler_pass_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,21 @@
"type": "boolean",
"definition": "parameter controlling the use of ZZPhase gates in \"GreedyPauliSimp\""
},
"timeout": {
"thread_timeout": {
"type": "number",
"definition": "parameter controlling the maximum runtime of \"GreedyPauliSimp\""
"definition": "parameter controlling the maximum runtime of a single thread in \"GreedyPauliSimp\""
},
"only_reduce": {
"type": "boolean",
"definition": "parameter controlling whether \"GreedyPauliSimp\" can return circuits with more two qubit gates"
},
"trials": {
"type": "number",
"definition": "parameter controlling the number of random solutions found when calling \"GreedyPauliSimp\""
},
"threads": {
"type": "number",
"definition": "parameter controlling the maximum number of threads used in parallel when calling \"GreedyPauliSimp\""
}
},
"required": [
Expand Down Expand Up @@ -913,10 +921,12 @@
"max_tqe_candidates",
"seed",
"allow_zzphase",
"timeout",
"only_reduce"
"thread_timeout",
"only_reduce",
"trials",
"threads"
],
"maxProperties": 9
"maxProperties": 11
}
},
{
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.46"
version = "1.3.47"
package_type = "library"
license = "Apache 2"
homepage = "https://github.com/CQCL/tket"
Expand Down
9 changes: 6 additions & 3 deletions tket/include/tket/Predicates/PassGenerators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,18 @@ PassPtr gen_special_UCC_synthesis(
* @param max_tqe_candidates
* @param seed
* @param allow_zzphase
* @param timeout
* @param thread_timeout
* @param only_reduce
* @param threads
* @param trials
* @return PassPtr
*/
PassPtr gen_greedy_pauli_simp(
double discount_rate = 0.7, double depth_weight = 0.3,
unsigned max_lookahead = 500, unsigned max_tqe_candidates = 500,
unsigned seed = 0, bool allow_zzphase = false, unsigned timeout = 100,
bool only_reduce = false);
unsigned seed = 0, bool allow_zzphase = false,
unsigned thread_timeout = 100, bool only_reduce = false,
unsigned threads = 1, unsigned trials = 1);

/**
* Generate a pass to simplify the circuit where it acts on known basis states.
Expand Down
28 changes: 27 additions & 1 deletion tket/include/tket/Transformations/GreedyPauliOptimisation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <atomic>

#include "Transform.hpp"
#include "tket/Circuit/Circuit.hpp"
#include "tket/Clifford/UnitaryTableau.hpp"
Expand Down Expand Up @@ -600,6 +602,29 @@ class GPGraph {
std::tuple<std::vector<PauliNode_ptr>, std::vector<PauliNode_ptr>>
gpg_from_unordered_set(const std::vector<SymPauliTensor>& unordered_set);

/**
* @brief Converts the given circuit into a GPGraph and conjugates each node
* by greedily applying 2-qubit Clifford gates until the node can be realised
* as a single-qubit gate, a measurement, or a reset. The final Clifford
* operator is synthesized in a similar fashion. Allows early termination
* from a thread via a stop_flag.
*
* @param circ
* @param stop_flag
* @param discount_rate
* @param depth_weight
* @param max_lookahead
* @param max_tqe_candidates
* @param seed
* @param allow_zzphase
* @return Circuit
*/
Circuit greedy_pauli_graph_synthesis_flag(
Circuit circ, std::atomic<bool>& stop_flag, double discount_rate = 0.7,
double depth_weight = 0.3, unsigned max_lookahead = 500,
unsigned max_tqe_candidates = 500, unsigned seed = 0,
bool allow_zzphase = false);

/**
* @brief Converts the given circuit into a GPGraph and conjugates each node
* by greedily applying 2-qubit Clifford gates until the node can be realised
Expand Down Expand Up @@ -643,7 +668,8 @@ Circuit greedy_pauli_set_synthesis(
Transform greedy_pauli_optimisation(
double discount_rate = 0.7, double depth_weight = 0.3,
unsigned max_lookahead = 500, unsigned max_tqe_candidates = 500,
unsigned seed = 0, bool allow_zzphase = false, unsigned timeout = 100);
unsigned seed = 0, bool allow_zzphase = false,
unsigned thread_timeout = 100, unsigned trials = 1, unsigned threads = 1);

} // namespace Transforms

Expand Down
6 changes: 4 additions & 2 deletions tket/src/Predicates/CompilerPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,13 @@ PassPtr deserialise(
unsigned max_lookahead = content.at("max_lookahead").get<unsigned>();
unsigned seed = content.at("seed").get<unsigned>();
bool allow_zzphase = content.at("allow_zzphase").get<bool>();
unsigned timeout = content.at("timeout").get<unsigned>();
unsigned timeout = content.at("thread_timeout").get<unsigned>();
bool only_reduce = content.at("only_reduce").get<bool>();
unsigned threads = content.at("threads").get<unsigned>();
unsigned trials = content.at("trials").get<unsigned>();
pp = gen_greedy_pauli_simp(
discount_rate, depth_weight, max_lookahead, max_tqe_candidates, seed,
allow_zzphase, timeout, only_reduce);
allow_zzphase, timeout, only_reduce, threads, trials);

} else if (passname == "PauliSimp") {
// SEQUENCE PASS - DESERIALIZABLE ONLY
Expand Down
23 changes: 18 additions & 5 deletions tket/src/Predicates/PassGenerators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,17 +1018,28 @@ PassPtr gen_synthesise_pauli_graph(
PassPtr gen_greedy_pauli_simp(
double discount_rate, double depth_weight, unsigned max_lookahead,
unsigned max_tqe_candidates, unsigned seed, bool allow_zzphase,
unsigned timeout, bool only_reduce) {
unsigned thread_timeout, bool only_reduce, unsigned threads,
unsigned trials) {
Transform t =
Transform([discount_rate, depth_weight, max_lookahead, max_tqe_candidates,
seed, allow_zzphase, timeout, only_reduce](Circuit& circ) {
seed, allow_zzphase, thread_timeout, only_reduce, threads,
trials](Circuit& circ) {
Transform gpo = Transforms::greedy_pauli_optimisation(
discount_rate, depth_weight, max_lookahead, max_tqe_candidates,
seed, allow_zzphase, timeout);
seed, allow_zzphase, thread_timeout, threads, trials);
if (only_reduce) {
Circuit gpo_circ = circ;
// comparison will be inaccurate if circuit has PauliExpBox
gpo_circ.decompose_boxes_recursively();
unsigned original_n_2qb_gates = gpo_circ.count_n_qubit_gates(2);
unsigned original_n_gates = gpo_circ.n_gates();
unsigned original_depth = gpo_circ.depth();
if (gpo.apply(gpo_circ) &&
gpo_circ.count_n_qubit_gates(2) < circ.count_n_qubit_gates(2)) {
std::make_tuple(
gpo_circ.count_n_qubit_gates(2), gpo_circ.n_gates(),
gpo_circ.depth()) <
std::make_tuple(
original_n_2qb_gates, original_n_gates, original_depth)) {
circ = gpo_circ;
return true;
}
Expand Down Expand Up @@ -1084,8 +1095,10 @@ PassPtr gen_greedy_pauli_simp(
j["max_tqe_candidates"] = max_tqe_candidates;
j["seed"] = seed;
j["allow_zzphase"] = allow_zzphase;
j["timeout"] = timeout;
j["thread_timeout"] = thread_timeout;
j["only_reduce"] = only_reduce;
j["threads"] = threads;
j["trials"] = trials;

return std::make_shared<StandardPass>(precons, t, postcon, j);
}
Expand Down
Loading
Loading