Make OpTree runtime checkable
See original GitHub issueIs your feature request related to a use case or problem? Please describe. I use code to process circuits or circuit-like objects. It would be nice to have a simple check to see if an object can be mapped to a circuit (i.e. if it is an OpTree).
Describe the solution you’d like
Add the @runtime_checkable
decorator to the OpTree protocol.
Then I could do isinstance(test_object, cirq.OP_TREE)
.
Could it be this easy? Would it break anything? I’m not sure…
[optional] Describe alternatives/workarounds you’ve considered Implement a function that recurs through an object to check if it is optree-like. Or see if invoking Circuit on the object crashes.
[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
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
I am implementing an API for specifying generic noise models. In order to parse the input I need to check that it can be converted to a circuit. For my purposes it is sufficient to check if Circuit( ) can evaluate the input.
is_op_tree
looks reasonable, but one possible pitfall is that many op-trees (e.g. those coming fromdecompose
methods) are implemented as generators; if you callis_op_tree
on a generator you will exhaust it and then if you try to do anything else with it it’ll be empty. As long as generators are allowed I don’t see a way to deal with the generically, so I’d lean more toward just trying to use the object as an OpTree and catching exceptions, e.g. try to make a circuit and if it fails then do something else.@dkafri, can you describe your use case in more detail? Where are you getting an object from where you don’t know whether or not it is an OpTree, and what are you trying to do with it?