microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
minestarks/circuit-magic

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/npm/qsharp/generate_samples_content.js

65lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// @ts-check
5
6import { readFileSync, writeFileSync } from "node:fs";
7import { dirname, join } from "node:path";
8import { fileURLToPath } from "node:url";
9
10import qSharpSampleList from "../../../samples/samples.mjs";
11import openQasmSampleList from "../../../samples/OpenQASM/samples.mjs";
12
13const thisDir = dirname(fileURLToPath(import.meta.url));
14const qSharpSampleDir = join(thisDir, "..", "..", "..", "samples");
15const openQasmSampleDir = join(
16 thisDir,
17 "..",
18 "..",
19 "..",
20 "samples",
21 "OpenQASM",
22);
23
24const tsDir = join(thisDir, "src");
25const qSharpGeneratedTsPath = join(tsDir, "samples.generated.ts");
26const openQasmGeneratedTsPath = join(tsDir, "openqasm-samples.generated.ts");
27
28embedSampleContentsInTsFile(
29 qSharpSampleList,
30 qSharpSampleDir,
31 qSharpGeneratedTsPath,
32);
33embedSampleContentsInTsFile(
34 openQasmSampleList,
35 openQasmSampleDir,
36 openQasmGeneratedTsPath,
37);
38
39/**
40 * @param {any[]} sampleList
41 * @param {string} sampleDir
42 * @param {import("fs").PathOrFileDescriptor} generatedTsFileName
43 */
44function embedSampleContentsInTsFile(
45 sampleList,
46 sampleDir,
47 generatedTsFileName,
48) {
49 const result = sampleList.map((sample) => {
50 const samplePath = join(sampleDir, sample.file);
51 const sampleText = readFileSync(samplePath, "utf8");
52 return {
53 title: sample.title,
54 shots: sample.shots,
55 code: sampleText,
56 omitFromTests: sample.omitFromTests,
57 };
58 });
59
60 writeFileSync(
61 generatedTsFileName,
62 `export default ${JSON.stringify(result, undefined, 2)}`,
63 "utf-8",
64 );
65}
66