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

Add kwargs to arguments for Backend.get_compiled_circuit #962

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

Unreleased
----------

Minor new features:

* ``Backend.get_compiled_circuit``, ``Backend.get_compiled_circuits`` and
``Backend.default_compilation_pass`` can now take ``kwargs``. Supported
``kwargs`` are specific to child ``Backend`` classes.

1.18.0 (August 2023)
--------------------

Expand Down
30 changes: 20 additions & 10 deletions pytket/pytket/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def valid_circuit(self, circuit: Circuit) -> bool:
def _check_all_circuits(
self, circuits: Iterable[Circuit], nomeasure_warn: Optional[bool] = None
) -> bool:

if nomeasure_warn is None:
nomeasure_warn = not (
self._supports_state
Expand Down Expand Up @@ -144,7 +143,9 @@ def rebase_pass(self) -> BasePass:
...

@abstractmethod
def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
def default_compilation_pass(
self, optimisation_level: int = 2, **kwargs: KwargTypes
) -> BasePass:
"""
A suggested compilation pass that will will, if possible, produce an equivalent
circuit suitable for running on this backend.
Expand All @@ -154,7 +155,7 @@ def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
higher optimisation levels, further optimisations may be applied.

This is a an abstract method which is implemented in the backend itself, and so
is tailored to the backend's requirements.
is tailored to the backend's requirements. Valid kwargs are Backend specific.

:param optimisation_level: The level of optimisation to perform during
compilation.
Expand All @@ -172,18 +173,24 @@ def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
...

def get_compiled_circuit(
self, circuit: Circuit, optimisation_level: int = 2
self, circuit: Circuit, optimisation_level: int = 2, **kwargs: KwargTypes
) -> Circuit:
"""
Return a single circuit compiled with :py:meth:`default_compilation_pass`. See
:py:meth:`Backend.get_compiled_circuits`.
Return a single circuit compiled with :py:meth:`default_compilation_pass` See
:py:meth:`Backend.get_compiled_circuits`. Valid kwargs are Backend specific.

"""
return_circuit = circuit.copy()
self.default_compilation_pass(optimisation_level).apply(return_circuit)
self.default_compilation_pass(optimisation_level, **kwargs).apply(
return_circuit
)
return return_circuit

def get_compiled_circuits(
self, circuits: Sequence[Circuit], optimisation_level: int = 2
self,
circuits: Sequence[Circuit],
optimisation_level: int = 2,
**kwargs: KwargTypes,
) -> List[Circuit]:
"""Compile a sequence of circuits with :py:meth:`default_compilation_pass`
and return the list of compiled circuits (does not act in place).
Expand All @@ -204,6 +211,8 @@ def get_compiled_circuits(
backend, and running the :py:meth:`verify` method on each in turn with your
circuit.

Valid kwargs are backend specific.

:param circuits: The circuits to compile.
:type circuit: Sequence[Circuit]
:param optimisation_level: The level of optimisation to perform during
Expand All @@ -213,7 +222,9 @@ def get_compiled_circuits(
:return: Compiled circuits.
:rtype: List[Circuit]
"""
return [self.get_compiled_circuit(c, optimisation_level) for c in circuits]
return [
self.get_compiled_circuit(c, optimisation_level, **kwargs) for c in circuits
]

@property
@abstractmethod
Expand Down Expand Up @@ -265,7 +276,6 @@ def process_circuits(
valid_check: bool = True,
**kwargs: KwargTypes,
) -> List[ResultHandle]:

"""
Submit circuits to the backend for running. The results will be stored
in the backend's result cache to be retrieved by the corresponding
Expand Down
8 changes: 6 additions & 2 deletions pytket/tests/simulator/tket_sim_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def required_predicates(self) -> List[Predicate]:
def rebase_pass(self) -> BasePass:
return auto_rebase_pass(_GATE_SET)

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
def default_compilation_pass(
self, optimisation_level: int = 1, **kwargs: KwargTypes
) -> BasePass:
assert optimisation_level in range(3)
if optimisation_level == 0:
return SequencePass([DecomposeBoxes(), self.rebase_pass()])
Expand Down Expand Up @@ -155,7 +157,9 @@ class TketSimShotBackend(TketSimBackend):
_supports_state = False
_supports_contextual_optimisation = True

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
def default_compilation_pass(
self, optimisation_level: int = 1, **kwargs: KwargTypes
) -> BasePass:
assert optimisation_level in range(3)
if optimisation_level == 0:
return SequencePass([DecomposeBoxes(), self.rebase_pass()])
Expand Down
Loading