Skip to content

Commit 4dc9e93

Browse files
Update Qubit Line Labels in Circuit Viz (#2308)
Changes the labels at the beginning of the qubit lines in circuit visualizer. From: ![image](https://github.com/user-attachments/assets/01dc08a2-a99d-4295-b4e6-c7e09f7a7a25) To: ![image](https://github.com/user-attachments/assets/db0d878f-55b0-482f-b124-dfe7a8017276) --------- Co-authored-by: Mine Starks <[email protected]> Co-authored-by: Mine Starks <[email protected]>
1 parent 7313a6e commit 4dc9e93

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

npm/qsharp/ux/circuit-vis/formatters/inputFormatter.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const formatInputs = (
2929
let currY: number = startY;
3030
qubits.forEach(({ id, numResults }) => {
3131
// Add qubit wire to list of qubit wires
32-
qubitWires.push(_qubitInput(currY));
32+
qubitWires.push(_qubitInput(currY, id.toString()));
3333

3434
// Create qubit register
3535
registers[id] = { type: RegisterType.Qubit, y: currY };
@@ -68,8 +68,39 @@ const formatInputs = (
6868
*
6969
* @returns SVG text component for the input register.
7070
*/
71-
const _qubitInput = (y: number): SVGElement => {
72-
const el: SVGElement = text("|𝜓⟩", leftPadding, y, 16);
71+
const _qubitInput = (y: number, subscript?: string): SVGElement => {
72+
const el: SVGElement = text("", leftPadding, y, 16);
73+
74+
// Create the main text node
75+
const mainText = document.createElementNS(
76+
"http://www.w3.org/2000/svg",
77+
"tspan",
78+
);
79+
mainText.textContent = "|𝜓";
80+
81+
// Create the subscript node if provided
82+
if (subscript) {
83+
const subscriptText = document.createElementNS(
84+
"http://www.w3.org/2000/svg",
85+
"tspan",
86+
);
87+
subscriptText.textContent = subscript;
88+
subscriptText.setAttribute("baseline-shift", "sub");
89+
subscriptText.setAttribute("font-size", "65%");
90+
mainText.appendChild(subscriptText);
91+
}
92+
93+
// Add the closing part of the text
94+
const closingText = document.createElementNS(
95+
"http://www.w3.org/2000/svg",
96+
"tspan",
97+
);
98+
closingText.textContent = "⟩";
99+
100+
// Append all parts to the main SVG text element
101+
el.appendChild(mainText);
102+
el.appendChild(closingText);
103+
73104
el.setAttribute("text-anchor", "start");
74105
el.setAttribute("dominant-baseline", "middle");
75106
return el;

npm/qsharp/ux/circuit-vis/formatters/registerFormatter.ts

+10-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { RegisterMap } from "../register";
55
import { regLineStart } from "../constants";
66
import { Metadata, GateType } from "../metadata";
7-
import { group, line, text } from "./formatUtils";
7+
import { group, line } from "./formatUtils";
88

99
/**
1010
* Generate the SVG representation of the qubit register wires in `registers` and the classical wires
@@ -24,7 +24,15 @@ const formatRegisters = (
2424
const formattedRegs: SVGElement[] = [];
2525
// Render qubit wires
2626
for (const qId in registers) {
27-
formattedRegs.push(_qubitRegister(Number(qId), endX, registers[qId].y));
27+
formattedRegs.push(
28+
line(
29+
regLineStart,
30+
registers[qId].y,
31+
endX,
32+
registers[qId].y,
33+
"qubit-wire",
34+
),
35+
);
2836
}
2937
// Render classical wires
3038
measureGates.forEach(({ type, x, targetsY, controlsY }) => {
@@ -89,30 +97,4 @@ const _classicalRegister = (
8997
return group([vLine1, vLine2, hLine1, hLine2]);
9098
};
9199

92-
/**
93-
* Generates the SVG representation of a qubit register.
94-
*
95-
* @param qId Qubit register index.
96-
* @param endX End x coord.
97-
* @param y y coord of wire.
98-
* @param labelOffset y offset for wire label.
99-
*
100-
* @returns SVG representation of the given qubit register.
101-
*/
102-
const _qubitRegister = (
103-
qId: number,
104-
endX: number,
105-
y: number,
106-
labelOffset = 16,
107-
): SVGElement => {
108-
const wire: SVGElement = line(regLineStart, y, endX, y, "qubit-wire");
109-
110-
const label: SVGElement = text(`q${qId}`, regLineStart, y - labelOffset);
111-
label.setAttribute("dominant-baseline", "hanging");
112-
label.setAttribute("text-anchor", "start");
113-
label.setAttribute("font-size", "75%");
114-
115-
return group([wire, label]);
116-
};
117-
118100
export { formatRegisters };

0 commit comments

Comments
 (0)