Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit ac3e656

Browse files
theRoughCodeRaphael Koh
authored andcommitted
Add path visualizer JS files (#201)
1 parent 3755860 commit ac3e656

File tree

15 files changed

+1252
-7
lines changed

15 files changed

+1252
-7
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Enum for the various gate operations handled.
3+
*/
4+
export enum GateType {
5+
/** Measurement gate. */
6+
Measure,
7+
/** CNOT gate. */
8+
Cnot,
9+
/** SWAP gate. */
10+
Swap,
11+
/** Single/multi qubit unitary gate. */
12+
Unitary,
13+
/** Single/multi controlled unitary gate. */
14+
ControlledUnitary,
15+
/** Nested group of classically-controlled gates. */
16+
ClassicalControlled,
17+
/** Invalid gate. */
18+
Invalid
19+
};
20+
21+
// Display attributes
22+
/** Left padding of SVG. */
23+
export const leftPadding: number = 20;
24+
/** x coordinate for first operation on each register. */
25+
export const startX: number = 80;
26+
/** y coordinate of first register. */
27+
export const startY: number = 40;
28+
/** Minimum width of each gate. */
29+
export const minGateWidth: number = 40;
30+
/** Height of each gate. */
31+
export const gateHeight: number = 40;
32+
/** Padding on each side of gate. */
33+
export const gatePadding: number = 10;
34+
/** Padding on each side of gate label. */
35+
export const labelPadding: number = 10;
36+
/** Height between each qubit register. */
37+
export const registerHeight: number = gateHeight + gatePadding * 2;
38+
/** Height between classical registers. */
39+
export const classicalRegHeight: number = gateHeight;
40+
/** Classical box inner padding. */
41+
export const classicalBoxPadding: number = 15;
42+
/** Additional offset for control button. */
43+
export const controlBtnOffset: number = 40;
44+
/** Control button radius. */
45+
export const controlBtnRadius: number = 15;
46+
/** Default font size for gate labels. */
47+
export const labelFontSize: number = 14;
48+
/** Default font size for gate arguments. */
49+
export const argsFontSize: number = 12;
50+
/** Starting x coord for each register wire. */
51+
export const regLineStart: number = 40;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Register } from "./register";
2+
3+
/**
4+
* Structure of JSON representation of the execution path of a Q# operation.
5+
*/
6+
export interface ExecutionPath {
7+
/** Array of qubit resources. */
8+
qubits: Qubit[];
9+
operations: Operation[];
10+
};
11+
12+
/**
13+
* Represents a unique qubit resource bit.
14+
*/
15+
export interface Qubit {
16+
/** Qubit ID. */
17+
id: number;
18+
/** Number of classical registers attached to quantum register. */
19+
numChildren?: number;
20+
};
21+
22+
/**
23+
* Represents an operation and the registers it acts on.
24+
*/
25+
export interface Operation {
26+
/** Gate label. */
27+
gate: string;
28+
/** Gate arguments as string. */
29+
argStr?: string,
30+
/** Classically-controlled gates.
31+
* - children[0]: gates when classical control bit is 0.
32+
* - children[1]: gates when classical control bit is 1.
33+
*/
34+
children?: Operation[][];
35+
/** Whether gate is a controlled operation. */
36+
controlled: boolean;
37+
/** Whether gate is an adjoint operation. */
38+
adjoint: boolean;
39+
/** Control registers the gate acts on. */
40+
controls: Register[];
41+
/** Target registers the gate acts on. */
42+
targets: Register[];
43+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { labelFontSize } from "../constants";
2+
3+
// Helper functions for basic SVG components
4+
5+
/**
6+
* Given an array of SVG elements, group them as an SVG group using the `<g>` tag.
7+
*
8+
* @param svgElems Array of SVG elements.
9+
*
10+
* @returns SVG string for grouped elements.
11+
*/
12+
export const group = (...svgElems: (string | string[])[]): string =>
13+
['<g>', ...svgElems.flat(), '</g>'].join('\n');
14+
15+
/**
16+
* Generate the SVG representation of a control dot used for controlled operations.
17+
*
18+
* @param x x coord of circle.
19+
* @param y y coord of circle.
20+
* @param radius Radius of circle.
21+
*
22+
* @returns SVG string for control dot.
23+
*/
24+
export const controlDot = (x: number, y: number, radius: number = 5): string =>
25+
`<circle cx="${x}" cy="${y}" r="${radius}" stroke="black" fill="black" stroke-width="1"></circle>`;
26+
27+
/**
28+
* Generate an SVG line.
29+
*
30+
* @param x1 x coord of starting point of line.
31+
* @param y1 y coord of starting point of line.
32+
* @param x2 x coord of ending point of line.
33+
* @param y2 y coord fo ending point of line.
34+
* @param strokeWidth Stroke width of line.
35+
*
36+
* @returns SVG string for line.
37+
*/
38+
export const line = (x1: number, y1: number, x2: number, y2: number, strokeWidth: number = 1): string =>
39+
`<line x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}" stroke="black" stroke-width="${strokeWidth}"></line>`;
40+
41+
/**
42+
* Generate the SVG representation of a unitary box that represents an arbitrary unitary operation.
43+
*
44+
* @param x x coord of box.
45+
* @param y y coord of box.
46+
* @param width Width of box.
47+
* @param height Height of box.
48+
*
49+
* @returns SVG string for unitary box.
50+
*/
51+
export const box = (x: number, y: number, width: number, height: number): string =>
52+
`<rect x="${x}" y="${y}" width="${width}" height="${height}" stroke="black" fill="white" stroke-width="1"></rect>`;
53+
54+
/**
55+
* Generate the SVG text element from a given text string.
56+
*
57+
* @param text String to render as SVG text.
58+
* @param x Middle x coord of text.
59+
* @param y Middle y coord of text.
60+
* @param fs Font size of text.
61+
*
62+
* @returns SVG string for text.
63+
*/
64+
export const text = (text: string, x: number, y: number, fs: number = labelFontSize): string =>
65+
`<text font-size="${fs}" font-family="Arial" x="${x}" y="${y}" dominant-baseline="middle" text-anchor="middle" fill="black">${text}</text>`;
66+
67+
/**
68+
* Generate the SVG representation of the arc used in the measurement box.
69+
*
70+
* @param x x coord of arc.
71+
* @param y y coord of arc.
72+
* @param rx x radius of arc.
73+
* @param ry y radius of arc.
74+
*
75+
* @returns SVG string for arc.
76+
*/
77+
export const arc = (x: number, y: number, rx: number, ry: number): string =>
78+
`<path d="M ${x + 2 * rx} ${y} A ${rx} ${ry} 0 0 0 ${x} ${y}" stroke="black" fill="none" stroke-width="1"></path>`;
79+
80+
/**
81+
* Generate a dashed SVG line.
82+
*
83+
* @param x1 x coord of starting point of line.
84+
* @param y1 y coord of starting point of line.
85+
* @param x2 x coord of ending point of line.
86+
* @param y2 y coord fo ending point of line.
87+
*
88+
* @returns SVG string for dashed line.
89+
*/
90+
export const dashedLine = (x1: number, y1: number, x2: number, y2: number): string =>
91+
`<line x1="${x1}" x2="${x2}" y1="${y1}" y2="${y2}" stroke="black" stroke-dasharray="8, 8" stroke-width="1"></line>`;
92+
93+
/**
94+
* Generate the SVG representation of the dashed box used for enclosing groups of operations controlled on a classical register.
95+
*
96+
* @param x x coord of box.
97+
* @param y y coord of box.
98+
* @param width Width of box.
99+
* @param height Height of box.
100+
*
101+
* @returns SVG string for dashed box.
102+
*/
103+
export const dashedBox = (x: number, y: number, width: number, height: number): string =>
104+
`<rect x="${x}" y ="${y}" width="${width}" height="${height}" stroke="black" fill-opacity="0" stroke-dasharray="8, 8" stroke-width="1"></rect>`;

0 commit comments

Comments
 (0)