@@ -63,7 +63,10 @@ pub fn draw_circuit(
6363 if approx:: abs_diff_eq!( * f, 0. ) {
6464 String :: new ( )
6565 } else {
66- format ! ( "global phase: {}\n " , F64UiFormatter :: new( 5 ) . format( * f) )
66+ format ! (
67+ "global phase: {}\n " ,
68+ F64UiFormatter :: new( 5 ) . format_with_pi( * f)
69+ )
6770 }
6871 }
6972 Param :: ParameterExpression ( expr) => {
@@ -672,13 +675,19 @@ impl TextWireElement {
672675 }
673676}
674677
675- /// A simple formatter for rendering nicely-truncated floating-point numbers,
676- /// in a way similar to Python's g or printf's %g format specifiers.
678+ /// A formatter for UI rendering of floating-point numbers
679+ ///
680+ /// Supports formatting similar to Python's `g` or C printf's `%g` format specifiers
681+ /// as well as formatting of multiples and fractions of pi.
682+ ///
677683/// Example outputs:
678- /// F64UiFormatter::new(4).format(1.23456) --> 1.235
679- /// F64UiFormatter::new(4).format(123.456) --> 123.5
680- /// F64UiFormatter::new(5).format(12345678f64) --> 1.2346e7
681- /// F64UiFormatter::new(5).format(-0.00001234) --> -1.234e-5
684+ /// ```text
685+ /// F64UiFormatter::new(4).format(1.23456) → 1.235
686+ /// F64UiFormatter::new(4).format(123.456) → 123.5
687+ /// F64UiFormatter::new(5).format(12345678.0) → 1.2346e7
688+ /// F64UiFormatter::new(5).format(-0.00001234) → -1.234e-5
689+ /// F64UiFormatter::new(5).format_with_pi(5π/6) → 5π/6
690+ /// ```
682691struct F64UiFormatter {
683692 buffer : Vec < u8 > ,
684693 options : lexical_write_float:: Options ,
@@ -707,6 +716,12 @@ impl F64UiFormatter {
707716 let buf = num. to_lexical_with_options :: < STANDARD > ( & mut self . buffer , & self . options ) ;
708717 std:: str:: from_utf8_mut ( buf) . expect ( "Byte representation should be valid" )
709718 }
719+
720+ /// Tries to format the string as a multiple or simple fraction of pi if possible,
721+ /// otherwise falls back to the simpler [F64UiFormatter::format] logic
722+ fn format_with_pi ( & mut self , num : f64 ) -> String {
723+ format_float_pi ( num) . unwrap_or_else ( || self . format ( num) . to_owned ( ) )
724+ }
710725}
711726
712727pub const Q_WIRE : char = '─' ;
@@ -807,7 +822,9 @@ impl TextDrawer {
807822 . params_view ( )
808823 . iter ( )
809824 . map ( |param| match param {
810- Param :: Float ( f) => F64UiFormatter :: new ( 5 ) . format ( * f) . to_string ( ) ,
825+ Param :: Float ( f) => {
826+ F64UiFormatter :: new ( 5 ) . format_with_pi ( * f) . to_string ( )
827+ }
811828 Param :: ParameterExpression ( expr) => expr. to_string ( ) ,
812829 _ => format ! ( "{:?}" , param) ,
813830 } )
@@ -2139,7 +2156,7 @@ q_1: ┤ Ry(🎩) ├┤1 ├┤ 💶🔉(🎩) ├┤1 ├
21392156 ShareableQubit :: new_anonymous( ) ,
21402157 ShareableQubit :: new_anonymous( ) ,
21412158 ] ;
2142- let mut circuit = CircuitData :: new ( Some ( qubits) , None , Param :: Float ( 12.3 ) ) . unwrap ( ) ;
2159+ let mut circuit = CircuitData :: new ( Some ( qubits) , None , Param :: Float ( 0.8 * PI ) ) . unwrap ( ) ;
21432160
21442161 circuit
21452162 . push_standard_gate ( StandardGate :: RX , & [ Param :: Float ( 1.234567 ) ] , & [ Qubit ( 0 ) ] )
@@ -2165,15 +2182,22 @@ q_1: ┤ Ry(🎩) ├┤1 ├┤ 💶🔉(🎩) ├┤1 ├
21652182 circuit
21662183 . push_standard_gate ( StandardGate :: RX , & [ Param :: Float ( 0.0000123456 ) ] , & [ Qubit ( 1 ) ] )
21672184 . unwrap ( ) ;
2185+ circuit
2186+ . push_standard_gate (
2187+ StandardGate :: RX ,
2188+ & [ Param :: Float ( 2.0 / 3.0 * PI ) ] ,
2189+ & [ Qubit ( 1 ) ] ,
2190+ )
2191+ . unwrap ( ) ;
21682192
21692193 let result = draw_circuit ( & circuit, true , true , None ) . unwrap ( ) ;
21702194 let expected = "
2171- global phase: 6.0168
2195+ global phase: 4π/5
21722196 ┌────────────┐ ┌────────────┐ ┌───────────────┐
2173- q_0: ─┤ Rx(1.2346) ├─┤ Rx(123.46) ├─┤ Ry(1.23456*ϕ) ├
2174- ┌┴────────────┴┐├────────────┴┐├───────────────┤
2175- q_1: ┤ Rz(1.2346e8) ├┤ Rx(0.12346) ├┤ Rx(1.2346e-5) ├
2176- └──────────────┘└─────────────┘└───────────────┘
2197+ q_0: ─┤ Rx(1.2346) ├─┤ Rx(123.46) ├─┤ Ry(1.23456*ϕ) ├────────────
2198+ ┌┴────────────┴┐├────────────┴┐├───────────────┤┌──────────┐
2199+ q_1: ┤ Rz(1.2346e8) ├┤ Rx(0.12346) ├┤ Rx(1.2346e-5) ├┤ Rx(2π/3) ├
2200+ └──────────────┘└─────────────┘└───────────────┘└──────────┘
21772201" ;
21782202
21792203 assert_eq ! ( result, expected. trim_start_matches( "\n " ) ) ;
@@ -2245,11 +2269,13 @@ q_1: ┤ Rz(1.2346e8) ├┤ Rx(0.12346) ├┤ Rx(1.2346e-5) ├
22452269 ( 12.34 * 1_000_000.0 , "1.234e7" ) ,
22462270 ( -0.00001 , "-1e-5" ) ,
22472271 ( 12345678.000001 , "1.2346e7" ) ,
2272+ ( 15.0 * PI / 16.0 , "15π/16" ) ,
2273+ ( -2.0 * PI / 3.0 , "-2π/3" ) ,
22482274 ] ;
22492275
22502276 let mut formatter = F64UiFormatter :: new ( 5 ) ;
22512277 for test in test_data_5_sig_digits {
2252- assert_eq ! ( test. 1 , formatter. format ( test. 0 ) ) ;
2278+ assert_eq ! ( test. 1 . to_owned ( ) , formatter. format_with_pi ( test. 0 ) ) ;
22532279 }
22542280 }
22552281}
0 commit comments