parser

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package parser implements a hand-written recursive descent parser for OpenQASM source. Both QASM 2 (qreg/creg, measure q -> c) and QASM 3 (qubit/bit, c = measure q) syntax are supported.

Entry points are Parse (from an io.Reader) and ParseString. The returned ir.Circuit contains all declared qubits, classical bits, gate operations, measurements, barriers, resets, and conditionals.

Use WithStrictMode to reject unknown gate names; the default mode treats them as opaque gates.

Package parser implements a hand-written recursive descent parser for OpenQASM 3.0.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(source io.Reader, opts ...Option) (*ir.Circuit, error)

Parse reads OpenQASM 3.0 source and returns a Circuit.

func ParseString

func ParseString(source string, opts ...Option) (*ir.Circuit, error)

ParseString parses OpenQASM 3.0 source from a string.

Example
package main

import (
	"fmt"

	"github.com/splch/goqu/qasm/parser"
)

func main() {
	src := `OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
bit[2] c;
h q[0];
cx q[0], q[1];
c = measure q;`

	c, err := parser.ParseString(src)
	if err != nil {
		panic(err)
	}
	stats := c.Stats()
	fmt.Printf("qubits=%d clbits=%d ops=%d depth=%d\n",
		c.NumQubits(), c.NumClbits(), stats.GateCount, stats.Depth)
}
Output:

qubits=2 clbits=2 ops=4 depth=3

Types

type Option

type Option func(*config)

Option configures parser behavior.

func WithStrictMode

func WithStrictMode(strict bool) Option

WithStrictMode rejects unknown gate names instead of treating them as opaque.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL