Skip to content

Add a pretty formatter for rotation angles so we can have nicer musical score diagrams #853

Open
@tanujkhattar

Description

@tanujkhattar

When plotting musical score diagrams for QvrZPow with a symbolic rotation angle sympy.Symbol('Y'), I want the output to have nicely formatted rotation angles instead of very small floating point values. For example:

When running the code

qvr_zpow = QvrZPow(Register('x', QFxp(12, 6, False)), gamma = sympy.Symbol('Y'))
show_bloq(qvr_zpow, 'dtype')
show_bloq(qvr_zpow.decompose_bloq(), 'musical_score')

I want the output to be like

image

instead of (current main)

image

or what you get if you update the pretty name of ZPowGate bloq to return f'Z^{self.exponent}'

image

To generate the original diagram, I implemented a very hacky format function locally as follows:

def format(x: SymbolicFloat) -> str:
    if isinstance(x, float) or x.is_constant():
        if x.is_integer:
            return f'{int(x)}'
        for i in range(1, 20):
            if x * (2**i) == 1:
                return f'/2^{i}'
        return f'{x:.2g}'
    if isinstance(x, sympy.Symbol):
        return str(x)
    if isinstance(x, sympy.Expr):
        return '*'.join(format(x) for x in x.args[::-1])
    return str(x)

The goal of this issue would be to add a similar production quality format function that can be used to get nice string representations of exponents represented as a SymbolicFloat

cc #791

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions