Skip to content

Commit 9a00b5a

Browse files
committed
Fix rendering bug of controlled gates
Fixes a bug where controlled gates (e.g. a CPhase gate) had their top or bottom connectors misaligned by one position with the corresponding controlled lines, in case their label width is even.
1 parent d045921 commit 9a00b5a

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

crates/circuit/src/circuit_drawer.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ impl TextDrawer {
841841
BoxedElement::Single(inst) => {
842842
let mut top_con = Q_WIRE;
843843
let mut bot_con = Q_WIRE;
844+
let mut label = format!(" {} ", Self::get_label(inst));
844845
if let Some(gate) = inst.op.try_standard_gate() {
845846
if gate.is_controlled_gate() {
846847
let qargs = circuit.get_qargs(inst.qubits);
@@ -851,13 +852,15 @@ impl TextDrawer {
851852
if qargs.last().unwrap().index() < maxima {
852853
bot_con = BOT_CON;
853854
}
855+
// This ensures the top_con/bot_con connectors are properly aligned with the control
856+
// lines regardless of whether the text element padding size.
857+
(label.len() % 2 == 0).then(|| label.push(' '));
854858
}
855859
} else if let Some(std_inst) = inst.op.try_standard_instruction() {
856860
if std_inst == StandardInstruction::Measure {
857861
bot_con = C_BOT_CON;
858862
}
859863
}
860-
let label = format!(" {} ", Self::get_label(inst));
861864
let label_len = label.width();
862865
let left_len = (label_len - 1) / 2;
863866
let right_len = label_len - left_len - 1;
@@ -1562,27 +1565,27 @@ q_4: ─────────────────────────
15621565
« │ │┌───────┐│ │ ┌─────┴─────┐ ┌──┴──┐»
15631566
«q_2: ──■──┤1 ├┤0 Ecr ├┤1 ├──────■──────┤ Ry(3.141) ├──────■──────┤ Sdg ├»
15641567
« │ └───────┘│ │└─────────┘ │ └───────────┘ │ └─────┘»
1565-
« ┌─┴─┐ │ │ ┌────┴─────┐ ┌─────┴─────┐ »
1566-
«q_3: ┤ Y ├─────────┤1 ├───────────┤ P(3.141) ├──────────────┤ Rz(3.141) ├───────»
1567-
« └───┘ └───────┘ └────────── └───────────┘ »
1568+
« ┌─┴─┐ │ │ ┌────┴─────┐ ┌─────┴─────┐ »
1569+
«q_3: ┤ Y ├─────────┤1 ├───────────┤ P(3.141) ─────────────┤ Rz(3.141) ├───────»
1570+
« └───┘ └───────┘ └───────────┘ └───────────┘ »
15681571
« »
15691572
«q_4: ────────────────────────────────────────────────────────────────────────────────»
15701573
« »
1571-
« ┌──────────────┐ »
1572-
«q_0: ──────────────■────────────────────────────────┤0 Rxx(3.141) ├─────»
1573-
« │ │ │ »
1574-
« ┌─────────────┴──────────────┐ │ │ »
1575-
«q_1: ┤ U(3.141,3.141,3.141,3.141) ├──────■──────────┤1 ├─────»
1576-
« └────────────────────────────┘ │ └──────────────┘ »
1577-
« ┌─────┴─────┐ »
1578-
«q_2: ──────────────■───────────────┤ U1(3.141) ├────────────■────────────»
1579-
« │ └───────────┘ │ »
1580-
« ┌─┴──┐ ┌───────────┴───────────┐»
1581-
«q_3: ────────────┤ Sx ├─────────────────────────┤ U3(3.141,3.141,3.141) ├»
1582-
« └────┘ └───────────────────────┘»
1583-
« »
1584-
«q_4: ────────────────────────────────────────────────────────────────────»
1585-
« »
1574+
« ┌──────────────┐ »
1575+
«q_0: ──────────────■────────────────────────────────┤0 Rxx(3.141) ├─────»
1576+
« │ │ │ »
1577+
« ┌─────────────┴──────────────┐ │ │ »
1578+
«q_1: ┤ U(3.141,3.141,3.141,3.141) ├──────■──────────┤1 ├─────»
1579+
« └────────────────────────────┘ │ └──────────────┘ »
1580+
« ┌─────┴─────┐ »
1581+
«q_2: ──────────────■───────────────┤ U1(3.141) ├────────────■────────────»
1582+
« │ └───────────┘ │ »
1583+
« ┌─┴──┐ ┌───────────┴───────────┐»
1584+
«q_3: ────────────┤ Sx ├─────────────────────────┤ U3(3.141,3.141,3.141) ├»
1585+
« └────┘ └───────────────────────┘»
1586+
« »
1587+
«q_4: ────────────────────────────────────────────────────────────────────»
1588+
« »
15861589
« ┌──────────────┐ »
15871590
«q_0: ────────────────┤0 Rzx(3.141) ├────────────────────────────────────────────────»
15881591
« │ │ »
@@ -1598,20 +1601,20 @@ q_4: ─────────────────────────
15981601
« »
15991602
«q_4: ────────────────────────────────────────────────────────────────────────────────»
16001603
« »
1601-
« ┌─────────┐ ┌───────────┐
1602-
«q_0: ───────■─────┤ 0 ├──■────■───┤ 0 ├
1603-
« │ │ │ │ │ │ │
1604-
« │ │ │ │ │ │ │
1605-
«q_1: ──■────■───■─┤ 1 Rccx ├──■────■───┤ 1 Rcccx ├
1606-
« │ │ │ │ │ │ │ │ │
1607-
« │ ┌─┴─┐ │ │ │ │ │ │ │
1608-
«q_2: ──■──┤ Z ├─X─┤ 2 ├──■────■───┤ 2 ├
1609-
« │ └───┘ │ └─────────┘ │ │ │ │
1610-
« ┌─┴─┐ │ ┌─┴─┐┌─┴──┐│ │
1611-
«q_3: ┤ X ├──────X────────────┤ X ├┤ Sx ├┤ 3 ├
1612-
« └───┘ └───┘└────┘└───────────┘
1604+
« ┌─────────┐ ┌───────────┐
1605+
«q_0: ───────■─────┤ 0 ├──■────■───┤ 0 ├
1606+
« │ │ │ │ │ │ │
1607+
« │ │ │ │ │ │ │
1608+
«q_1: ──■────■───■─┤ 1 Rccx ├──■────■───┤ 1 Rcccx ├
1609+
« │ │ │ │ │ │ │ │ │
1610+
« │ ┌─┴─┐ │ │ │ │ │ │ │
1611+
«q_2: ──■──┤ Z ├─X─┤ 2 ├──■────■───┤ 2 ├
1612+
« │ └───┘ │ └─────────┘ │ │ │ │
1613+
« ┌─┴─┐ │ ┌─┴─┐┌─┴──┐│ │
1614+
«q_3: ┤ X ├──────X────────────┤ X ├┤ Sx ├┤ 3 ├
1615+
« └───┘ └───┘└────┘└───────────┘
16131616
«
1614-
«q_4: ────────────────────────────────────────────────
1617+
«q_4: ────────────────────────────────────────────────
16151618
«
16161619
";
16171620
assert_eq!(result, expected.trim_start_matches("\n"));

0 commit comments

Comments
 (0)