Cirq
Cirq copied to clipboard
Add transformer primitives as methods on TransformerContext
This is a proposal to make transformer_primitives
functions available as methods on TransformerContext
. Then they can be invoked without passing extra parameters which are already stored on the context, such as the deep
flag and tags_to_ignore
. I think this would be simplified somewhat if we changed transformer primitives to take TransformerContext
instead of Optional[TransformerContext]
, with a default value specified as appropriate, but we can't change that due to backwards compatibility so there are a bunch of places where we would have to do things like context = context or transformers.TransformerContext(...)
to instantiate a default context if needed. Still I think this simplifies existing transformers quite a bit and could prevent bugs in the future from forgetting to pass args from the context manually. I also added a function TransformerContext.replace
that allows creating a new context with some parameters changed, to allow overriding some parameters before calling a primitive. I've updated a few existing callsites to use the new style, but for now just want to put this up to get feedback before changing more.