Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions crates/circuit/src/circuit_drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn draw_circuit(
if approx::abs_diff_eq!(*f, 0.) {
String::new()
} else {
format!("global phase: {}\n", f)
format!("global phase: {}\n", format_float_pi(*f))
}
}
Param::ParameterExpression(expr) => {
Expand Down Expand Up @@ -763,7 +763,7 @@ impl TextDrawer {
.params_view()
.iter()
.map(|param| match param {
Param::Float(f) => f.to_string(),
Param::Float(f) => format_float_pi(*f),
Param::ParameterExpression(expr) => expr.to_string(),
_ => format!("{:?}", param),
})
Expand Down Expand Up @@ -1202,6 +1202,42 @@ impl TextDrawer {
}
}

fn format_float_pi(f: f64) -> String {
Comment thread
OnyxBrumeSky marked this conversation as resolved.
Outdated
const DELTA: f64 = 1e-9; // This is the minimal treshold to accept a number as pi
const DENOMINATOR: i64 = 16; // This is the max denominator used to find ratio. 16 is used here but greater or smaller values could be added for either precison or speed

if approx::abs_diff_eq!(f, 0.0, epsilon = DELTA) {
return "0".to_string();
}
Comment thread
OnyxBrumeSky marked this conversation as resolved.

let frac: f64 = f / std::f64::consts::PI;

for q in 1..DENOMINATOR {
Comment thread
OnyxBrumeSky marked this conversation as resolved.
Outdated
let p: i64 = (frac * q as f64).round() as i64;

if p == 0 {
continue;
}
Comment thread
OnyxBrumeSky marked this conversation as resolved.
Outdated

if approx::abs_diff_eq!(p as f64 / q as f64, frac, epsilon = DELTA) {
let sign = match p {
x if x < 0 => "-",
_ => "",
};
Comment thread
OnyxBrumeSky marked this conversation as resolved.
Outdated
let numerator = p.unsigned_abs();

return match (numerator, q) {
(1, 1) => format!("{}π", sign),
(n, 1) => format!("{}{}π", sign, n),
(1, d) => format!("{}π/{}", sign, d),
(n, d) => format!("{}{}π/{}", sign, n, d),
};
}
}

f.to_string() // case no denominator found
}

#[cfg(test)]
mod tests {
use ndarray::Array2;
Expand Down Expand Up @@ -1902,4 +1938,24 @@ q_1: ┤ Ry(🎩) ├┤1 ├┤ 💶🔉(🎩) ├┤1 ├
";
assert_eq!(result, expected.trim_start_matches("\n"));
}

#[test]
fn test_pi_float_format() {
use std::f64::consts::PI;

assert_eq!(format_float_pi(0.0), "0");
assert_eq!(format_float_pi(PI), "π");
assert_eq!(format_float_pi(2.0 * PI), "2π");
assert_eq!(format_float_pi(1.0 * PI), "π");
assert_eq!(format_float_pi(PI / 3.0), "π/3");
assert_eq!(format_float_pi(2.0 * PI / 3.0), "2π/3");
assert_eq!(format_float_pi(-PI), "-π");
assert_eq!(format_float_pi(-PI / 4.0), "-π/4");
assert_eq!(format_float_pi(5.0), "5");
assert_eq!(format_float_pi(PI / 15.0), "π/15");

let x = PI / 17.0;
assert_eq!(format_float_pi(PI / 17.0), x.to_string());
assert_eq!(format_float_pi(3.14159), "3.14159");
}
}