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.toggle_tags helper to apply transformers on specific subsets of operations in a circuit #4973

Merged
merged 3 commits into from
Feb 10, 2022

Conversation

tanujkhattar
Copy link
Collaborator

Adds indirect capability to apply a transformer, that supports ignoring tags_to_ignore, on a specific subset of operations in a large circuit.

Fixes #4972
A step towards fixing #3144

@tanujkhattar tanujkhattar requested review from a team, vtomole and cduck as code owners February 10, 2022 04:36
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 10, 2022
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

If the intended use case for this is large circuits, is performance overhead a concern? It seems like an explicit tags_to_apply_on would be cheaper. (Not super worried about this, just curious)

@@ -426,3 +426,18 @@ def unroll_circuit_op_greedy_frontier(
)
frontier = unrolled_circuit.insert_at_frontier(sub_circuit.all_operations(), idx, frontier)
return _to_target_circuit_type(unrolled_circuit, circuit)


def xor_ops_with_tags(circuit: CIRCUIT_TYPE, tags: Sequence[Hashable], *, deep: bool = False):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a function docstring.

Also, I'm not sure about this name - we're not xor'ing ops, we're xor'ing their tags. Maybe (as used in the test) "flip_tags" or "toggle_tags" would be clearer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, naming this method is hard. changed to toggle_tags and added a docstring

cirq.testing.assert_same_circuits(
cirq.xor_ops_with_tags(
cirq.align_left(
cirq.xor_ops_with_tags(c_orig, [tag]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is going on here? Is the xor a necessary step for alignment, or just a test element to verify proper behavior of align_left?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So in the example here, directly doing an align_left on the entire circuit would cause both X(q1) and Y(q2) in moments 2 & 3 (0-based indexing) to move left. But by adding a tag to X(q1) and using xor_ops_with_tags to toggle tags --> do optimization --> toggle tags again, we are able to do the transformation only the subset of operations that are tagged -- therefore only X(q1) aligns to the left but Y(q2) stays in-place.

@tanujkhattar
Copy link
Collaborator Author

If the intended use case for this is large circuits, is performance overhead a concern? It seems like an explicit tags_to_apply_on would be cheaper. (Not super worried about this, just curious)

I think the maintenance overhead to modify every individual transformer implementation to support both tags_to_ignore and tags_to_apply_on is probably not worth the effort. For large circuits going forward, I also have plans to parallelize map_operations (since it's inherently local, unless the map_func is stateful -- which is not the case here) which should be enough to take care of any potential performance overhead.

Addressed other feedback. PTAL.

@tanujkhattar tanujkhattar changed the title Add xor_ops_with_tags helper to apply transformers on specific subsets of operations in a circuit Add cirq.toggle_tags helper to apply transformers on specific subsets of operations in a circuit Feb 10, 2022
Copy link
Collaborator

@95-martin-orion 95-martin-orion 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 docstring explanation is a great reference.

@tanujkhattar tanujkhattar merged commit 9ab327a into quantumlib:master Feb 10, 2022
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
…ts of operations in a circuit (quantumlib#4973)

* Add  helper to apply transformers on specific subsets of operations in a circuit

* Rename to toggle_tags and address feedback
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…ts of operations in a circuit (quantumlib#4973)

* Add  helper to apply transformers on specific subsets of operations in a circuit

* Rename to toggle_tags and address feedback
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…ts of operations in a circuit (quantumlib#4973)

* Add  helper to apply transformers on specific subsets of operations in a circuit

* Rename to toggle_tags and address feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
3 participants