Qualtran
Qualtran copied to clipboard
Add a pretty formatter for rotation angles so we can have nicer musical score diagrams
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
instead of (current main)
or what you get if you update the pretty name of ZPowGate
bloq to return f'Z^{self.exponent}'
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
if you update the pretty name of ZPowGate bloq to return f'Z^{self.exponent}'
What if you return f'$Z^{self.exponent}$'
Oh the issue is fractional formatting not that the label isn't rendered as latex
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.)'