-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add cirq.toggle_tags
helper to apply transformers on specific subsets of operations in a circuit
#4973
Changes from 1 commit
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 |
---|---|---|
|
@@ -77,4 +77,5 @@ | |
unroll_circuit_op, | ||
unroll_circuit_op_greedy_earliest, | ||
unroll_circuit_op_greedy_frontier, | ||
xor_ops_with_tags, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
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. 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? 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. Yeah, naming this method is hard. changed to |
||
tags_to_xor = set(tags) | ||
|
||
def map_func(op: 'cirq.Operation', _) -> 'cirq.Operation': | ||
op_tags = set(op.tags) | ||
new_tags = (op_tags - tags_to_xor) | (tags_to_xor - op_tags) | ||
tanujkhattar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
op | ||
if deep and isinstance(op, circuits.CircuitOperation) | ||
else op.untagged.with_tags(*new_tags) | ||
) | ||
|
||
return map_operations(circuit, map_func, deep=deep) |
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 bothX(q1)
andY(q2)
in moments 2 & 3 (0-based indexing) to move left. But by adding a tag toX(q1)
and usingxor_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 onlyX(q1)
aligns to the left butY(q2)
stays in-place.