Skip to content

Commit

Permalink
push with couts to question bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdilkes committed Nov 12, 2024
1 parent da718fc commit 42f2884
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pytket/tests/predicates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,9 @@ def test_greedy_pauli_synth() -> None:
).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 not GreedyPauliSimp(0.5, 0.5, thread_timeout=0, only_reduce=False).apply(d)
assert pss.apply(d)
assert GreedyPauliSimp(0.5, 0.5, thread_timeout = 10, trials = 5, threads = 5).apply(d)
assert np.allclose(circ.get_unitary(), d.get_unitary())
assert d.name == "test"
# test gateset
Expand Down
18 changes: 17 additions & 1 deletion tket/src/Transformations/GreedyPauliOptimisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ Circuit greedy_pauli_graph_synthesis(
}

} // namespace GreedyPauliSimp

Transform greedy_pauli_optimisation(
double discount_rate, double depth_weight, unsigned max_lookahead,
unsigned max_tqe_candidates, unsigned seed, bool allow_zzphase,
Expand All @@ -790,6 +791,8 @@ Transform greedy_pauli_optimisation(
std::min(threads, std::thread::hardware_concurrency());
unsigned threads_started = 0;

std::cout << "Doing GPO! Start off, number of threads and trials: "
<< max_threads << " " << threads << " " << trials << std::endl;
while (threads_started < trials || !all_threads.empty()) {
// Start new jobs if we haven't reached the max threads or trials
if (threads_started < trials && all_threads.size() < max_threads) {
Expand All @@ -811,8 +814,21 @@ Transform greedy_pauli_optimisation(
Circuit c = thread.get();
c.decompose_boxes_recursively();
circuits.push_back(c);
all_threads.pop();
} else {
// If the thread is not ready, move it to the back of the queue
all_threads.push(std::move(all_threads.front()));
all_threads.pop();
}
all_threads.pop();
}

std::cout << "Found " << circuits.size() << " circuits! " << std::endl;

for (unsigned i = 0; i < circuits.size(); i++) {
Circuit c = circuits[i];
std::cout << "Circuit " << i << " has " << c.count_n_qubit_gates(2)
<< " two-qubit gates, " << c.n_gates() << " gates and depth "
<< c.depth() << std::endl;
}

// Return the smallest circuit if any were found within the single
Expand Down
66 changes: 66 additions & 0 deletions tket/test/src/test_GreedyPauli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,5 +760,71 @@ SCENARIO("Test GreedyPauliSimp pass construction") {
->apply(cu));
}
}

SCENARIO("Test GreedyPauliSimp with multiple trials and threads") {
GIVEN("Large circuit with ZZPhase") {
Circuit circ(6);
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::X, Pauli::X}, 0.3)), {0, 1});
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::Z, Pauli::Y, Pauli::X}, 0.2)),
{0, 1, 2});
circ.add_box(
PauliExpCommutingSetBox({
{{Pauli::I, Pauli::Y, Pauli::I, Pauli::Z}, 1.2},
{{Pauli::X, Pauli::Y, Pauli::Z, Pauli::I}, 0.8},
{{Pauli::I, Pauli::I, Pauli::I, Pauli::Z}, 1.25},
}),
{1, 2, 3, 4});
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::Y, Pauli::X}, 0.1)), {2, 3});
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::Z, Pauli::Y, Pauli::X}, 0.11)),
{1, 3, 4});
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::Y, Pauli::Y}, 0.2)), {4, 5});
circ.add_box(
PauliExpBox(SymPauliTensor({Pauli::Z, Pauli::Z, Pauli::X}, 0.15)),
{2, 4, 5});
circ.add_box(
PauliExpBox(
SymPauliTensor({Pauli::X, Pauli::X, Pauli::X, Pauli::X}, 0.25)),
{2, 4, 5, 0});
circ.add_box(
PauliExpBox(
SymPauliTensor({Pauli::Y, Pauli::Z, Pauli::Z, Pauli::X}, 0.125)),
{1, 3, 5, 0});

circ.add_box(
PauliExpBox(SymPauliTensor(
{Pauli::X, Pauli::Z, Pauli::Y, Pauli::Y, Pauli::Z, Pauli::X},
0.125)),
{1, 3, 5, 0, 2, 4});

circ.add_box(
PauliExpBox(SymPauliTensor(
{Pauli::Z, Pauli::Y, Pauli::Y, Pauli::Z, Pauli::Z, Pauli::X},
0.125)),
{0, 1, 2, 3, 4, 5});

circ.add_box(
PauliExpBox(SymPauliTensor(
{Pauli::X, Pauli::Z, Pauli::Y, Pauli::Z, Pauli::Z, Pauli::X},
0.125)),
{5, 2, 4, 1, 3, 0});

circ.add_box(
PauliExpBox(SymPauliTensor(
{Pauli::X, Pauli::Z, Pauli::Y, Pauli::Y, Pauli::Z, Pauli::X},
0.125)),
{0, 5, 1, 4, 3, 2});

Circuit d(circ);
REQUIRE(Transforms::greedy_pauli_optimisation(
0.7, 0.3, 500, 500, 0, true, 10, 10, 10)
.apply(d));
REQUIRE(test_unitary_comparison(circ, d, true));
}
}
} // namespace test_GreedyPauliSimp
} // namespace tket

0 comments on commit 42f2884

Please sign in to comment.