qasm-ts - v2.0.0
    Preparing search index...

    Function parseString

    • Parses OpenQASM code from a string and returns the abstract syntax tree.

      This is the primary entry point for parsing OpenQASM code. It handles both OpenQASM 2.0 and 3.0 syntax, automatically selecting the appropriate lexer and parser based on the version parameter.

      Parameters

      • qasm: string

        The OpenQASM code string to parse

      • Optionalversion: number | OpenQASMVersion

        The OpenQASM version to use (defaults to 3.0)

      • Optionalverbose: boolean

        Whether to include class names in the output (defaults to false)

      • Optionalstringify: boolean

        Whether to return stringified JSON (defaults to false)

      Returns any

      The corresponding AST as an array of nodes, or stringified JSON if stringify is true

      import { parseString } from 'qasm-ts';

      const qasmCode = `
      OPENQASM 3.0;
      include "stdgates.inc";
      qubit[2] q;
      h q[0];
      cx q[0], q[1];
      `;

      const ast = parseString(qasmCode);
      console.log(ast);
      const qasm2Code = `
      OPENQASM 2.0;
      include "qelib1.inc";
      qreg q[2];
      creg c[2];
      h q[0];
      cx q[0],q[1];
      measure q -> c;
      `;

      const ast = parseString(qasm2Code, 2);
      const ast = parseString(qasmCode, 3, true);
      // Output will include __className__ properties for each node