Skip to content

Commit

Permalink
Fix RebaseCustom for TK2 gates (#1003)
Browse files Browse the repository at this point in the history
* Fix RebaseCustom

* bump

* Remove leftover comments

* Update test to check types

* Update predicates_test.py

* Update predicates_test.py
  • Loading branch information
sjdilkes authored Sep 5, 2023
1 parent 186c031 commit 495626f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pytket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
39 changes: 37 additions & 2 deletions pytket/tests/predicates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -902,6 +902,40 @@ 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: 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: 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(
{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)
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__":
test_predicate_generation()
test_compilation_unit_generation()
Expand All @@ -919,3 +953,4 @@ def test_PeepholeOptimise2Q() -> None:
test_RebaseOQC_and_SynthesiseOQC()
test_ZZPhaseToRz()
test_flatten_relabel_pass()
test_rebase_custom_tk2()
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.2.37"
version = "1.2.38"
package_type = "library"
license = "Apache 2"
homepage = "https://github.com/CQCL/tket"
Expand Down
4 changes: 1 addition & 3 deletions tket/src/Transformations/Rebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ 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
Circuit replacement = TK2_circ_from_multiq(op);
// Find replacement Circuit for all TK2 gates
Expand Down

0 comments on commit 495626f

Please sign in to comment.