Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor auto rebase and auto squash #1410

Merged
merged 25 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
65ab75e
Implement python rebase functions in C++
yao-cqc May 21, 2024
12b0852
Implement auto_rebase in c++
yao-cqc May 22, 2024
d6b79ee
Implement auto_squash in c++
yao-cqc May 22, 2024
8167b60
Test exception handling
yao-cqc May 23, 2024
5a451b3
Add AutoRebase, AutoSquash to binder
yao-cqc May 23, 2024
8a6239c
Update circuit_library
yao-cqc May 23, 2024
143a55f
Update json schema
yao-cqc May 23, 2024
f36819b
deprecate auto_rebase and auto_squash
yao-cqc May 23, 2024
300ebf5
Fix bug
yao-cqc May 23, 2024
982753a
Replace auto_rebase_pass and auto_squash_pass with AutoRebase and Aut…
yao-cqc May 23, 2024
b3cb0c2
Remove auto_rebase_pass and auto_squash_pass from docs
yao-cqc May 23, 2024
dc21d09
Merge branch 'main' into refactor-auto_rebase-and-auto_squash
yao-cqc May 23, 2024
ee735b9
Bump tket version
yao-cqc May 23, 2024
13df986
Remove extra lines in docs
yao-cqc May 23, 2024
ddf7b1c
Regenerate stubs
yao-cqc May 23, 2024
3cac1c2
Add changelog entry
yao-cqc May 23, 2024
ad383d1
Fix linting errors
yao-cqc May 23, 2024
fbe6a47
Sorted gate sets in serialisation
yao-cqc May 28, 2024
1a0a055
Better deprecation handling
yao-cqc May 28, 2024
c7ebd26
Add docs for added CircPool functions
yao-cqc May 28, 2024
5e163d1
Add comment to hard-to-understand test
yao-cqc May 28, 2024
a8f84c3
Fix typo in json schema
yao-cqc May 28, 2024
e4702ed
Replace `find()` with `contains()`
yao-cqc May 28, 2024
c4725b8
Set `always_squash_symbols` to false when squash `Rz` and `PhasedX`
yao-cqc May 28, 2024
aa52d46
Merge branch 'main' into refactor-auto_rebase-and-auto_squash
yao-cqc May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pytket/binders/circuit_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ PYBIND11_MODULE(circuit_library, library_m) {
"TK2_using_TK2_or_swap", &CircPool::TK2_using_TK2_or_swap,
"Either the exact TK2, or a wire swap encoded in the implicit qubit "
"permutation of the Circuit and single qubit gates.");
library_m.def(
"TK2_using_TK2", &CircPool::TK2_using_TK2,
"A circuit of a single TK2 gate with given parameters");
library_m.def(
"approx_TK2_using_1xZZPhase", &CircPool::approx_TK2_using_1xZZPhase,
"Approximate equivalent to TK2, using 1 ZZPhase gate and single-qubit "
Expand Down Expand Up @@ -285,15 +288,25 @@ PYBIND11_MODULE(circuit_library, library_m) {
library_m.def(
"TK1_to_RzRx", &CircPool::tk1_to_rzrx,
"A tk1 equivalent circuit given tk1 parameters in terms of Rz, Rx");
library_m.def(
"TK1_to_RxRy", &CircPool::tk1_to_rxry,
"A tk1 equivalent circuit given tk1 parameters in terms of Rx, Ry");
library_m.def(
"TK1_to_RzH", &CircPool::tk1_to_rzh,
"A tk1 equivalent circuit given tk1 parameters in terms of Rz, H");
library_m.def(
"TK1_to_RzSX", &CircPool::tk1_to_rzsx,
"A tk1 equivalent circuit given tk1 parameters in terms of Rz, Sx");
library_m.def(
"TK1_to_RzXSX", &CircPool::tk1_to_rzxsx,
"A tk1 equivalent circuit given tk1 parameters in terms of Rz, X, Sx");
library_m.def(
"TK1_to_TK1", &CircPool::tk1_to_tk1,
"A circuit of a single tk1 gate with given parameters");
library_m.def(
"TK1_to_U3", &CircPool::tk1_to_u3,
"A tk1 equivalent circuit given tk1 parameters in terms of U3 and global "
"phase");
library_m.def(
"Rx_using_GPI", &CircPool::Rx_using_GPI,
"Equivalent to Rx, using GPI and GPI2 gates");
Expand Down
27 changes: 26 additions & 1 deletion pytket/binders/passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ PYBIND11_MODULE(passes, m) {
"symbolic complexity.",
py::arg("singleqs"), py::arg("tk1_replacement"),
py::arg("always_squash_symbols") = false);
m.def(
"AutoSquash", &gen_auto_squash_pass,
"Attempt to generate a squash pass automatically for the given target "
"single qubit gateset.\n"
"Raises an error if no known TK1 decomposition can be found based on the "
"given gateset, in which case try using :py:class:`SquashCustom` with "
"your own decomposition."
"\n\n:param singleqs: The types of single qubit gates in the target "
"gate set. This pass will only affect sequences of gates that are "
"already in this set.",
py::arg("singleqs"));
m.def(
"DelayMeasures", &DelayMeasures,
"Commutes Measure operations to the end of the circuit. Throws an "
Expand Down Expand Up @@ -660,7 +671,21 @@ PYBIND11_MODULE(passes, m) {
"conditional and phase operations, and Measure, Reset and Collapse)",
py::arg("gateset"), py::arg("tk2_replacement"),
py::arg("tk1_replacement"));

m.def(
"AutoRebase", &gen_auto_rebase_pass,
"Attempt to generate a rebase pass automatically for the given target "
"gateset. Checks if there are known existing decompositions "
"to target gateset and TK1 to target gateset and uses those to construct "
"a custom rebase.\n"
"Raises an error if no known decompositions can be found, in which case "
"try using :py:class:`RebaseCustom` with your own decompositions.\n\n"
":param gateset: Set of supported OpTypes, target gate set. "
"(in addition, Measure, Reset and Collapse operations are always allowed "
"and are left alone; conditional operations may be present; and Phase "
"gates may also be introduced by the rebase)\n"
":param allow_swaps: Whether to allow implicit wire swaps. Default to "
"False.",
py::arg("gateset"), py::arg("allow_swaps") = false);
m.def(
"EulerAngleReduction", &gen_euler_pass,
"Uses Euler angle decompositions to squash all chains of P and Q "
Expand Down
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.3.1@tket/stable")
self.requires("tket/1.3.2@tket/stable")
self.requires("tklog/0.3.3@tket/stable")
self.requires("tkrng/0.3.3@tket/stable")
self.requires("tkassert/0.3.4@tket/stable")
Expand Down
2 changes: 2 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Unreleased
Features:

* Add ``OpType.CnRx`` and ``OpType.CnRz``.
* Add ``AutoRebase`` and ``AutoSquash`` passes.
Deprecate ``auto_rebase_pass`` and ``auto_squash_pass``.

Fixes:

Expand Down
3 changes: 1 addition & 2 deletions pytket/docs/passes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ pytket.passes.script
pytket.passes.auto_rebase
cqc-alec marked this conversation as resolved.
Show resolved Hide resolved
~~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: pytket.passes.auto_rebase
:members: auto_rebase_pass, auto_squash_pass

:members: auto_rebase_pass, auto_squash_pass
18 changes: 17 additions & 1 deletion pytket/pytket/_tket/circuit_library.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from __future__ import annotations
import pytket._tket.circuit
import sympy
import typing
__all__ = ['BRIDGE', 'BRIDGE_using_CX_0', 'BRIDGE_using_CX_1', 'C3X_normal_decomp', 'C4X_normal_decomp', 'CCX', 'CCX_modulo_phase_shift', 'CCX_normal_decomp', 'CH_using_CX', 'CRx_using_CX', 'CRx_using_TK2', 'CRy_using_CX', 'CRy_using_TK2', 'CRz_using_CX', 'CRz_using_TK2', 'CSWAP_using_CX', 'CSX_using_CX', 'CSXdg_using_CX', 'CS_using_CX', 'CSdg_using_CX', 'CU1_using_CX', 'CU1_using_TK2', 'CU3_using_CX', 'CV_using_CX', 'CVdg_using_CX', 'CX', 'CX_S_CX_reduced', 'CX_S_V_XC_reduced', 'CX_VS_CX_reduced', 'CX_V_CX_reduced', 'CX_V_S_XC_reduced', 'CX_XC_reduced', 'CX_using_AAMS', 'CX_using_ECR', 'CX_using_TK2', 'CX_using_XXPhase_0', 'CX_using_XXPhase_1', 'CX_using_ZZMax', 'CX_using_ZZPhase', 'CX_using_flipped_CX', 'CY_using_CX', 'CZ_using_CX', 'ECR_using_CX', 'ESWAP_using_CX', 'ESWAP_using_TK2', 'FSim_using_CX', 'FSim_using_TK2', 'H_CZ_H', 'ISWAP_using_CX', 'ISWAP_using_TK2', 'NPhasedX_using_PhasedX', 'PhasedISWAP_using_CX', 'PhasedISWAP_using_TK2', 'Rx_using_GPI', 'Ry_using_GPI', 'Rz_using_GPI', 'SWAP_using_CX_0', 'SWAP_using_CX_1', 'TK1_to_PhasedXRz', 'TK1_to_RzH', 'TK1_to_RzRx', 'TK1_to_RzSX', 'TK1_to_TK1', 'TK1_using_GPI', 'TK2_using_3xCX', 'TK2_using_AAMS', 'TK2_using_CX', 'TK2_using_CX_and_swap', 'TK2_using_TK2_or_swap', 'TK2_using_ZZMax', 'TK2_using_ZZMax_and_swap', 'TK2_using_ZZPhase', 'TK2_using_ZZPhase_and_swap', 'TK2_using_normalised_TK2', 'X', 'X1_CX', 'XXPhase3_using_CX', 'XXPhase3_using_TK2', 'XXPhase_using_AAMS', 'XXPhase_using_CX', 'XXPhase_using_TK2', 'YYPhase_using_AAMS', 'YYPhase_using_CX', 'YYPhase_using_TK2', 'Z0_CX', 'ZZMax_using_CX', 'ZZPhase_using_AAMS', 'ZZPhase_using_CX', 'ZZPhase_using_TK2', 'approx_TK2_using_1xCX', 'approx_TK2_using_1xZZPhase', 'approx_TK2_using_2xCX', 'approx_TK2_using_2xZZPhase', 'ladder_down', 'ladder_down_2', 'ladder_up']
__all__ = ['BRIDGE', 'BRIDGE_using_CX_0', 'BRIDGE_using_CX_1', 'C3X_normal_decomp', 'C4X_normal_decomp', 'CCX', 'CCX_modulo_phase_shift', 'CCX_normal_decomp', 'CH_using_CX', 'CRx_using_CX', 'CRx_using_TK2', 'CRy_using_CX', 'CRy_using_TK2', 'CRz_using_CX', 'CRz_using_TK2', 'CSWAP_using_CX', 'CSX_using_CX', 'CSXdg_using_CX', 'CS_using_CX', 'CSdg_using_CX', 'CU1_using_CX', 'CU1_using_TK2', 'CU3_using_CX', 'CV_using_CX', 'CVdg_using_CX', 'CX', 'CX_S_CX_reduced', 'CX_S_V_XC_reduced', 'CX_VS_CX_reduced', 'CX_V_CX_reduced', 'CX_V_S_XC_reduced', 'CX_XC_reduced', 'CX_using_AAMS', 'CX_using_ECR', 'CX_using_TK2', 'CX_using_XXPhase_0', 'CX_using_XXPhase_1', 'CX_using_ZZMax', 'CX_using_ZZPhase', 'CX_using_flipped_CX', 'CY_using_CX', 'CZ_using_CX', 'ECR_using_CX', 'ESWAP_using_CX', 'ESWAP_using_TK2', 'FSim_using_CX', 'FSim_using_TK2', 'H_CZ_H', 'ISWAP_using_CX', 'ISWAP_using_TK2', 'NPhasedX_using_PhasedX', 'PhasedISWAP_using_CX', 'PhasedISWAP_using_TK2', 'Rx_using_GPI', 'Ry_using_GPI', 'Rz_using_GPI', 'SWAP_using_CX_0', 'SWAP_using_CX_1', 'TK1_to_PhasedXRz', 'TK1_to_RxRy', 'TK1_to_RzH', 'TK1_to_RzRx', 'TK1_to_RzSX', 'TK1_to_RzXSX', 'TK1_to_TK1', 'TK1_to_U3', 'TK1_using_GPI', 'TK2_using_3xCX', 'TK2_using_AAMS', 'TK2_using_CX', 'TK2_using_CX_and_swap', 'TK2_using_TK2', 'TK2_using_TK2_or_swap', 'TK2_using_ZZMax', 'TK2_using_ZZMax_and_swap', 'TK2_using_ZZPhase', 'TK2_using_ZZPhase_and_swap', 'TK2_using_normalised_TK2', 'X', 'X1_CX', 'XXPhase3_using_CX', 'XXPhase3_using_TK2', 'XXPhase_using_AAMS', 'XXPhase_using_CX', 'XXPhase_using_TK2', 'YYPhase_using_AAMS', 'YYPhase_using_CX', 'YYPhase_using_TK2', 'Z0_CX', 'ZZMax_using_CX', 'ZZPhase_using_AAMS', 'ZZPhase_using_CX', 'ZZPhase_using_TK2', 'approx_TK2_using_1xCX', 'approx_TK2_using_1xZZPhase', 'approx_TK2_using_2xCX', 'approx_TK2_using_2xZZPhase', 'ladder_down', 'ladder_down_2', 'ladder_up']
def BRIDGE() -> pytket._tket.circuit.Circuit:
"""
Just a BRIDGE[0,1,2] gate
Expand Down Expand Up @@ -239,6 +239,10 @@ def TK1_to_PhasedXRz(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: s
"""
A tk1 equivalent circuit given tk1 parameters in terms of PhasedX, Rz
"""
def TK1_to_RxRy(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A tk1 equivalent circuit given tk1 parameters in terms of Rx, Ry
"""
def TK1_to_RzH(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A tk1 equivalent circuit given tk1 parameters in terms of Rz, H
Expand All @@ -251,10 +255,18 @@ def TK1_to_RzSX(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.
"""
A tk1 equivalent circuit given tk1 parameters in terms of Rz, Sx
"""
def TK1_to_RzXSX(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A tk1 equivalent circuit given tk1 parameters in terms of Rz, X, Sx
"""
def TK1_to_TK1(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A circuit of a single tk1 gate with given parameters
"""
def TK1_to_U3(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A tk1 equivalent circuit given tk1 parameters in terms of U3 and global phase
"""
def TK1_using_GPI(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
Equivalent to TK1, using GPI and GPI2 gates
Expand All @@ -281,6 +293,10 @@ def TK2_using_CX_and_swap(arg0: sympy.Expr | float, arg1: sympy.Expr | float, ar

The decomposition minimizes the number of CX gates.
"""
def TK2_using_TK2(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
A circuit of a single TK2 gate with given parameters
"""
def TK2_using_TK2_or_swap(arg0: sympy.Expr | float, arg1: sympy.Expr | float, arg2: sympy.Expr | float) -> pytket._tket.circuit.Circuit:
"""
Either the exact TK2, or a wire swap encoded in the implicit qubit permutation of the Circuit and single qubit gates.
Expand Down
17 changes: 16 additions & 1 deletion pytket/pytket/_tket/passes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pytket._tket.transform
import pytket._tket.unit_id
import sympy
import typing
__all__ = ['AASRouting', 'Audit', 'BasePass', 'CNotSynthType', 'CXMappingPass', 'CliffordPushThroughMeasures', 'CliffordResynthesis', 'CliffordSimp', 'CnXPairwiseDecomposition', 'CommuteThroughMultis', 'ComposePhasePolyBoxes', 'ContextSimp', 'CustomPass', 'CustomRoutingPass', 'DecomposeArbitrarilyControlledGates', 'DecomposeBoxes', 'DecomposeClassicalExp', 'DecomposeMultiQubitsCX', 'DecomposeSingleQubitsTK1', 'DecomposeSwapsToCXs', 'DecomposeSwapsToCircuit', 'DecomposeTK2', 'Default', 'DefaultMappingPass', 'DelayMeasures', 'EulerAngleReduction', 'FlattenRegisters', 'FlattenRelabelRegistersPass', 'FullMappingPass', 'FullPeepholeOptimise', 'GlobalisePhasedX', 'GreedyPauliSimp', 'GuidedPauliSimp', 'HamPath', 'KAKDecomposition', 'NaivePlacementPass', 'NormaliseTK2', 'OptimisePhaseGadgets', 'PauliExponentials', 'PauliSimp', 'PauliSquash', 'PeepholeOptimise2Q', 'PlacementPass', 'RebaseCustom', 'RebaseTket', 'Rec', 'RemoveBarriers', 'RemoveDiscarded', 'RemoveImplicitQubitPermutation', 'RemoveRedundancies', 'RenameQubitsPass', 'RepeatPass', 'RepeatUntilSatisfiedPass', 'RepeatWithMetricPass', 'RoundAngles', 'RoutingPass', 'SWAP', 'SafetyMode', 'SequencePass', 'SimplifyInitial', 'SimplifyMeasured', 'SquashCustom', 'SquashRzPhasedX', 'SquashTK1', 'SynthesiseOQC', 'SynthesiseTK', 'SynthesiseTket', 'SynthesiseUMD', 'ThreeQubitSquash', 'ZXGraphlikeOptimisation', 'ZZPhaseToRz']
__all__ = ['AASRouting', 'Audit', 'AutoRebase', 'AutoSquash', 'BasePass', 'CNotSynthType', 'CXMappingPass', 'CliffordPushThroughMeasures', 'CliffordResynthesis', 'CliffordSimp', 'CnXPairwiseDecomposition', 'CommuteThroughMultis', 'ComposePhasePolyBoxes', 'ContextSimp', 'CustomPass', 'CustomRoutingPass', 'DecomposeArbitrarilyControlledGates', 'DecomposeBoxes', 'DecomposeClassicalExp', 'DecomposeMultiQubitsCX', 'DecomposeSingleQubitsTK1', 'DecomposeSwapsToCXs', 'DecomposeSwapsToCircuit', 'DecomposeTK2', 'Default', 'DefaultMappingPass', 'DelayMeasures', 'EulerAngleReduction', 'FlattenRegisters', 'FlattenRelabelRegistersPass', 'FullMappingPass', 'FullPeepholeOptimise', 'GlobalisePhasedX', 'GreedyPauliSimp', 'GuidedPauliSimp', 'HamPath', 'KAKDecomposition', 'NaivePlacementPass', 'NormaliseTK2', 'OptimisePhaseGadgets', 'PauliExponentials', 'PauliSimp', 'PauliSquash', 'PeepholeOptimise2Q', 'PlacementPass', 'RebaseCustom', 'RebaseTket', 'Rec', 'RemoveBarriers', 'RemoveDiscarded', 'RemoveImplicitQubitPermutation', 'RemoveRedundancies', 'RenameQubitsPass', 'RepeatPass', 'RepeatUntilSatisfiedPass', 'RepeatWithMetricPass', 'RoundAngles', 'RoutingPass', 'SWAP', 'SafetyMode', 'SequencePass', 'SimplifyInitial', 'SimplifyMeasured', 'SquashCustom', 'SquashRzPhasedX', 'SquashTK1', 'SynthesiseOQC', 'SynthesiseTK', 'SynthesiseTket', 'SynthesiseUMD', 'ThreeQubitSquash', 'ZXGraphlikeOptimisation', 'ZZPhaseToRz']
class BasePass:
"""
Base class for passes.
Expand Down Expand Up @@ -219,6 +219,21 @@ def AASRouting(arc: pytket._tket.architecture.Architecture, **kwargs: Any) -> Ba
:param \\**kwargs: parameters for routing (described above)
:return: a pass to perform the remapping
"""
def AutoRebase(gateset: set[pytket._tket.circuit.OpType], allow_swaps: bool = False) -> BasePass:
"""
Attempt to generate a rebase pass automatically for the given target gateset. Checks if there are known existing decompositions to target gateset and TK1 to target gateset and uses those to construct a custom rebase.
Raises an error if no known decompositions can be found, in which case try using :py:class:`RebaseCustom` with your own decompositions.

:param gateset: Set of supported OpTypes, target gate set. (in addition, Measure, Reset and Collapse operations are always allowed and are left alone; conditional operations may be present; and Phase gates may also be introduced by the rebase)
:param allow_swaps: Whether to allow implicit wire swaps. Default to False.
"""
def AutoSquash(singleqs: set[pytket._tket.circuit.OpType]) -> BasePass:
"""
Attempt to generate a squash pass automatically for the given target single qubit gateset.
Raises an error if no known TK1 decomposition can be found based on the given gateset, in which case try using :py:class:`SquashCustom` with your own decomposition.

:param singleqs: The types of single qubit gates in the target gate set. This pass will only affect sequences of gates that are already in this set.
"""
def CXMappingPass(arc: pytket._tket.architecture.Architecture, placer: pytket._tket.placement.Placement, **kwargs: Any) -> BasePass:
"""
Construct a pass to convert all gates to CX, relabel :py:class:`Circuit` Qubits to :py:class:`Architecture` Nodes, route to the connectivty graph of a :py:class:`Architecture` and decompose additional routing gates (SWAP and BRIDGE) to CX gates.
Expand Down
121 changes: 0 additions & 121 deletions pytket/pytket/passes/_decompositions.py

This file was deleted.

Loading
Loading