microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/ux/circuit-vis/formatters/inputFormatter.ts
92lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT license. |
| 3 | |
| 4 | import { Qubit } from "../circuit"; |
| 5 | import { RegisterType, RegisterMap, RegisterRenderData } from "../register"; |
| 6 | import { |
| 7 | leftPadding, |
| 8 | startY, |
| 9 | registerHeight, |
| 10 | classicalRegHeight, |
| 11 | } from "../constants"; |
| 12 | import { group, text } from "./formatUtils"; |
| 13 | import { mathChars } from "../utils"; |
| 14 | |
| 15 | /** |
| 16 | * `formatInputs` takes in an array of Qubits and outputs the SVG string of formatted |
| 17 | * qubit wires and a mapping from register IDs to register rendering data. |
| 18 | * |
| 19 | * @param qubits List of declared qubits. |
| 20 | * |
| 21 | * @returns returns the SVG string of formatted qubit wires, a mapping from registers |
| 22 | * to y coord and total SVG height. |
| 23 | */ |
| 24 | const formatInputs = ( |
| 25 | qubits: Qubit[], |
| 26 | ): { qubitWires: SVGElement; registers: RegisterMap; svgHeight: number } => { |
| 27 | const qubitWires: SVGElement[] = []; |
| 28 | const registers: RegisterMap = {}; |
| 29 | |
| 30 | let currY: number = startY; |
| 31 | qubits.forEach(({ id, numResults }, wireIndex) => { |
| 32 | // Add qubit wire to list of qubit wires |
| 33 | qubitWires.push(qubitInput(currY, wireIndex, id.toString())); |
| 34 | |
| 35 | // Create qubit register |
| 36 | registers[id] = { type: RegisterType.Qubit, y: currY }; |
| 37 | |
| 38 | // If there are no attached classical registers, increment y by fixed register height |
| 39 | if (numResults == null || numResults === 0) { |
| 40 | currY += registerHeight; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Increment current height by classical register height for attached classical registers |
| 45 | currY += classicalRegHeight; |
| 46 | |
| 47 | // Add classical wires |
| 48 | registers[id].children = Array.from(Array(numResults), () => { |
| 49 | const clsReg: RegisterRenderData = { |
| 50 | type: RegisterType.Classical, |
| 51 | y: currY, |
| 52 | }; |
| 53 | currY += classicalRegHeight; |
| 54 | return clsReg; |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | return { |
| 59 | qubitWires: group(qubitWires, { class: "qubit-input-states" }), |
| 60 | registers, |
| 61 | svgHeight: currY, |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Generate the SVG text component for the input qubit register. |
| 67 | * |
| 68 | * @param y y coord of input wire to render in SVG. |
| 69 | * |
| 70 | * @returns SVG text component for the input register. |
| 71 | */ |
| 72 | const qubitInput = ( |
| 73 | y: number, |
| 74 | wireIndex: number, |
| 75 | subscript?: string, |
| 76 | ): SVGElement => { |
| 77 | const el: SVGElement = text("", leftPadding, y, 16); |
| 78 | |
| 79 | const subtext = subscript |
| 80 | ? `<tspan baseline-shift="sub" font-size="65%">${subscript}</tspan>` |
| 81 | : ""; |
| 82 | |
| 83 | el.innerHTML = `|<tspan class="qs-mathtext">${mathChars.psi}</tspan>${subtext}${mathChars.rangle}</tspan>`; |
| 84 | |
| 85 | el.setAttribute("text-anchor", "start"); |
| 86 | el.setAttribute("dominant-baseline", "middle"); |
| 87 | el.setAttribute("data-wire", wireIndex.toString()); |
| 88 | el.classList.add("qs-maintext"); |
| 89 | return el; |
| 90 | }; |
| 91 | |
| 92 | export { formatInputs, qubitInput }; |
| 93 | |