From 3d4ebfd62c513bca5687736f4ad9dba7359b2ae4 Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 13:48:10 +0100 Subject: [PATCH 1/6] Fix RebaseCustom --- pytket/tests/predicates_test.py | 19 +++++++++++++++++++ tket/src/Transformations/Rebase.cpp | 7 ++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pytket/tests/predicates_test.py b/pytket/tests/predicates_test.py index d8ce02347e..d881d5e602 100644 --- a/pytket/tests/predicates_test.py +++ b/pytket/tests/predicates_test.py @@ -902,6 +902,24 @@ def test_PeepholeOptimise2Q() -> None: assert all(k == v for k, v in perm.items()) +def test_rebase_custom_tk2() -> None: + def _tk1_to_phase(a: float, b: float, c: float) -> Circuit: + return Circuit(1).Rz(c, 0).Rx(b, 0).Rz(a, 0) + + def _tk2_to_phase(a: float, b: float, c: float) -> Circuit: + return Circuit(2).ZZPhase(c, 0, 1).YYPhase(b, 0, 1).XXPhase(a, 0, 1) + + to_phase_gates = RebaseCustom( + {OpType.Rx, OpType.Rz, OpType.XXPhase, OpType.YYPhase, OpType.ZZPhase}, + tk2_replacement=_tk2_to_phase, + tk1_replacement=_tk1_to_phase, + ) + + tk2_c = Circuit(2).TK2(0.123, 0.5634, 0.2345, 0, 1) + assert to_phase_gates.apply(tk2_c) + assert len(tk2_c.get_commands()) == 11 + + if __name__ == "__main__": test_predicate_generation() test_compilation_unit_generation() @@ -919,3 +937,4 @@ def test_PeepholeOptimise2Q() -> None: test_RebaseOQC_and_SynthesiseOQC() test_ZZPhaseToRz() test_flatten_relabel_pass() + test_rebase_custom_tk2() diff --git a/tket/src/Transformations/Rebase.cpp b/tket/src/Transformations/Rebase.cpp index 004d3fe7db..2230b1cc17 100644 --- a/tket/src/Transformations/Rebase.cpp +++ b/tket/src/Transformations/Rebase.cpp @@ -122,6 +122,7 @@ static bool standard_rebase_via_tk2( tk2_replacement) { bool success = false; VertexSet bin; + std::cout << "It's definitely in the TK2 version. " << std::endl; // 1. Replace all multi-qubit gates outside the target gateset to TK2. for (const Vertex& v : circ.all_vertices()) { Op_ptr op = circ.get_Op_ptr_from_Vertex(v); @@ -133,11 +134,11 @@ static bool standard_rebase_via_tk2( op = cond.get_op(); } OpType type = op->get_type(); - if (allowed_gates.contains(type) || type == OpType::TK2 || - type == OpType::Barrier) - continue; + if (allowed_gates.contains(type) || type == OpType::Barrier) continue; // need to convert + std::cout << "Converting to TK2. " << std::endl; Circuit replacement = TK2_circ_from_multiq(op); + std::cout << "TK2 Circuit: " << replacement << std::endl; // Find replacement Circuit for all TK2 gates VertexSet TK2_bin; for (const Vertex& u : replacement.all_vertices()) { From 4a3c5733c056cb80604d02e035b9fbbf0a70090d Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 13:49:36 +0100 Subject: [PATCH 2/6] bump --- pytket/conanfile.py | 2 +- tket/conanfile.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pytket/conanfile.py b/pytket/conanfile.py index 6505b08146..cf3933f698 100644 --- a/pytket/conanfile.py +++ b/pytket/conanfile.py @@ -32,7 +32,7 @@ def package(self): cmake.install() def requirements(self): - self.requires("tket/1.2.37@tket/stable") + self.requires("tket/1.2.38@tket/stable") self.requires("tklog/0.3.3@tket/stable") self.requires("tkrng/0.3.3@tket/stable") self.requires("tkassert/0.3.3@tket/stable") diff --git a/tket/conanfile.py b/tket/conanfile.py index 1a304ebd92..ad73ac168f 100644 --- a/tket/conanfile.py +++ b/tket/conanfile.py @@ -23,7 +23,7 @@ class TketConan(ConanFile): name = "tket" - version = "1.2.37" + version = "1.2.38" package_type = "library" license = "Apache 2" homepage = "https://github.com/CQCL/tket" From db78542f3aa40f23a4f7f8f21f8b5d520b318c39 Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 14:38:37 +0100 Subject: [PATCH 3/6] Remove leftover comments --- tket/src/Transformations/Rebase.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tket/src/Transformations/Rebase.cpp b/tket/src/Transformations/Rebase.cpp index 2230b1cc17..03e3a91f5d 100644 --- a/tket/src/Transformations/Rebase.cpp +++ b/tket/src/Transformations/Rebase.cpp @@ -122,7 +122,6 @@ static bool standard_rebase_via_tk2( tk2_replacement) { bool success = false; VertexSet bin; - std::cout << "It's definitely in the TK2 version. " << std::endl; // 1. Replace all multi-qubit gates outside the target gateset to TK2. for (const Vertex& v : circ.all_vertices()) { Op_ptr op = circ.get_Op_ptr_from_Vertex(v); @@ -136,9 +135,7 @@ static bool standard_rebase_via_tk2( OpType type = op->get_type(); if (allowed_gates.contains(type) || type == OpType::Barrier) continue; // need to convert - std::cout << "Converting to TK2. " << std::endl; Circuit replacement = TK2_circ_from_multiq(op); - std::cout << "TK2 Circuit: " << replacement << std::endl; // Find replacement Circuit for all TK2 gates VertexSet TK2_bin; for (const Vertex& u : replacement.all_vertices()) { From f751caa4504fd8487f67714973bb624f6e3dced4 Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 14:38:45 +0100 Subject: [PATCH 4/6] Update test to check types --- pytket/tests/predicates_test.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pytket/tests/predicates_test.py b/pytket/tests/predicates_test.py index d881d5e602..33afe957e8 100644 --- a/pytket/tests/predicates_test.py +++ b/pytket/tests/predicates_test.py @@ -917,7 +917,19 @@ def _tk2_to_phase(a: float, b: float, c: float) -> Circuit: tk2_c = Circuit(2).TK2(0.123, 0.5634, 0.2345, 0, 1) assert to_phase_gates.apply(tk2_c) - assert len(tk2_c.get_commands()) == 11 + coms = tk2_c.get_commands() + assert len(coms) == 11 + assert coms[0].op.type == OpType.Rz + assert coms[1].op.type == OpType.Rz + assert coms[2].op.type == OpType.Rx + assert coms[3].op.type == OpType.Rx + assert coms[4].op.type == OpType.ZZPhase + assert coms[5].op.type == OpType.YYPhase + assert coms[6].op.type == OpType.XXPhase + assert coms[7].op.type == OpType.Rx + assert coms[8].op.type == OpType.Rx + assert coms[9].op.type == OpType.Rz + assert coms[10].op.type == OpType.Rz if __name__ == "__main__": From 6977cda73cfc452bfcd3e8f7cd0191672844290a Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 15:16:21 +0100 Subject: [PATCH 5/6] Update predicates_test.py --- pytket/tests/predicates_test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pytket/tests/predicates_test.py b/pytket/tests/predicates_test.py index 33afe957e8..00e41a2840 100644 --- a/pytket/tests/predicates_test.py +++ b/pytket/tests/predicates_test.py @@ -903,10 +903,14 @@ def test_PeepholeOptimise2Q() -> None: def test_rebase_custom_tk2() -> None: - def _tk1_to_phase(a: float, b: float, c: float) -> Circuit: + def _tk1_to_phase( + a: Union[Expr, float], b: Union[Expr, float], c: Union[Expr, float] + ) -> Circuit: return Circuit(1).Rz(c, 0).Rx(b, 0).Rz(a, 0) - def _tk2_to_phase(a: float, b: float, c: float) -> Circuit: + def _tk2_to_phase( + a: Union[Expr, float], b: Union[Expr, float], c: Union[Expr, float] + ) -> Circuit: return Circuit(2).ZZPhase(c, 0, 1).YYPhase(b, 0, 1).XXPhase(a, 0, 1) to_phase_gates = RebaseCustom( From a2d906ff08dd09db7b62ae500f48df3456fbe52a Mon Sep 17 00:00:00 2001 From: sjdilkes Date: Tue, 5 Sep 2023 15:29:35 +0100 Subject: [PATCH 6/6] Update predicates_test.py --- pytket/tests/predicates_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytket/tests/predicates_test.py b/pytket/tests/predicates_test.py index 00e41a2840..e5b306d4cf 100644 --- a/pytket/tests/predicates_test.py +++ b/pytket/tests/predicates_test.py @@ -83,8 +83,8 @@ from pytket.transform import Transform, PauliSynthStrat, CXConfigType from pytket.passes import SynthesiseOQC import numpy as np -from sympy import Symbol -from typing import Dict, Any, List, cast +from sympy import Symbol, Expr +from typing import Dict, Any, List, cast, Union from useful_typedefs import ParamType as Param # type: ignore