Skip to content

Commit

Permalink
[feature] add auto_squash_pass in vein of auto_rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Feb 17, 2022
1 parent beabaaf commit ae90705
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ API changes:
Minor new features:

* Add ``delay_measures`` option to ``DefaultMappingPass``.
* New ``pytket.passes.auto_rebase_pass`` which attempts to construct a rebase pass given a target gate set from known decompositions.
* New ``pytket.passes.auto_rebase_pass`` and ``pytket.passes.auto_squash_pass``
which attempt to construct rebase and squash passess given a target gate set from known
decompositions.

0.19.1 (February 2022)
----------------------
Expand Down
2 changes: 1 addition & 1 deletion pytket/pytket/passes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
from pytket._tket.passes import * # type: ignore

from .script import compilation_pass_from_script, compilation_pass_grammar
from .auto_rebase import auto_rebase_pass
from .auto_rebase import auto_rebase_pass, auto_squash_pass
12 changes: 12 additions & 0 deletions pytket/pytket/passes/auto_rebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ def auto_rebase_pass(gateset: Set[OpType]) -> RebaseCustom:
return RebaseCustom(
gateset, get_cx_decomposition(gateset), get_TK1_decomposition_function(gateset)
)


def auto_squash_pass(gateset: Set[OpType]) -> SquashCustom:
"""Attempt to generate a squash pass automatically for the given target
single qubit gateset.
:param gateset: Available single qubit gateset
:type gateset: Set[OpType]
:return: Squash to target gateset
:rtype: SquashCustom
"""
return SquashCustom(gateset, get_TK1_decomposition_function(gateset))
53 changes: 51 additions & 2 deletions pytket/tests/transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import itertools
from pathlib import Path
from typing import List
from pytket.circuit import Circuit, OpType, PauliExpBox # type: ignore
from pytket._tket.circuit import _library # type: ignore
from pytket.pauli import Pauli # type: ignore
from pytket.passes import RemoveRedundancies, KAKDecomposition, ThreeQubitSquash, CommuteThroughMultis, PauliSquash, FullPeepholeOptimise, GlobalisePhasedX, RebaseCustom # type: ignore
from pytket.passes import auto_rebase_pass
from pytket.passes import RemoveRedundancies, KAKDecomposition, SquashCustom, CommuteThroughMultis, PauliSquash, FullPeepholeOptimise, GlobalisePhasedX, RebaseCustom # type: ignore
from pytket.passes import auto_rebase_pass, auto_squash_pass
from pytket.passes.auto_rebase import _CX_CIRCS, NoAutoRebase
from pytket.predicates import CompilationUnit # type: ignore
from pytket.transform import Transform, CXConfigType, PauliSynthStrat # type: ignore
Expand Down Expand Up @@ -780,6 +782,53 @@ def test_auto_rebase() -> None:
assert "TK1" in str(cx_err.value)


def test_auto_squash() -> None:
pass_params = [
({OpType.Rz, OpType.Rx}, _library._TK1_to_RzRx),
(
{OpType.Rz, OpType.SX},
_library._TK1_to_RzSX,
),
(
{OpType.T, OpType.Rz, OpType.H},
_library._TK1_to_RzH,
),
(
{OpType.T, OpType.Rz, OpType.H},
_library._TK1_to_RzH,
),
(
{OpType.PhasedX, OpType.Rz},
_library._TK1_to_PhasedXRz,
),
(
{OpType.TK1, OpType.U3},
_library._TK1_to_TK1,
),
]

for gateset, TK1_func in pass_params:
circ = Circuit(1)
for gate in itertools.islice(itertools.cycle(gateset), 5):
# make a sequence of 5 gates from gateset to make sure squash does
# something
params: List[float] = []
while True:
try:
circ.add_gate(gate, params, [0])
break
except (RuntimeError, TypeError):
params.append(0.1)
squash = auto_squash_pass(gateset)
assert squash.to_dict() == SquashCustom(gateset, TK1_func).to_dict()

assert squash.apply(circ)

with pytest.raises(NoAutoRebase) as tk_err:
_ = auto_squash_pass({OpType.H, OpType.T})
assert "TK1" in str(tk_err.value)


if __name__ == "__main__":
test_remove_redundancies()
test_reduce_singles()
Expand Down

0 comments on commit ae90705

Please sign in to comment.