Skip to content

Conversation

tanujkhattar
Copy link
Collaborator

The PR introduces new transformer primitives -- map_operations and map_moments, as proposed in https://tinyurl.com/moment-preserving-transformers.

Part of roadmap item #3237 and #4483

@tanujkhattar tanujkhattar requested review from a team, cduck and vtomole as code owners November 17, 2021 08:11
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Nov 17, 2021
@tanujkhattar tanujkhattar requested a review from dabacon November 17, 2021 08:11
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Nov 17, 2021
@tanujkhattar tanujkhattar changed the title Add map_operations and map_moments transformer primitives Add map_operations and map_moments transformer primitives Nov 17, 2021
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.

A few high level comments before a more detailed review. My main question is if we need the unroll_circuit_op_*** variants and we can't get away with having just the one function with an extra parameter to change the behavior. Maybe something like:

def unroll_circuit_op(circuit, ..., insert_strategy=None):
"""Unroll circuit operations and emplace them in the outer circuit. if insert_strategy=None a greedy frontier insertion is done."""

or something


Args:
circuit: Input circuit to apply the transformations on. The input circuit is not mutated.
map_func: Mapping function from (cirq.Operation, moment_index) to a cirq.OP_TREE. If the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the index i needed ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The index i is needed for situations when the mapping to be applied on the operation also depends upon which moment the operation is currently present in. For example, when characterizing and compensating for fsim gate angles, the moment index can be useful to determine what compensation to apply.

Comment on lines 97 to 109
def map_operations_and_unroll(
circuit: CIRCUIT_TYPE,
map_func: Callable[[ops.Operation, int], ops.OP_TREE],
) -> CIRCUIT_TYPE:
"""Applies local transformations via `cirq.map_operations` & unrolls intermediate circuit ops.

See `cirq.map_operations` and `cirq.unroll_circuit_op` for more details.

Args:
circuit: Input circuit to apply the transformations on. The input circuit is not mutated.
map_func: Mapping function from (cirq.Operation, moment_index) to a cirq.OP_TREE.
"""
return unroll_circuit_op(map_operations(circuit, map_func))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this really needed ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The map_operations_and_unroll method? It's expected to be the most common use case of these two primitives and hence the convenience method will reduce 2 function calls to 1 function call at all call sites.

)


def unroll_circuit_op(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we merge these three unroll_circuit_op_*** into one ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think there's a clean way to merge the unrol_circuit_op** into a single method because 2/3 implementations do not correspond to a standard insert strategy and hence the insert strategy parameter would be misleading at best. We can come up with another enum and pass it as a parameter but I don't see the advantage, especially because the implementations of the 3 methods are also very different.

@tanujkhattar
Copy link
Collaborator Author

@MichaelBroughton Addressed the comments. Please take another look.

Tl;Dr - We cannot combine unroll_circuit_op** methods into one in a clean way and I don't see much advantage in going out of the way to do it.

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, just missing docstring return: entries on most of the functions. Feel free to merge once those are added.

@tanujkhattar tanujkhattar merged commit 272350d into quantumlib:master Nov 23, 2021
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…mlib#4692)

* Add map_operations and map_moments transformer primitives

* Improve circuit type conversion efficiency

* Add return entries to docstrings
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…mlib#4692)

* Add map_operations and map_moments transformer primitives

* Improve circuit type conversion efficiency

* Add return entries to docstrings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Makes googlebot stop complaining. size: L 250< lines changed <1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants