Skip to content

Commit 4192f29

Browse files
authored
Copy over quantum-viz.js source code (microsoft#1973)
This eliminates the dependency on the `@microsoft/quantum-viz.js` package and moves the source over into the `qsharp-lang` package.
1 parent a35d05b commit 4192f29

26 files changed

+2466
-15
lines changed

compiler/qsc_circuit/src/circuit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::Serialize;
99
use std::{fmt::Display, fmt::Write, ops::Not, vec};
1010

1111
/// Representation of a quantum circuit.
12-
/// Implementation of <https://github.com/microsoft/quantum-viz.js/wiki/API-schema-reference>
12+
/// Implementation of `CircuitData` type from `qsharp-lang` npm package.
1313
#[derive(Clone, Serialize, Default, Debug, PartialEq)]
1414
pub struct Circuit {
1515
pub operations: Vec<Operation>,

npm/qsharp/src/browser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,5 @@ export type {
191191
};
192192

193193
export * as utils from "./utils.js";
194+
195+
export type { Circuit as CircuitData } from "./shared/circuit.js";

npm/qsharp/src/compiler/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { type Circuit as CircuitData } from "@microsoft/quantum-viz.js/lib/circuit.js";
4+
import { type Circuit as CircuitData } from "../shared/circuit.js";
55
import {
66
IDocFile,
77
IOperationInfo,

npm/qsharp/src/debug-service/debug-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { type Circuit as CircuitData } from "@microsoft/quantum-viz.js/lib/circuit.js";
4+
import { type Circuit as CircuitData } from "../shared/circuit.js";
55
import type {
66
DebugService,
77
IBreakpointSpan,

npm/qsharp/src/shared/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory contains shared modules to be referenced from both `qsharp/src/` and `qsharp/ux/`.

npm/qsharp/src/shared/circuit.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { Register } from "./register.js";
5+
6+
/**
7+
* Circuit to be visualized.
8+
*/
9+
export interface Circuit {
10+
/** Array of qubit resources. */
11+
qubits: Qubit[];
12+
operations: Operation[];
13+
}
14+
15+
/**
16+
* Represents a unique qubit resource bit.
17+
*/
18+
export interface Qubit {
19+
/** Qubit ID. */
20+
id: number;
21+
/** Number of classical registers attached to quantum register. */
22+
numChildren?: number;
23+
}
24+
25+
/**
26+
* Conditions on when to render the given operation.
27+
*/
28+
export enum ConditionalRender {
29+
/** Always rendered. */
30+
Always,
31+
/** Render classically-controlled operation when measurement is a zero. */
32+
OnZero,
33+
/** Render classically-controlled operation when measurement is a one. */
34+
OnOne,
35+
/** Render operation as a group of its nested operations. */
36+
AsGroup,
37+
}
38+
39+
/**
40+
* Custom data attributes (e.g. data-{attr}="{val}")
41+
*/
42+
export interface DataAttributes {
43+
[attr: string]: string;
44+
}
45+
46+
/**
47+
* Represents an operation and the registers it acts on.
48+
*/
49+
export interface Operation {
50+
/** Gate label. */
51+
gate: string;
52+
/** Formatted gate arguments to be displayed. */
53+
displayArgs?: string;
54+
/** Nested operations within this operation. */
55+
children?: Operation[];
56+
/** Whether gate is a measurement operation. */
57+
isMeasurement: boolean;
58+
/** Whether gate is a conditional operation. */
59+
isConditional: boolean;
60+
/** Whether gate is a controlled operation. */
61+
isControlled: boolean;
62+
/** Whether gate is an adjoint operation. */
63+
isAdjoint: boolean;
64+
/** Control registers the gate acts on. */
65+
controls?: Register[];
66+
/** Target registers the gate acts on. */
67+
targets: Register[];
68+
/** Specify conditions on when to render operation. */
69+
conditionalRender?: ConditionalRender;
70+
/** Custom data attributes to attach to gate element. */
71+
dataAttributes?: DataAttributes;
72+
}

npm/qsharp/src/shared/register.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
/**
5+
* Type of register.
6+
*/
7+
export enum RegisterType {
8+
Qubit,
9+
Classical,
10+
}
11+
12+
/**
13+
* Represents a register resource.
14+
*/
15+
export interface Register {
16+
/** Type of register. If missing defaults to Qubit. */
17+
type?: RegisterType;
18+
/** Qubit register ID. */
19+
qId: number;
20+
/** Classical register ID (if classical register). */
21+
cId?: number;
22+
}
23+
24+
/**
25+
* Metadata for qubit register.
26+
*/
27+
export interface RegisterMetadata {
28+
/** Type of register. */
29+
type: RegisterType;
30+
/** y coord of register */
31+
y: number;
32+
/** Nested classical registers attached to quantum register. */
33+
children?: RegisterMetadata[];
34+
}
35+
36+
/**
37+
* Mapping from qubit IDs to their register metadata.
38+
*/
39+
export interface RegisterMap {
40+
[id: number]: RegisterMetadata;
41+
}

npm/qsharp/ux/circuit-vis/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The code in this folder was copied and adapted from `https://github.com/microsoft/quantum-viz.js`.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
export {
5+
ConditionalRender,
6+
type Circuit,
7+
type DataAttributes,
8+
type Operation,
9+
type Qubit,
10+
} from "../../src/shared/circuit";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
// SVG Namespace
5+
export const svgNS = "http://www.w3.org/2000/svg";
6+
7+
// Display attributes
8+
/** Left padding of SVG. */
9+
export const leftPadding = 20;
10+
/** x coordinate for first operation on each register. */
11+
export const startX = 80;
12+
/** y coordinate of first register. */
13+
export const startY = 40;
14+
/** Minimum width of each gate. */
15+
export const minGateWidth = 40;
16+
/** Height of each gate. */
17+
export const gateHeight = 40;
18+
/** Padding on each side of gate. */
19+
export const gatePadding = 10;
20+
/** Padding on each side of gate label. */
21+
export const labelPadding = 10;
22+
/** Height between each qubit register. */
23+
export const registerHeight: number = gateHeight + gatePadding * 2;
24+
/** Height between classical registers. */
25+
export const classicalRegHeight: number = gateHeight;
26+
/** Group box inner padding. */
27+
export const groupBoxPadding = gatePadding;
28+
/** Padding between nested groups. */
29+
export const nestedGroupPadding = 2;
30+
/** Additional offset for control button. */
31+
export const controlBtnOffset = 40;
32+
/** Control button radius. */
33+
export const controlBtnRadius = 15;
34+
/** Default font size for gate labels. */
35+
export const labelFontSize = 14;
36+
/** Default font size for gate arguments. */
37+
export const argsFontSize = 12;
38+
/** Starting x coord for each register wire. */
39+
export const regLineStart = 40;

0 commit comments

Comments
 (0)