microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jan

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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

208lines · modeblame

2b583ddaIan Davis3 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
1287d30fScott Carda1 years ago4import {
5CURRENT_VERSION,
6type CircuitGroup as CircuitData,
32f9dba0Scott Carda1 years ago7} from "../data-structures/circuit.js";
2b583ddaIan Davis3 years ago8import type {
9DebugService,
c23c2ae4Mine Starks2 years ago10IBreakpointSpan,
11IQuantumState,
9d35d32eIan Davis3 years ago12IStackFrame,
114ec14fIan Davis2 years ago13IStructStepResult,
14IVariable,
021250d6Bill Ticehurst2 years ago15} from "../../lib/web/qsc_wasm.js";
21a29618Mine Starks2 years ago16import { ProgramConfig } from "../browser.js";
2b583ddaIan Davis3 years ago17import { eventStringToMsg } from "../compiler/common.js";
c23c2ae4Mine Starks2 years ago18import {
19IQscEventTarget,
20QscEventData,
21QscEvents,
22makeEvent,
23} from "../compiler/events.js";
2b583ddaIan Davis3 years ago24import { log } from "../log.js";
c23c2ae4Mine Starks2 years ago25import { IServiceProxy, ServiceProtocol } from "../workers/common.js";
21a29618Mine Starks2 years ago26import { toWasmProgramConfig } from "../compiler/compiler.js";
277ab24dMine Starks2 years ago27
021250d6Bill Ticehurst2 years ago28type QscWasm = typeof import("../../lib/web/qsc_wasm.js");
2b583ddaIan Davis3 years ago29
30// These need to be async/promise results for when communicating across a WebWorker, however
31// for running the debugger in the same thread the result will be synchronous (a resolved promise).
32export interface IDebugService {
21a29618Mine Starks2 years ago33loadProgram(
34program: ProgramConfig,
b43f3f45Mine Starks2 years ago35entry: string | undefined,
ede796dbIan Davis2 years ago36): Promise<string>;
9d35d32eIan Davis3 years ago37getBreakpoints(path: string): Promise<IBreakpointSpan[]>;
7706cffaorpuente-MS1 years ago38getLocalVariables(frameID: number): Promise<Array<IVariable>>;
278d567aIan Davis2 years ago39captureQuantumState(): Promise<Array<IQuantumState>>;
277ab24dMine Starks2 years ago40getCircuit(): Promise<CircuitData>;
9d35d32eIan Davis3 years ago41getStackFrames(): Promise<IStackFrame[]>;
2b583ddaIan Davis3 years ago42evalContinue(
43bps: number[],
b43f3f45Mine Starks2 years ago44eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago45): Promise<IStructStepResult>;
46evalNext(
47bps: number[],
b43f3f45Mine Starks2 years ago48eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago49): Promise<IStructStepResult>;
50evalStepIn(
51bps: number[],
b43f3f45Mine Starks2 years ago52eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago53): Promise<IStructStepResult>;
54evalStepOut(
55bps: number[],
b43f3f45Mine Starks2 years ago56eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago57): Promise<IStructStepResult>;
2b583ddaIan Davis3 years ago58dispose(): Promise<void>;
59}
60
61export type IDebugServiceWorker = IDebugService & IServiceProxy;
62
63export class QSharpDebugService implements IDebugService {
114ec14fIan Davis2 years ago64private wasm: QscWasm;
2b583ddaIan Davis3 years ago65private debugService: DebugService;
66
67constructor(wasm: QscWasm) {
68log.info("Constructing a QSharpDebugService instance");
114ec14fIan Davis2 years ago69this.wasm = wasm;
2b583ddaIan Davis3 years ago70this.debugService = new wasm.DebugService();
71}
72
21a29618Mine Starks2 years ago73async loadProgram(
74program: ProgramConfig,
b43f3f45Mine Starks2 years ago75entry: string | undefined,
ede796dbIan Davis2 years ago76): Promise<string> {
21a29618Mine Starks2 years ago77return this.debugService.load_program(
78toWasmProgramConfig(program, "unrestricted"),
7a91721aAlex Hansen2 years ago79entry,
80);
2b583ddaIan Davis3 years ago81}
82
277ab24dMine Starks2 years ago83async getBreakpoints(path: string): Promise<IBreakpointSpan[]> {
84return this.debugService.get_breakpoints(path).spans;
85}
86
7706cffaorpuente-MS1 years ago87async getLocalVariables(frameID: number): Promise<Array<IVariable>> {
88const variable_list = this.debugService.get_locals(frameID);
277ab24dMine Starks2 years ago89return variable_list.variables;
90}
91
92async captureQuantumState(): Promise<Array<IQuantumState>> {
93const state = this.debugService.capture_quantum_state();
94return state.entries;
95}
96
97async getCircuit(): Promise<CircuitData> {
1287d30fScott Carda1 years ago98const circuit = this.debugService.get_circuit();
99return {
100circuits: [circuit],
101version: CURRENT_VERSION,
102};
277ab24dMine Starks2 years ago103}
104
9d35d32eIan Davis3 years ago105async getStackFrames(): Promise<IStackFrame[]> {
101b4a7bMine Starks2 years ago106return this.debugService.get_stack_frames().frames;
2b583ddaIan Davis3 years ago107}
108
277ab24dMine Starks2 years ago109async evalContinue(
114ec14fIan Davis2 years ago110bps: number[],
b43f3f45Mine Starks2 years ago111eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago112): Promise<IStructStepResult> {
113const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
114const ids = new Uint32Array(bps);
277ab24dMine Starks2 years ago115return this.debugService.eval_continue(event_cb, ids);
114ec14fIan Davis2 years ago116}
117
277ab24dMine Starks2 years ago118async evalNext(
114ec14fIan Davis2 years ago119bps: number[],
b43f3f45Mine Starks2 years ago120eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago121): Promise<IStructStepResult> {
122const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
123const ids = new Uint32Array(bps);
277ab24dMine Starks2 years ago124return this.debugService.eval_next(event_cb, ids);
114ec14fIan Davis2 years ago125}
126
277ab24dMine Starks2 years ago127async evalStepIn(
114ec14fIan Davis2 years ago128bps: number[],
b43f3f45Mine Starks2 years ago129eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago130): Promise<IStructStepResult> {
131const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
132const ids = new Uint32Array(bps);
277ab24dMine Starks2 years ago133return this.debugService.eval_step_in(event_cb, ids);
114ec14fIan Davis2 years ago134}
135
277ab24dMine Starks2 years ago136async evalStepOut(
2b583ddaIan Davis3 years ago137bps: number[],
b43f3f45Mine Starks2 years ago138eventHandler: IQscEventTarget,
114ec14fIan Davis2 years ago139): Promise<IStructStepResult> {
2b583ddaIan Davis3 years ago140const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
141const ids = new Uint32Array(bps);
277ab24dMine Starks2 years ago142return this.debugService.eval_step_out(event_cb, ids);
114ec14fIan Davis2 years ago143}
144
2b583ddaIan Davis3 years ago145async dispose() {
146this.debugService.free();
147}
148}
149
150export function onCompilerEvent(msg: string, eventTarget: IQscEventTarget) {
151const qscMsg = eventStringToMsg(msg);
152if (!qscMsg) {
153log.error("Unknown event message: %s", msg);
154return;
155}
156
157let qscEvent: QscEvents;
158
159const msgType = qscMsg.type;
160switch (msgType) {
161case "Message":
162qscEvent = makeEvent("Message", qscMsg.message);
163break;
164case "DumpMachine":
cc8e7546DmitryVasilevsky2 years ago165qscEvent = makeEvent("DumpMachine", {
166state: qscMsg.state,
167stateLatex: qscMsg.stateLatex,
0c5ae2c5Stefan J. Wernli1 years ago168qubitCount: qscMsg.qubitCount,
cc8e7546DmitryVasilevsky2 years ago169});
2b583ddaIan Davis3 years ago170break;
171case "Result":
172qscEvent = makeEvent("Result", qscMsg.result);
173break;
c8c13383Bill Ticehurst1 years ago174case "Matrix":
175qscEvent = makeEvent("Matrix", {
176matrix: qscMsg.matrix,
177matrixLatex: qscMsg.matrixLatex,
178});
179break;
2b583ddaIan Davis3 years ago180default:
181log.never(msgType);
182throw "Unexpected message type";
183}
184log.debug("worker dispatching event " + JSON.stringify(qscEvent));
185eventTarget.dispatchEvent(qscEvent);
186}
c23c2ae4Mine Starks2 years ago187
188/** The protocol definition to allow running the debugger in a worker. */
189export const debugServiceProtocol: ServiceProtocol<
190IDebugService,
191QscEventData
192> = {
193class: QSharpDebugService,
194methods: {
21a29618Mine Starks2 years ago195loadProgram: "request",
c23c2ae4Mine Starks2 years ago196getBreakpoints: "request",
197getLocalVariables: "request",
198captureQuantumState: "request",
277ab24dMine Starks2 years ago199getCircuit: "request",
c23c2ae4Mine Starks2 years ago200getStackFrames: "request",
201evalContinue: "requestWithProgress",
202evalNext: "requestWithProgress",
203evalStepIn: "requestWithProgress",
204evalStepOut: "requestWithProgress",
205dispose: "request",
206},
c8c13383Bill Ticehurst1 years ago207eventNames: ["DumpMachine", "Message", "Matrix", "Result"],
c23c2ae4Mine Starks2 years ago208};