From 42f28846aa48e50d76aa60b20b465663eb580c4d Mon Sep 17 00:00:00 2001 From: Silas Dilkes <36165522+sjdilkes@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:47:03 +0000 Subject: [PATCH] push with couts to question bug --- pytket/tests/predicates_test.py | 3 +- .../GreedyPauliOptimisation.cpp | 18 ++++- tket/test/src/test_GreedyPauli.cpp | 66 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/pytket/tests/predicates_test.py b/pytket/tests/predicates_test.py index 5de371e01a..bd7538fb78 100644 --- a/pytket/tests/predicates_test.py +++ b/pytket/tests/predicates_test.py @@ -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 diff --git a/tket/src/Transformations/GreedyPauliOptimisation.cpp b/tket/src/Transformations/GreedyPauliOptimisation.cpp index 39b9b82666..7c17086bb3 100644 --- a/tket/src/Transformations/GreedyPauliOptimisation.cpp +++ b/tket/src/Transformations/GreedyPauliOptimisation.cpp @@ -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, @@ -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) { @@ -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 diff --git a/tket/test/src/test_GreedyPauli.cpp b/tket/test/src/test_GreedyPauli.cpp index c803514183..0d3cc86123 100644 --- a/tket/test/src/test_GreedyPauli.cpp +++ b/tket/test/src/test_GreedyPauli.cpp @@ -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