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 cirq.optimize_for_target_gateset transformer and cirq.CompilationTargetGateset interface #5005

Merged
merged 7 commits into from
Feb 18, 2022

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Feb 18, 2022

  • Adds the following new objects:
    • cirq.CompilationTargetGateset: Interface for defining compilation targets.
    • _decompose_operations_to_target_gateset : Transformer to locally convert each operation to target gateset.
    • cirq.optimize_for_target_gateset: Transformer to convert a circuit to a target gateset -- also supports running pre and post processing transformers before decomposing individual operations.
  • See Add cirq.convert_to_target_gateset and implementations of CZ, SqrtIswap target gatesets. #5003 for larger context and implementations of specific target gatesets deriving from cirq.CompilationTargetGateset.

cc @MichaelBroughton @dstrain115

@tanujkhattar tanujkhattar requested review from a team, vtomole and cduck as code owners February 18, 2022 01:23
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Feb 18, 2022
Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small nits and then a few high level questions. My biggest concern is if we should ship something in decompose_operations_to_target_gateset that looks so similar to decompose. My thinking is that less is more and it's probably best if we don't give users too many ways to do the same thing.

cirq-core/cirq/transformers/convert_to_target_gateset.py Outdated Show resolved Hide resolved
cirq-core/cirq/transformers/convert_to_target_gateset.py Outdated Show resolved Hide resolved
Comment on lines 33 to 40
def decompose_operations_to_target_gateset(
circuit: 'cirq.AbstractCircuit',
*,
context: Optional['cirq.TransformerContext'] = None,
gateset: Optional['cirq.Gateset'] = None,
decomposer: Callable[['cirq.Operation', int], DecomposeResult] = lambda *_: NotImplemented,
ignore_failures=True,
) -> 'cirq.Circuit':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this transformer ? It seems like a user should be able to fairly easily accomplish this same thing by just calling into the regular decompose. Having this as a thin wrapper over decompose seems like it might be complicating things for the users.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replied in the main comment. Tl;Dr is that having benefits of making it a transformer, like automated logging + no-compile tags, make it worth it. Plus we have other similar wrappers like expand_composite.

Comment on lines +53 to +56
@property
@abc.abstractmethod
def num_qubits(self) -> int:
"""Maximum number of qubits on which a gate from this gateset can act upon."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be defined here as something like:

return max(g.num_qubits() for g in self.gates)

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gateset is composed of gate families, and gate families don't have a num_qubits parameter. For example, it's possible to create gate families which can accept any controlled single qubit rotation with one or more controls. A num_qubits parameter cannot be precisely defined in this case.

Enforcing num_qubits on gate families would be a much broader change without significant benefit outside the scope of compilation target gatesets.

Comment on lines +47 to +50
"""Abstract base class to create gatesets that can be used as targets for compilation.

An instance of this type can be passed to transformers like `cirq.convert_to_target_gateset`,
which can transform any given circuit to contain gates accepted by this gateset.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class seems important enough for users that it might be good to give a snippet of a simple implementation in the docstring. WDYT ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concrete implementations will be added in the follow-up PR. Maybe we can then update the docstrings here to include a hyper-linked example to implementation? Something like

"Please see cirq.CZTargetGateset for a concrete implementation." ?

@tanujkhattar
Copy link
Collaborator Author

My biggest concern is if we should ship something in decompose_operations_to_target_gateset that looks so similar to decompose.

The primary benefit here is the automated logging + support for no-compile tags, that comes with making it a transformer.
For example: If someone runs convert_to_target_gateset with a context and then calls context.logger.show(), they'd be able to clearly inspect the action of each stage, including the decomposition stage wrapped in decompose_operations_to_target_gateset, because of the automated logging framework.

Given this benefit, and given that we already have other transformers like expand_composite, which are essentially wrappers on top of decompose + map_operations, I'd argue that we should keep this decompose_operations_to_target_gateset as well.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The closeness of decompose_operations_to_target_gateset to decompose still worries me a bit. Could we do something like:

decompose_operationst_to_target_gateset -> convert_to_gateset
convert_to_target_gateset -> fuse_and_convert_to_gateset

Just to avoid the overlap with decompose and in the case of fuse_and_convert_to_gateset communicate the fact that it won't be just a simple conversion. WDYT ?

cirq-core/cirq/transformers/convert_to_target_gateset.py Outdated Show resolved Hide resolved
@tanujkhattar
Copy link
Collaborator Author

As discussed offline,

  • Changed decompose_operations_to_target_gateset --> _decompose_operations_to_target_gateset to avoid polluting the global namespace.
  • Changed convert_to_target_gateset --> optimize_for_target_gateset to convey that other pre/post process transformers will also be executed.

@tanujkhattar tanujkhattar merged commit 59b72a1 into quantumlib:master Feb 18, 2022
@tanujkhattar tanujkhattar deleted the compilation_target branch February 18, 2022 20:58
@tanujkhattar tanujkhattar changed the title Add cirq.convert_to_target_gateset transformer and cirq.CompilationTargetGateset interface Add cirq.optimize_for_target_gateset transformer and cirq.CompilationTargetGateset interface Feb 18, 2022
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
…nTargetGateset` interface (quantumlib#5005)

* Add convert_to_target_gateset transformer and CompilationTargetGateset interface

* Override validation_operation to not accept intermediate results
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…nTargetGateset` interface (quantumlib#5005)

* Add convert_to_target_gateset transformer and CompilationTargetGateset interface

* Override validation_operation to not accept intermediate results
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…nTargetGateset` interface (quantumlib#5005)

* Add convert_to_target_gateset transformer and CompilationTargetGateset interface

* Override validation_operation to not accept intermediate results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants