Qualtran icon indicating copy to clipboard operation
Qualtran copied to clipboard

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

Open tanujkhattar opened this issue 10 months ago • 3 comments

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 https://github.com/quantumlib/Qualtran/issues/791

tanujkhattar avatar Apr 03 '24 18:04 tanujkhattar

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

What if you return f'$Z^{self.exponent}$'

fdmalone avatar Apr 03 '24 20:04 fdmalone

Oh the issue is fractional formatting not that the label isn't rendered as latex

fdmalone avatar Apr 03 '24 20:04 fdmalone

What does your ideal solution look like here? Would it be mostly solved by #791? Shouldn't sympy be responsible for keeping the expression in fractional form? Remember that there is a big difference between sympify('x^(1/2)') and 'x^(1/2.)'

mpharrigan avatar Apr 18 '24 23:04 mpharrigan