diff --git a/compiler/qsc_circuit/src/circuit.rs b/compiler/qsc_circuit/src/circuit.rs index e9493b8bf5..effd05a839 100644 --- a/compiler/qsc_circuit/src/circuit.rs +++ b/compiler/qsc_circuit/src/circuit.rs @@ -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 { diff --git a/compiler/qsc_circuit/src/circuit/tests.rs b/compiler/qsc_circuit/src/circuit/tests.rs index 3a9f992a82..91614a3cb8 100644 --- a/compiler/qsc_circuit/src/circuit/tests.rs +++ b/compiler/qsc_circuit/src/circuit/tests.rs @@ -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()); +} diff --git a/samples/notebooks/circuits.ipynb b/samples/notebooks/circuits.ipynb index 9b8b210774..411f065794 100644 --- a/samples/notebooks/circuits.ipynb +++ b/samples/notebooks/circuits.ipynb @@ -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": {