-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Make _create_transformer_with_kwargs a public method #5492
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
Changes from all commits
041e974
fa0198a
fe339bc
cedfc1a
42abd79
aca24bf
021ec10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,15 +29,53 @@ | |
| import cirq | ||
|
|
||
|
|
||
| def _create_transformer_with_kwargs(func: 'cirq.TRANSFORMER', **kwargs) -> 'cirq.TRANSFORMER': | ||
| """Hack to capture additional keyword arguments to transformers while preserving mypy type.""" | ||
| def create_transformer_with_kwargs(transformer: 'cirq.TRANSFORMER', **kwargs) -> 'cirq.TRANSFORMER': | ||
| """Method to capture additional keyword arguments to transformers while preserving mypy type. | ||
|
|
||
| Returns a `cirq.TRANSFORMER` which, when called with a circuit and transformer context, is | ||
| equivalent to calling `transformer(circuit, context=context, **kwargs)`. It is often useful to | ||
| capture keyword arguments of a transformer before passing them as an argument to an API that | ||
| expects `cirq.TRANSFORMER`. For example: | ||
|
|
||
| >>> def run_transformers(transformers: List[cirq.TRANSFORMER]): | ||
| >>> for transformer in transformers: | ||
| >>> transformer(circuit, context=context) | ||
| >>> | ||
| >>> transformers: List[cirq.TRANSFORMER] = [] | ||
| >>> transformers.append( | ||
| >>> cirq.create_transformer_with_kwargs( | ||
| >>> cirq.expand_composite, no_decomp=lambda op: cirq.num_qubits(op) <= 2 | ||
| >>> ) | ||
| >>> ) | ||
| >>> transformers.append(cirq.create_transformer_with_kwargs(cirq.merge_k_qubit_unitaries, k=2)) | ||
| >>> run_transformers(transformers) | ||
|
|
||
|
|
||
| Args: | ||
| transformer: A `cirq.TRANSFORMER` for which additional kwargs should be captured. | ||
| **kwargs: The keyword arguments which should be captured and passed to `transformer`. | ||
|
|
||
| Returns: | ||
| A `cirq.TRANSFORMER` method `transformer_with_kwargs`, s.t. executing | ||
| `transformer_with_kwargs(circuit, context=context)` is equivalent to executing | ||
| `transformer(circuit, context=context, **kwargs)`. | ||
|
|
||
| Raises: | ||
| SyntaxError: if **kwargs contain a 'context'. | ||
| """ | ||
| if 'context' in kwargs: | ||
| raise SyntaxError('**kwargs to be captured must not contain `context`.') | ||
|
|
||
| def transformer( | ||
| def transformer_with_kwargs( | ||
| circuit: 'cirq.AbstractCircuit', *, context: Optional['cirq.TransformerContext'] = None | ||
| ) -> 'cirq.AbstractCircuit': | ||
| return func(circuit, context=context, **kwargs) # type: ignore | ||
| # Need to ignore mypy type because `cirq.TRANSFORMER` is a callable protocol which only | ||
| # accepts circuit and context; and doesn't expect additional keyword arguments. Note | ||
| # that transformers with additional keyword arguments with a default value do satisfy the | ||
| # `cirq.TRANSFORMER` API. | ||
| return transformer(circuit, context=context, **kwargs) # type: ignore | ||
tanujkhattar marked this conversation as resolved.
Show resolved
Hide resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the type ignore? I guess it's because In any case, there should be a comment explaining the reason for the ignore and linking to a mypy issue if there is one open.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's right.
This is not possible because function arguments are contravariant, so adding
Added a comment. |
||
|
|
||
| return transformer | ||
| return transformer_with_kwargs | ||
|
|
||
|
|
||
| class CompilationTargetGateset(ops.Gateset, metaclass=abc.ABCMeta): | ||
|
|
@@ -93,11 +131,11 @@ def _intermediate_result_tag(self) -> Hashable: | |
| def preprocess_transformers(self) -> List['cirq.TRANSFORMER']: | ||
| """List of transformers which should be run before decomposing individual operations.""" | ||
| return [ | ||
| _create_transformer_with_kwargs( | ||
| create_transformer_with_kwargs( | ||
| expand_composite.expand_composite, | ||
| no_decomp=lambda op: protocols.num_qubits(op) <= self.num_qubits, | ||
| ), | ||
| _create_transformer_with_kwargs( | ||
| create_transformer_with_kwargs( | ||
| merge_k_qubit_gates.merge_k_qubit_unitaries, | ||
| k=self.num_qubits, | ||
| rewriter=lambda op: op.with_tags(self._intermediate_result_tag), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.