-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] auto_rebase_pass function for attempting automatic rebase p…
…asses
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2019-2022 Cambridge Quantum Computing | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Union, Callable, Dict, FrozenSet, TYPE_CHECKING | ||
from pytket.circuit import Circuit, OpType, pool # type: ignore | ||
from pytket.passes import RebaseCustom # type: ignore | ||
|
||
if TYPE_CHECKING: | ||
from sympy import Expr # type: ignore | ||
|
||
|
||
class NoAutoRebase(Exception): | ||
"""Automatic rebase could not be found""" | ||
|
||
|
||
_CX_CIRCS: Dict[OpType, Callable[[], "Circuit"]] = { | ||
OpType.CX: pool.CX, | ||
OpType.ZZMax: pool.CX_using_ZZMax, | ||
OpType.XXPhase: pool.CX_using_XXPhase_0, | ||
OpType.ECR: pool.CX_using_ECR, | ||
OpType.CZ: lambda: Circuit(2).H(1).CZ(0, 1).H(1), | ||
} | ||
|
||
Param = Union[str, "Expr"] | ||
|
||
_tk1_circs: Dict[FrozenSet[OpType], Callable[[Param, Param, Param], "Circuit"]] = { | ||
frozenset({OpType.TK1}): pool.tk1_to_tk1, | ||
frozenset({OpType.PhasedX, OpType.Rz}): pool.tk1_to_PhasedXRz, | ||
frozenset({OpType.Rx, OpType.Rz}): pool.tk1_to_rzrx, | ||
frozenset({OpType.Rz, OpType.H}): pool.tk1_to_rzh, | ||
frozenset({OpType.Rz, OpType.SX}): pool.tk1_to_rzsx, | ||
} | ||
|
||
|
||
def auto_rebase_pass(gateset: FrozenSet[OpType]) -> RebaseCustom: | ||
"""Attempt to generate a rebase pass automatically for the given target | ||
gateset. | ||
Checks if there are known existing decompositions from CX | ||
to target gateset and TK1 to target gateset and uses thsoe to construct a | ||
CustomRebase. | ||
Raises an error if known decompositions can be found, in which case try | ||
using RebaseCustom with your own decompositions. | ||
:param gateset: Set of supported OpTypes, target gate set | ||
:type gateset: FrozenSet[OpType] | ||
:raises NoAutoRebase: No suitable CX or TK1 decomposition found. | ||
:return: Rebase pass. | ||
:rtype: RebaseCustom | ||
""" | ||
if any((matching := k) in gateset for k in _CX_CIRCS): | ||
cx_circ = _CX_CIRCS[matching]() | ||
else: | ||
raise NoAutoRebase("No known decomposition from CX to available gateset.") | ||
|
||
if any((matching := k).issubset(gateset) for k in _tk1_circs): | ||
tk1_func = _tk1_circs[matching] | ||
else: | ||
raise NoAutoRebase("No known decomposition from TK1 to available gateset.") | ||
|
||
return RebaseCustom(set(gateset), cx_circ, tk1_func) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters