pyquil
pyquil copied to clipboard
Refactor `pyquil.latex._diagram.DiagramBuilder` to be more flexible
Pre-Request Checklist
- [x] I am running the latest versions of pyQuil and the Forest SDK
- [x] I checked to make sure that this feature has not already been requested
Issue Description
The top level control flow for PyQuil's latex diagram generation routine is found in pyquil.latex._diagram.DiagramBuilder.build
. Right now the approach taken is that one loops over instructions, handling each by a case analysis. This is adequate but makes adding additional identities (e.g. as requrested in #1193 ) a bit of a nuisance.
Proposed Solution
pyquil.latex._diagram.DiagramBuilder.build
could instead check a list of "instruction handlers" of type (Instruction, DiagramState) -> Optional[DiagramState]
, which return a non-None
value if they are able to update the diagram with the provided instruction. Handlers for the existing cases (e.g. measure, 1q gates, all of the custom source-target ops, and the generic unitary constructor) could be provided by default, and additional handlers which address certain common circuit identies (e.g. CONTROLLED X 0
equals CNOT 0
) could also be implemented. The availability of handlers which do nontrivial rewriting could be controlled by a flag in DiagramSettings
.