-
Notifications
You must be signed in to change notification settings - Fork 989
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
Implement a way to identify CircuitOperations
(operations with loops)
#6570
Comments
The incantation I usually use is |
It becomes somewhat cumbersome, since I usually want to use |
I agree this can be a bit tedious. Often typing will help with this, since if you do Note that the root problem here is that tags are handled by wrapping operations with |
@pavoljuhas @verult I'm interested in helping out on this issue if it is still available. @maffoo your comment provides some helpful direction for this issue - is the ask here still just to add a helper function, or is the scope a bit larger to add some tag support to the base |
tbh I'd consider removing immutability from the next version of Cirq. Immutability is nice for lots of things, but I don't feel like it's providing all that much value here. It affects circuit construction perf, and makes things like this unnecessarily painful. It keeps people from shooting themselves in the foot in some ways, but makes things harder to work with in other ways. If we remove immutability, we can just have |
@mathe-matician - thanks for volunteering to help! @dkafri - I wonder if it would work to add some |
Do you mean to add this to a @cirq.transformer(add_deep_support=True)
@attrs.define
class _ReplaceOpWith:
"""cirq Transformer protocol for replacing operations."""
predicate: Callable[[cirq.Operation], bool]
replacement: Callable[[cirq.Operation], cirq.OP_TREE]
def __call__(
self, circuit: cirq.AbstractCircuit, *, context: cirq.TransformerContext | None = None
) -> cirq.Circuit:
circuit = circuit.unfreeze(copy=True)
return circuit.map_operations(self._replace_if_pred)
def _replace_if_pred(self, op: cirq.Operation) -> cirq.Operation:
return self.replacement(op) if self.predicate(op) else op
def deep_replace(
circuit: cirq.Circuit | cirq.FrozenCircuit,
predicate: Callable[[cirq.Operation], bool],
replacement: Callable[[cirq.Operation], cirq.OP_TREE],
) -> cirq.Circuit:
"""Replace operations in a circuit according to a standard rule.
This implementation works for nested CircuitOperations.
Args:
circuit: The circuit to modify.
predicate: Operations flagged by this boolean function are modified.
replacement: Takes an operation to be modified and returns the desired changed operation(s).
"""
replacer = _ReplaceOpWith(predicate, replacement)
return replacer(circuit, context=cirq.TransformerContext(deep=True)) |
I would be wary of removing immutability from |
Is the transformer library not already handling these? It looks from the code like it should be working around tags on CircuitOperations here.
|
Is your feature request related to a use case or problem? Please describe.
I've been doing a lot of transformations on
Circuit
s containingCircuitOperation
s. Sometimes I need to manipulateCircuitOperation
s. The way I nominally identify them is usingisinstance(..., cirq.CircuitOperation)
. But it turns out this doesn't work if theCircuitOperation
is contained in acirq.TaggedOperation
.Describe the solution you'd like
Implement a function like
cirq.measurement_key_objs
that I can trust to always tell me if an operation is aCircuitOperation
(or effectively one under the hood).[optional] Describe alternatives/workarounds you've considered
Writing my own custom identifier that handles TaggedOperations. But am I missing something?
[optional] Additional context (e.g. screenshots)
What is the urgency from your perspective for this issue? Is it blocking important work?
P3 - I'm not really blocked by it, it is an idea I'd like to discuss / suggestion based on principle
The text was updated successfully, but these errors were encountered: