microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/npm/qsharp/src/compiler/common.ts
120lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { 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. |
| 8 | export type Dump = { |
| 9 | [index: string]: [number, number]; |
| 10 | }; |
| 11 | |
| 12 | export 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 | |
| 24 | interface DumpMsg { |
| 25 | type: "DumpMachine"; |
| 26 | state: Dump; |
| 27 | stateLatex: string | null; |
| 28 | qubitCount: number; |
| 29 | } |
| 30 | |
| 31 | interface 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 | |
| 37 | interface MessageMsg { |
| 38 | type: "Message"; |
| 39 | message: string; |
| 40 | } |
| 41 | |
| 42 | interface ResultMsg { |
| 43 | type: "Result"; |
| 44 | result: Result; |
| 45 | } |
| 46 | |
| 47 | type EventMsg = ResultMsg | DumpMsg | MatrixMsg | MessageMsg; |
| 48 | |
| 49 | function 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 | |
| 67 | function 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 | |
| 79 | function 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 | |
| 91 | function 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 | |
| 103 | export function eventStringToMsg(msg: string): EventMsg | null { |
| 104 | return ( |
| 105 | outputAsResult(msg) || |
| 106 | outputAsMessage(msg) || |
| 107 | outputAsDump(msg) || |
| 108 | outputAsMatrix(msg) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | export 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 | |