microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/npm/qsharp/src/compiler/common.ts

120lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4import { IQSharpError, type VSDiagnostic } from "../../lib/web/qsc_wasm.js";
5
6// Each DumpMachine output is represented as an object where each key is a basis
7// state, e.g., "|3>" and the value is the [real, imag] parts of the complex amplitude.
8export type Dump = {
9 [index: string]: [number, number];
10};
11
12export type Result =
13 | { success: true; value: string }
14 | {
15 success: false;
16 /**
17 * The `VSDiagnostic` variant is deprecated and kept for backward compatibility.
18 * Check type at runtime and prefer `IQSharpError[]` when possible, as it
19 * contains all the information in `VSDiagnostic` plus additional fields.
20 */
21 value: VSDiagnostic & { errors: IQSharpError[] };
22 };
23
24interface DumpMsg {
25 type: "DumpMachine";
26 state: Dump;
27 stateLatex: string | null;
28 qubitCount: number;
29}
30
31interface MatrixMsg {
32 type: "Matrix";
33 matrix: number[][][]; // Array or rows, which are an array of elements, which are complex numbers as [re, im]
34 matrixLatex: string;
35}
36
37interface MessageMsg {
38 type: "Message";
39 message: string;
40}
41
42interface ResultMsg {
43 type: "Result";
44 result: Result;
45}
46
47type EventMsg = ResultMsg | DumpMsg | MatrixMsg | MessageMsg;
48
49function outputAsResult(msg: string): ResultMsg | null {
50 try {
51 const obj = JSON.parse(msg);
52 if (obj?.type == "Result" && typeof obj.success == "boolean") {
53 return {
54 type: "Result",
55 result: {
56 success: obj.success,
57 value: obj.result,
58 },
59 };
60 }
61 } catch {
62 return null;
63 }
64 return null;
65}
66
67function outputAsMessage(msg: string): MessageMsg | null {
68 try {
69 const obj = JSON.parse(msg);
70 if (obj?.type == "Message" && typeof obj.message == "string") {
71 return obj as MessageMsg;
72 }
73 } catch {
74 return null;
75 }
76 return null;
77}
78
79function outputAsDump(msg: string): DumpMsg | null {
80 try {
81 const obj = JSON.parse(msg);
82 if (obj?.type == "DumpMachine" && typeof obj.state == "object") {
83 return obj as DumpMsg;
84 }
85 } catch {
86 return null;
87 }
88 return null;
89}
90
91function outputAsMatrix(msg: string): MatrixMsg | null {
92 try {
93 const obj = JSON.parse(msg);
94 if (obj?.type == "Matrix" && Array.isArray(obj.matrix)) {
95 return obj as MatrixMsg;
96 }
97 } catch {
98 return null;
99 }
100 return null;
101}
102
103export function eventStringToMsg(msg: string): EventMsg | null {
104 return (
105 outputAsResult(msg) ||
106 outputAsMessage(msg) ||
107 outputAsDump(msg) ||
108 outputAsMatrix(msg)
109 );
110}
111
112export type ShotResult = {
113 success: boolean;
114 /**
115 * `VSDiagnostic` is kept for backward compatibility, check runtime type
116 * for `IQSharpError[]` and prefer that when possible.
117 */
118 result: string | (VSDiagnostic & { errors: IQSharpError[] });
119 events: Array<MessageMsg | DumpMsg | MatrixMsg>;
120};
121