npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

q-sharp-ts

v0.1.6

Published

A parser for Q# language features, implemented in TypeScript.

Downloads

13

Readme

Q Sharp TS

NPM Downloads

Q#, the high-level quantum programming language for quantum circuit specification, implemented in TypeScript.

Language documentation is provided by MicroSoft here.

NOTE: this version 0 package is experimental!

New in Version 0.1.5

  • Fixed range type array representation bug.

Usage

Import the parse function or parseString function from the package.

import { parse, parseString } from 'q-sharp-ts';

parse can be called with a file path to a .qs file. It will parse the file and return the abstract syntax tree representation.

let ast = parse("<file-path>");

parseString should be called with a string of Q# code. It will parse the code and return the abstract syntax tree representation. parseString also takes the same optional arguments as parse.

let ast = parseString("<qs-string>");

Example I/O

Q# Operation:

operation ReflectAboutMarked(inputQubits : Qubit[]) : Unit {
  use outputQubit = Qubit();
  within {
    // We initialize the outputQubit to (|0> = |1>) / sqrt(2), so that
    // toggling it results in a (=1) phase.
    X(outputQubit);
    H(outputQubit);
    // Flip the outputQubit for marked states.
    // Here, we get the state with alternating 0s and 1s by using the X
    // operation on every other qubit.
    for q in inputQubits [...2...] {
      X(q);
    }
  } apply {
    Controlled X(inputQubits, outputQubit);
  }
}

Parsed Abstract Syntax Tree Segment (we have added comments):

// operation
{
    "name": "ReflectAboutMarked",
    "nodes": [
        // qubit allocation
        {
            "name": {
                "repr": "outputQubit",
                "val": "outputQubit"
            },
            "qubits": {
                "repr": "outputQubit",
                "name": "outputQubit",
                "length": {
                    "repr": "1",
                    "val": 1
                }
            }
        },
        // within / apply closure
        {
            "within": [
                // comment
                {
                    "val": " We initialize the outputQubit to (|0> - |1>) / sqrt(2), so that"
                },
                // comment
                {
                    "val": " toggling it results in a (-1) phase."
                },
                // Pauli X gate
                {
                    "target": {
                        "repr": "outputQubit",
                        "id": "outputQubit"
                    }
                },
                // Hadamard gate
                {
                    "target": {
                        "repr": "outputQubit",
                        "id": "outputQubit"
                    }
                },
                // comment
                {
                    "val": " Flip the outputQubit for marked states."
                },
                // comment
                {
                    "val": " Here, we get the state with alternating 0s and 1s by using the X"
                },
                // comment
                {
                    "val": " operation on every other qubit."
                },
                // for loop
                {
                    "variable": {
                        "repr": "q",
                        "name": "q"
                    },
                    "inside": [
                        // Pauli X gate
                        {
                            "target": {
                                "repr": "q",
                                "id": "q"
                            }
                        }
                    ],
                    "vals": {
                        "repr": "inputQubits[...2...]",
                        "vals": [
                            // expression for loop iterable
                            {
                                "repr": "inputQubits[...2...]",
                                "instance": "inputQubits",
                                "index": {
                                    "repr": "...2...",
                                    "lower": {},
                                    "upper": {}
                                }
                            }
                        ],
                        "size": 1
                    }
                }
            ],
            "applies": [
                // controlled X gate
                {
                    "control": {
                        "repr": "inputQubits",
                        "id": "inputQubits"
                    },
                    "target": {
                        "repr": "outputQubit",
                        "id": "outputQubit"
                    }
                }
            ]
        }
    ],
    "params": [
        [
            // input parameter expression
            {
                "repr": "inputQubits",
                "elements": [
                    {
                        "repr": "inputQubits",
                        "id": "inputQubits"
                    }
                ]
            }
        ]
    ],
    // the operation has no modifiers
    "modifiers": [],
    // the operation does not return anything
    "returnType": {}
}

