aboutsummaryrefslogtreecommitdiff
path: root/libpsi-visualizer/src/cli
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-12-10 06:27:29 +0100
committerhachem <im@hachem.wtf>2025-12-10 06:27:29 +0100
commit140daa490485812ef50756796435538b9f6d9428 (patch)
tree635d792ae7d4db123d538ef30304bc65cde739b2 /libpsi-visualizer/src/cli
parente77aef153c8676ff90659433ac0ca157a3c585c2 (diff)
[add]: add support for non-clifford gates
Diffstat (limited to 'libpsi-visualizer/src/cli')
-rw-r--r--libpsi-visualizer/src/cli/horizontal_cli.rs174
-rw-r--r--libpsi-visualizer/src/cli/vertical_cli.rs47
2 files changed, 217 insertions, 4 deletions
diff --git a/libpsi-visualizer/src/cli/horizontal_cli.rs b/libpsi-visualizer/src/cli/horizontal_cli.rs
index bac0c5e..f1a6ba4 100644
--- a/libpsi-visualizer/src/cli/horizontal_cli.rs
+++ b/libpsi-visualizer/src/cli/horizontal_cli.rs
@@ -144,6 +144,180 @@ impl<'a> fmt::Display for HorizontalRenderer<'a> {
}
gap_line.push_str(" ");
}
+ GateOp::Sdg(t) => {
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str("─[S†]─");
+ } else {
+ line.push_str("──────");
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str("══════");
+ }
+ gap_line.push_str(" ");
+ }
+ GateOp::Tdg(t) => {
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str("─[T†]─");
+ } else {
+ line.push_str("──────");
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str("══════");
+ }
+ gap_line.push_str(" ");
+ }
+ GateOp::Sx(t) => {
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str("─[√X]─");
+ } else {
+ line.push_str("──────");
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str("══════");
+ }
+ gap_line.push_str(" ");
+ }
+ GateOp::Sxdg(t) => {
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str("─[√X†]─");
+ } else {
+ line.push_str("───────");
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str("═══════");
+ }
+ gap_line.push_str(" ");
+ }
+ GateOp::Rx(t, theta) => {
+ let label = format!("[Rx({:.2})]", theta);
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::Ry(t, theta) => {
+ let label = format!("[Ry({:.2})]", theta);
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::Rz(t, theta) => {
+ let label = format!("[Rz({:.2})]", theta);
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::P(t, theta) => {
+ let label = format!("[P({:.2})]", theta);
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::U1(t, lambda) => {
+ let label = format!("[U1({:.2})]", lambda);
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::U2(t, _, _) => {
+ let label = "[U2]";
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::U3(t, _, _, _) => {
+ let label = "[U3]";
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
+ GateOp::CRx(c, t, theta) | GateOp::CRy(c, t, theta) | GateOp::CRz(c, t, theta) | GateOp::CP(c, t, theta) => {
+ let label = match op {
+ GateOp::CRx(_, _, _) => format!("[CRx({:.2})]", theta),
+ GateOp::CRy(_, _, _) => format!("[CRy({:.2})]", theta),
+ GateOp::CRz(_, _, _) => format!("[CRz({:.2})]", theta),
+ GateOp::CP(_, _, _) => format!("[CP({:.2})]", theta),
+ _ => unreachable!(),
+ };
+ for (i, line) in q_lines.iter_mut().enumerate() {
+ if i == *c {
+ line.push_str(&format!("─{}─", "●".to_string() + &"─".repeat(label.len() - 1)));
+ } else if i == *t {
+ line.push_str(&format!("─{}─", label));
+ } else if i > min_q && i < max_q {
+ line.push_str(&format!("─{}─", "│".to_string() + &"─".repeat(label.len() - 1)));
+ } else {
+ line.push_str(&format!("─{}─", "─".repeat(label.len())));
+ }
+ }
+ for line in c_lines.iter_mut() {
+ line.push_str(&format!("═{}═", "═".repeat(label.len())));
+ }
+ gap_line.push_str(&format!(" {} ", " ".repeat(label.len())));
+ }
GateOp::CNOT(c, t) => {
for (i, line) in q_lines.iter_mut().enumerate() {
if i == *c {
diff --git a/libpsi-visualizer/src/cli/vertical_cli.rs b/libpsi-visualizer/src/cli/vertical_cli.rs
index 317833c..6698961 100644
--- a/libpsi-visualizer/src/cli/vertical_cli.rs
+++ b/libpsi-visualizer/src/cli/vertical_cli.rs
@@ -19,6 +19,21 @@ impl<'a> VerticalRenderer<'a> {
GateOp::Z(_) => "[Z]".to_string(),
GateOp::S(_) => "[S]".to_string(),
GateOp::T(_) => "[T]".to_string(),
+ GateOp::Sdg(_) => "[S†]".to_string(),
+ GateOp::Tdg(_) => "[T†]".to_string(),
+ GateOp::Sx(_) => "[√X]".to_string(),
+ GateOp::Sxdg(_) => "[√X†]".to_string(),
+ GateOp::Rx(_, theta) => format!("[Rx({:.2})]", theta),
+ GateOp::Ry(_, theta) => format!("[Ry({:.2})]", theta),
+ GateOp::Rz(_, theta) => format!("[Rz({:.2})]", theta),
+ GateOp::P(_, theta) => format!("[P({:.2})]", theta),
+ GateOp::U1(_, lambda) => format!("[U1({:.2})]", lambda),
+ GateOp::U2(_, _, _) => "[U2]".to_string(),
+ GateOp::U3(_, _, _, _) => "[U3]".to_string(),
+ GateOp::CRx(_, _, _) => "[CRx]".to_string(),
+ GateOp::CRy(_, _, _) => "[CRy]".to_string(),
+ GateOp::CRz(_, _, _) => "[CRz]".to_string(),
+ GateOp::CP(_, _, _) => "[CP]".to_string(),
GateOp::CNOT(_, _) => "●".to_string(),
GateOp::CZ(_, _) => "●".to_string(),
GateOp::SWAP(_, _) => "╳".to_string(),
@@ -121,7 +136,18 @@ impl<'a> fmt::Display for VerticalRenderer<'a> {
| GateOp::Y(t)
| GateOp::Z(t)
| GateOp::S(t)
- | GateOp::T(t) => {
+ | GateOp::T(t)
+ | GateOp::Sdg(t)
+ | GateOp::Tdg(t)
+ | GateOp::Sx(t)
+ | GateOp::Sxdg(t)
+ | GateOp::Rx(t, _)
+ | GateOp::Ry(t, _)
+ | GateOp::Rz(t, _)
+ | GateOp::P(t, _)
+ | GateOp::U1(t, _)
+ | GateOp::U2(t, _, _)
+ | GateOp::U3(t, _, _, _) => {
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
@@ -145,24 +171,37 @@ impl<'a> fmt::Display for VerticalRenderer<'a> {
let gate_line: String = line.into_iter().collect();
writeln!(f, "{}", gate_line)?;
}
- GateOp::CNOT(c, t) | GateOp::CZ(c, t) | GateOp::SWAP(c, t) => {
+ GateOp::CNOT(c, t) | GateOp::CZ(c, t) | GateOp::SWAP(c, t)
+ | GateOp::CRx(c, t, _) | GateOp::CRy(c, t, _) | GateOp::CRz(c, t, _) | GateOp::CP(c, t, _) => {
let (sym1, sym2) = match op {
GateOp::CNOT(_, _) => ('●', '⊕'),
GateOp::CZ(_, _) => ('●', '●'),
GateOp::SWAP(_, _) => ('╳', '╳'),
+ GateOp::CRx(_, _, _) | GateOp::CRy(_, _, _) | GateOp::CRz(_, _, _) | GateOp::CP(_, _, _) => ('●', '□'),
_ => unreachable!(),
};
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
- let center = i * (col_width + 1) + col_width / 2;
+ let col_start = i * (col_width + 1);
+ let center = col_start + col_width / 2;
if i < min_q || i > max_q {
line[center] = '│';
} else if i == *c {
line[center] = sym1;
} else if i == *t {
- line[center] = sym2;
+ // For controlled parametric gates, show the gate label on target
+ if matches!(op, GateOp::CRx(_, _, _) | GateOp::CRy(_, _, _) | GateOp::CRz(_, _, _) | GateOp::CP(_, _, _)) {
+ let label_start = col_start + (col_width - label.chars().count()) / 2;
+ for (j, ch) in label.chars().enumerate() {
+ if label_start + j < line.len() {
+ line[label_start + j] = ch;
+ }
+ }
+ } else {
+ line[center] = sym2;
+ }
}
}