Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix circuit rendering for long gate names #2127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions compiler/qsc_circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ fn fmt_qubit_label(id: usize) -> String {

/// "── A ──"
fn fmt_on_qubit_wire(obj: &str) -> String {
format!("{:─^COLUMN_WIDTH$}", format!(" {obj} "))
let width = obj.len() + 4;
format!("{:─^width$}", format!(" {obj} "))
}

/// "══ A ══"
fn fmt_on_classical_wire(obj: &str) -> String {
format!("{:═^COLUMN_WIDTH$}", format!(" {obj} "))
let width = obj.len() + 4;
format!("{:═^width$}", format!(" {obj} "))
}

impl Display for Circuit {
Expand Down
59 changes: 59 additions & 0 deletions compiler/qsc_circuit/src/circuit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,62 @@ fn two_targets() {
"]]
.assert_eq(&c.to_string());
}

#[test]
fn long_gate_names() {
let c = Circuit {
operations: vec![
Operation {
gate: "rx".to_string(),
display_args: Some("3.1416".to_string()),
is_controlled: false,
is_adjoint: false,
is_measurement: false,
controls: vec![],
targets: vec![Register::quantum(0)],
children: vec![],
},
Operation {
gate: "H".to_string(),
display_args: None,
is_controlled: false,
is_adjoint: false,
is_measurement: false,
controls: vec![],
targets: vec![Register::quantum(1)],
children: vec![],
},
Operation {
gate: "rx".to_string(),
display_args: Some("3.1416".to_string()),
is_controlled: false,
is_adjoint: false,
is_measurement: false,
controls: vec![],
targets: vec![Register::quantum(2)],
children: vec![],
},
],
qubits: vec![
Qubit {
id: 0,
num_children: 0,
},
Qubit {
id: 1,
num_children: 0,
},
Qubit {
id: 2,
num_children: 0,
},
],
};

expect![[r"
q_0 rx(3.1416)
q_1 ─── H ───
q_2 rx(3.1416)
"]]
.assert_eq(&c.to_string());
}
64 changes: 64 additions & 0 deletions samples/notebooks/circuits.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,70 @@
"source": [
"Circuit(qsharp.dump_circuit())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test the rendering of long gate names in the circuit."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"gates that fit the column width\")\n",
"print(\"circuit looks okay\")\n",
"\n",
"qsharp.eval(\"\"\"\n",
"\n",
"use q0 = Qubit();\n",
"use q1 = Qubit();\n",
"\n",
"H(q0);\n",
"H(q1);\n",
"X(q1);\n",
"CNOT(q0, q1);\n",
"M(q0)\n",
"\n",
"\"\"\")\n",
"\n",
"print(qsharp.dump_circuit())\n",
"\n",
"print(\"q_2 has gates that are too wide for the column width\")\n",
"print(\"circuit wires disappear, and the vertical columns don't align anymore\")\n",
"\n",
"qsharp.eval(\"\"\"\n",
"\n",
"use q2 = Qubit();\n",
"\n",
"Rx(1.0, q2);\n",
"Rx(1.0, q2);\n",
"\n",
"\"\"\")\n",
"\n",
"print(qsharp.dump_circuit())\n",
"\n",
"print(\"q_3 has gates with both short and long gate labels\")\n",
"print(\"here, the column widths should be variable so that the H doesn't take up too much width, but rx(1.000) still fits within a column\")\n",
"\n",
"qsharp.eval(\"\"\"\n",
"\n",
"use q3 = Qubit();\n",
"\n",
"H(q3);\n",
"Rx(1.0, q3);\n",
"H(q3);\n",
"Rx(1.0, q3);\n",
"H(q3);\n",
"Rx(1.0, q3);\n",
"\n",
"\"\"\")\n",
"\n",
"print(qsharp.dump_circuit())"
]
}
],
"metadata": {
Expand Down