To demonstrate how the `Expressions' are parsed, we also include the following example from the same Grover.qs file.

function CalculateOptimalIterations(nQubits : Int) : Int {
    if nQubits > 63 {
        fail "This sample supports at most 63 qubits.";
    }
    let nItems = 1 <<< nQubits; // 2^nQubits
    let angle = ArcSin(1. / Sqrt(IntAsDouble(nItems)));
    let iterations = Round(0.25 * PI() / angle - 0.5);
    return iterations;
}

Parsed Function:

    // function
    {
        "name": "CalculateOptimalIterations",
        "nodes": [
          // if
          {
            "condition": 
            // condition expression
            {
              "repr": "nQubits>63",
              "elements": [
                // qubit identifier
                {
                  "repr": "nQubits",
                  "id": "nQubits"
                },
                // greater than operator
                {
                  "repr": ">"
                },
                // integer literal
                {
                  "repr": "63",
                  "val": 63
                }
              ]
            },
            "ifClause": [
                // fail statement
              {
                "msg": {
                  "repr": [
                    51,
                    "\"This sample supports at most 63 qubits.\""
                  ],
                  "val": [
                    51,
                    "\"This sample supports at most 63 qubits.\""
                  ]
                }
              }
            ]
          },
          // let variable declaration and assignment
          {
            "expression": 
            // right hand side expression
            {
              "repr": "1<<<nQubits",
              "elements": [
                // literal
                {
                  "repr": "1",
                  "val": 1
                },
                // bitwise operator
                {
                  "repr": "<<<"
                },
                // qubit identifier
                {
                  "repr": "nQubits",
                  "id": "nQubits"
                }
              ]
            },
            "variable": {
              "repr": "nItems",
              "name": "nItems"
            }
          },
          // let variable declaration and assignment
          {
            "expression": 
            // right hand side expression
            {
              "repr": "ArcSin(1/Sqrt(IntAsDouble(nItems, ), ), )",
              "elements": [
                // ArcSin function call
                {
                  "repr": "ArcSin(1/Sqrt(IntAsDouble(nItems, ), ), )",
                  "name": "ArcSin",
                  "params": [
                    [
                      // ArcSin parameter expression
                      {
                        "repr": "1/Sqrt(IntAsDouble(nItems, ), )",
                        "elements": [
                          // Double literal
                          {
                            "repr": "1",
                            "val": 1
                          },
                          // divide by operator
                          {
                            "repr": "/"
                          },
                          // Sqrt function call
                          {
                            "repr": "Sqrt(IntAsDouble(nItems, ), )",
                            "name": "Sqrt",
                            "params": [
                              [
                                // Sqrt function parameter expression
                                {
                                  "repr": "IntAsDouble(nItems, )",
                                  "elements": [
                                    //  IntAsDouble function call
                                    {
                                      "repr": "IntAsDouble(nItems, )",
                                      "name": "IntAsDouble",
                                      "params": [
                                        [
                                          // IntAsDouble function param expression
                                          {
                                            "repr": "nItems",
                                            "elements": [
                                              // variable reference
                                              {
                                                "repr": "nItems",
                                                "name": "nItems"
                                              }
                                            ]
                                          }
                                        ]
                                      ]
                                    }
                                  ]
                                }
                              ]
                            ]
                          }
                        ]
                      }
                    ]
                  ]
                }
              ]
            },
            "variable": {
              "repr": "angle",
              "name": "angle"
            }
          },
          {
            "expression": 
            // let variable declaration and assignment
            {
              "repr": "Round(0.25*PI()/angle-0.5, )",
              "elements": [
                // right hand side expression
                {
                  "repr": "Round(0.25*PI()/angle-0.5, )",
                  "name": "Round",
                  "params": [
                    [
                      // Round function parameter expression
                      {
                        "repr": "0.25*PI()/angle-0.5",
                        "elements": [
                          // Double literal
                          {
                            "repr": "0.25",
                            "val": 0.25
                          },
                          // multiplier operator
                          {
                            "repr": "*"
                          },
                          // PI function call
                          {
                            "repr": "PI()",
                            "name": "PI",
                            "params": []
                          },
                          // divide operator
                          {
                            "repr": "/"
                          },
                          // variable reference
                          {
                            "repr": "angle",
                            "name": "angle"
                          },
                          // minus operator
                          {
                            "repr": "-"
                          },
                          // Double literal
                          {
                            "repr": "0.5",
                            "val": 0.5
                          }
                        ]
                      }
                    ]
                  ]
                }
              ]
            },
            "variable": {
              "repr": "iterations",
              "name": "iterations"
            }
          },
          // return statement 
          {
            "expr": {
              "repr": "iterations",
              "elements": [
                // return statement expression
                {
                  "repr": "iterations",
                  "name": "iterations"
                }
              ]
            }
          }
        ],
        "params": [
          [
            // function parameters expression
            {
              "repr": "nQubits",
              "elements": [
                {
                  "repr": "nQubits",
                  "id": "nQubits"
                }
              ]
            }
          ]
        ]
      }

Transpiling

tsc src/*.ts --outDir dist

Installing dependencies

npm install

Source code

Feel free to clone, fork, comment or contribute on GitHub!

License

Copyright 2025 Marcus Edwards

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

How to Cite

If you are using Q-SHARP-TS for research we appreciate any citations. Please read and cite our pre-print.

@misc{edwards2025compilingqsubsetqasm,
      title={Compiling a Q# Subset to QASM 3.0 in TypeScript via a JSON Based IR}, 
      author={Marcus Edwards},
      year={2025},
      eprint={2506.23407},
      archivePrefix={arXiv},
      primaryClass={cs.PL},
      url={https://arxiv.org/abs/2506.23407}, 
}