visitor

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	Break    = "break;"
	Continue = "continue;"
)

Variables

View Source
var (
	ErrAlreadyDeclared      = errors.New("already declared")
	ErrIdentifierNotFound   = errors.New("identifier not found")
	ErrQubitNotFound        = errors.New("qubit not found")
	ErrClassicalBitNotFound = errors.New("classical bit not found")
	ErrVariableNotFound     = errors.New("variable not found")
	ErrGateNotFound         = errors.New("gate not found")
	ErrFunctionNotFound     = errors.New("function not found")
	ErrUnexpected           = errors.New("unexpected")
	ErrNotImplemented       = errors.New("not implemented")
)
View Source
var Const = map[string]float64{
	"pi":    math.Pi,
	"tau":   2 * math.Pi,
	"euler": math.E,
	"π":     math.Pi,
	"τ":     2 * math.Pi,
	"ℇ":     math.E,
}

Functions

This section is empty.

Types

type Environ

type Environ struct {
	Version      string
	Const        map[string]interface{}
	Variable     map[string]interface{}
	Qubit        map[string][]q.Qubit
	ClassicalBit map[string][]int64
	Gate         map[string]Gate
	Subroutine   map[string]Subroutine
	Outer        *Environ
}

func NewEnviron

func NewEnviron() *Environ

func (*Environ) GetClassicalBit

func (e *Environ) GetClassicalBit(name string) ([]int64, bool)

func (*Environ) GetConst

func (e *Environ) GetConst(name string) (interface{}, bool)

func (*Environ) GetGate

func (e *Environ) GetGate(name string) (Gate, bool)
Example
package main

import (
	"fmt"

	"github.com/itsubaki/qasm/visitor"
)

func main() {
	env := visitor.NewEnviron()
	_, ok := env.GetGate("x")
	fmt.Println(ok)

}
Output:

false

func (*Environ) GetQubit

func (e *Environ) GetQubit(name string) ([]q.Qubit, bool)

func (*Environ) GetSubroutine

func (e *Environ) GetSubroutine(name string) (Subroutine, bool)
Example
package main

import (
	"fmt"

	"github.com/itsubaki/qasm/visitor"
)

func main() {
	env := visitor.NewEnviron()
	env.Subroutine["x"] = visitor.Subroutine{
		Name: "x",
	}

	enclosed := env.NewEnclosed()
	sub, ok := enclosed.GetSubroutine("x")
	fmt.Println(sub.Name, ok)

}
Output:

x true

func (*Environ) GetVariable

func (e *Environ) GetVariable(name string) (interface{}, bool)

func (*Environ) NewEnclosed

func (e *Environ) NewEnclosed() *Environ
Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
	"github.com/itsubaki/qasm/visitor"
)

func main() {
	env := visitor.NewEnviron()
	env.Qubit["q0"] = []q.Qubit{0, 1}
	env.ClassicalBit["c0"] = []int64{0, 1}

	enclosed := env.NewEnclosed()
	enclosed.Qubit["q1"] = []q.Qubit{2, 3}
	enclosed.ClassicalBit["c1"] = []int64{2, 3}

	fmt.Println(enclosed.GetQubit("q0"))
	fmt.Println(enclosed.GetQubit("q1"))
	fmt.Println(enclosed.GetClassicalBit("c0"))
	fmt.Println(enclosed.GetClassicalBit("c1"))

}
Output:

[0 1] true
[2 3] true
[0 1] true
[2 3] true

func (*Environ) SetVariable

func (e *Environ) SetVariable(name string, value interface{})
Example
package main

import (
	"fmt"

	"github.com/itsubaki/qasm/visitor"
)

func main() {
	env := visitor.NewEnviron()

	env.SetVariable("a", 42)
	fmt.Println("env:", env.Variable)

	enclosed := env.NewEnclosed()
	enclosed.SetVariable("a", 43)
	fmt.Println("env:", env.Variable)
	fmt.Println("enclosed:", enclosed.Variable)

	enclosed.SetVariable("b", 100)
	enclosed.SetVariable("b", 101)
	fmt.Println("env:", env.Variable)
	fmt.Println("enclosed:", enclosed.Variable)

}
Output:

env: map[a:42]
env: map[a:43]
enclosed: map[]
env: map[a:43]
enclosed: map[b:101]

type Gate

type Gate struct {
	Name   string
	Params []string
	QArgs  []string
	Body   []*parser.GateCallStatementContext
}

type Subroutine

type Subroutine struct {
	Name            string
	QArgs           []string
	Body            *parser.ScopeContext
	ReturnSignature *parser.ReturnSignatureContext
}

type Visitor

type Visitor struct {
	Environ *Environ
	// contains filtered or unexported fields
}

func New

func New(qsim *q.Q, env *Environ) *Visitor

func (*Visitor) Builtin

func (*Visitor) Defined

func (*Visitor) Enclosed

func (v *Visitor) Enclosed() *Visitor

func (*Visitor) MeasureAssignment

func (v *Visitor) MeasureAssignment(identifier parser.IIndexedIdentifierContext, measure parser.IMeasureExpressionContext) error

func (*Visitor) Modify

func (v *Visitor) Modify(u matrix.Matrix, qargs []q.Qubit, modifier []parser.IGateModifierContext) (matrix.Matrix, error)

func (*Visitor) Params

func (v *Visitor) Params(xlist parser.IExpressionListContext) ([]float64, error)

func (*Visitor) Visit

func (v *Visitor) Visit(tree antlr.ParseTree) interface{}

func (*Visitor) VisitAdditiveExpression

func (v *Visitor) VisitAdditiveExpression(ctx *parser.AdditiveExpressionContext) interface{}

func (*Visitor) VisitAliasDeclarationStatement

func (v *Visitor) VisitAliasDeclarationStatement(ctx *parser.AliasDeclarationStatementContext) interface{}

func (*Visitor) VisitAliasExpression

func (v *Visitor) VisitAliasExpression(ctx *parser.AliasExpressionContext) interface{}

func (*Visitor) VisitAnnotation

func (v *Visitor) VisitAnnotation(ctx *parser.AnnotationContext) interface{}

func (*Visitor) VisitArgumentDefinition

func (v *Visitor) VisitArgumentDefinition(ctx *parser.ArgumentDefinitionContext) interface{}

func (*Visitor) VisitArgumentDefinitionList

func (v *Visitor) VisitArgumentDefinitionList(ctx *parser.ArgumentDefinitionListContext) interface{}

func (*Visitor) VisitArrayLiteral

func (v *Visitor) VisitArrayLiteral(ctx *parser.ArrayLiteralContext) interface{}

func (*Visitor) VisitArrayReferenceType

func (v *Visitor) VisitArrayReferenceType(ctx *parser.ArrayReferenceTypeContext) interface{}

func (*Visitor) VisitArrayType

func (v *Visitor) VisitArrayType(ctx *parser.ArrayTypeContext) interface{}

func (*Visitor) VisitAssignmentStatement

func (v *Visitor) VisitAssignmentStatement(ctx *parser.AssignmentStatementContext) interface{}

func (*Visitor) VisitBarrierStatement

func (v *Visitor) VisitBarrierStatement(ctx *parser.BarrierStatementContext) interface{}

func (*Visitor) VisitBitshiftExpression

func (v *Visitor) VisitBitshiftExpression(ctx *parser.BitshiftExpressionContext) interface{}

func (*Visitor) VisitBitwiseAndExpression

func (v *Visitor) VisitBitwiseAndExpression(ctx *parser.BitwiseAndExpressionContext) interface{}

func (*Visitor) VisitBitwiseOrExpression

func (v *Visitor) VisitBitwiseOrExpression(ctx *parser.BitwiseOrExpressionContext) interface{}

func (*Visitor) VisitBitwiseXorExpression

func (v *Visitor) VisitBitwiseXorExpression(ctx *parser.BitwiseXorExpressionContext) interface{}

func (*Visitor) VisitBoxStatement

func (v *Visitor) VisitBoxStatement(ctx *parser.BoxStatementContext) interface{}

func (*Visitor) VisitBreakStatement

func (v *Visitor) VisitBreakStatement(ctx *parser.BreakStatementContext) interface{}

func (*Visitor) VisitCalStatement

func (v *Visitor) VisitCalStatement(ctx *parser.CalStatementContext) interface{}

func (*Visitor) VisitCalibrationGrammarStatement

func (v *Visitor) VisitCalibrationGrammarStatement(ctx *parser.CalibrationGrammarStatementContext) interface{}

func (*Visitor) VisitCallExpression

func (v *Visitor) VisitCallExpression(ctx *parser.CallExpressionContext) interface{}

func (*Visitor) VisitCastExpression

func (v *Visitor) VisitCastExpression(ctx *parser.CastExpressionContext) interface{}

func (*Visitor) VisitChildren

func (v *Visitor) VisitChildren(node antlr.RuleNode) interface{}

func (*Visitor) VisitClassicalDeclarationStatement

func (v *Visitor) VisitClassicalDeclarationStatement(ctx *parser.ClassicalDeclarationStatementContext) interface{}

func (*Visitor) VisitComparisonExpression

func (v *Visitor) VisitComparisonExpression(ctx *parser.ComparisonExpressionContext) interface{}

func (*Visitor) VisitConstDeclarationStatement

func (v *Visitor) VisitConstDeclarationStatement(ctx *parser.ConstDeclarationStatementContext) interface{}

func (*Visitor) VisitContinueStatement

func (v *Visitor) VisitContinueStatement(ctx *parser.ContinueStatementContext) interface{}

func (*Visitor) VisitDeclarationExpression

func (v *Visitor) VisitDeclarationExpression(ctx *parser.DeclarationExpressionContext) interface{}

func (*Visitor) VisitDefStatement

func (v *Visitor) VisitDefStatement(ctx *parser.DefStatementContext) interface{}

func (*Visitor) VisitDefcalArgumentDefinition

func (v *Visitor) VisitDefcalArgumentDefinition(ctx *parser.DefcalArgumentDefinitionContext) interface{}

func (*Visitor) VisitDefcalArgumentDefinitionList

func (v *Visitor) VisitDefcalArgumentDefinitionList(ctx *parser.DefcalArgumentDefinitionListContext) interface{}

func (*Visitor) VisitDefcalOperand

func (v *Visitor) VisitDefcalOperand(ctx *parser.DefcalOperandContext) interface{}

func (*Visitor) VisitDefcalOperandList

func (v *Visitor) VisitDefcalOperandList(ctx *parser.DefcalOperandListContext) interface{}

func (*Visitor) VisitDefcalStatement

func (v *Visitor) VisitDefcalStatement(ctx *parser.DefcalStatementContext) interface{}

func (*Visitor) VisitDefcalTarget

func (v *Visitor) VisitDefcalTarget(ctx *parser.DefcalTargetContext) interface{}

func (*Visitor) VisitDelayStatement

func (v *Visitor) VisitDelayStatement(ctx *parser.DelayStatementContext) interface{}

func (*Visitor) VisitDesignator

func (v *Visitor) VisitDesignator(ctx *parser.DesignatorContext) interface{}

func (*Visitor) VisitDurationofExpression

func (v *Visitor) VisitDurationofExpression(ctx *parser.DurationofExpressionContext) interface{}

func (*Visitor) VisitEndStatement

func (v *Visitor) VisitEndStatement(ctx *parser.EndStatementContext) interface{}

func (*Visitor) VisitEqualityExpression

func (v *Visitor) VisitEqualityExpression(ctx *parser.EqualityExpressionContext) interface{}

func (*Visitor) VisitErrorNode

func (v *Visitor) VisitErrorNode(node antlr.ErrorNode) interface{}

func (*Visitor) VisitExpressionList

func (v *Visitor) VisitExpressionList(ctx *parser.ExpressionListContext) interface{}

func (*Visitor) VisitExpressionStatement

func (v *Visitor) VisitExpressionStatement(ctx *parser.ExpressionStatementContext) interface{}

func (*Visitor) VisitExternArgument

func (v *Visitor) VisitExternArgument(ctx *parser.ExternArgumentContext) interface{}

func (*Visitor) VisitExternArgumentList

func (v *Visitor) VisitExternArgumentList(ctx *parser.ExternArgumentListContext) interface{}

func (*Visitor) VisitExternStatement

func (v *Visitor) VisitExternStatement(ctx *parser.ExternStatementContext) interface{}

func (*Visitor) VisitForStatement

func (v *Visitor) VisitForStatement(ctx *parser.ForStatementContext) interface{}

func (*Visitor) VisitGateCallStatement

func (v *Visitor) VisitGateCallStatement(ctx *parser.GateCallStatementContext) interface{}

func (*Visitor) VisitGateModifier

func (v *Visitor) VisitGateModifier(ctx *parser.GateModifierContext) interface{}

func (*Visitor) VisitGateOperand

func (v *Visitor) VisitGateOperand(ctx *parser.GateOperandContext) interface{}

func (*Visitor) VisitGateOperandList

func (v *Visitor) VisitGateOperandList(ctx *parser.GateOperandListContext) interface{}

func (*Visitor) VisitGateStatement

func (v *Visitor) VisitGateStatement(ctx *parser.GateStatementContext) interface{}

func (*Visitor) VisitIdentifierList

func (v *Visitor) VisitIdentifierList(ctx *parser.IdentifierListContext) interface{}

func (*Visitor) VisitIfStatement

func (v *Visitor) VisitIfStatement(ctx *parser.IfStatementContext) interface{}

func (*Visitor) VisitIncludeStatement

func (v *Visitor) VisitIncludeStatement(ctx *parser.IncludeStatementContext) interface{}
Example
text := `
	include "../_testdata/stdgates.qasm";
	qubit q;
	h q;
	`

lexer := parser.Newqasm3Lexer(antlr.NewInputStream(text))
p := parser.Newqasm3Parser(antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel))

tree := p.Program()
fmt.Println(tree.ToStringTree(nil, p))

qsim := q.New()
env := visitor.NewEnviron()
v := visitor.New(qsim, env)

if err := v.Visit(tree); err != nil {
	fmt.Println(err)
}

fmt.Println(slices.Sorted(maps.Keys(env.Gate)))
for _, s := range qsim.State() {
	fmt.Println(s)
}
Output:

(program (statementOrScope (statement (includeStatement include "../_testdata/stdgates.qasm" ;))) (statementOrScope (statement (quantumDeclarationStatement (qubitType qubit) q ;))) (statementOrScope (statement (gateCallStatement h (gateOperandList (gateOperand (indexedIdentifier q))) ;))) <EOF>)
[cx h i x y z]
[0][  0]( 0.7071 0.0000i): 0.5000
[1][  1]( 0.7071 0.0000i): 0.5000

func (*Visitor) VisitIndexExpression

func (v *Visitor) VisitIndexExpression(ctx *parser.IndexExpressionContext) interface{}

func (*Visitor) VisitIndexOperator

func (v *Visitor) VisitIndexOperator(ctx *parser.IndexOperatorContext) interface{}

func (*Visitor) VisitIndexedIdentifier

func (v *Visitor) VisitIndexedIdentifier(ctx *parser.IndexedIdentifierContext) interface{}

func (*Visitor) VisitIoDeclarationStatement

func (v *Visitor) VisitIoDeclarationStatement(ctx *parser.IoDeclarationStatementContext) interface{}

func (*Visitor) VisitLiteralExpression

func (v *Visitor) VisitLiteralExpression(ctx *parser.LiteralExpressionContext) interface{}

func (*Visitor) VisitLogicalAndExpression

func (v *Visitor) VisitLogicalAndExpression(ctx *parser.LogicalAndExpressionContext) interface{}

func (*Visitor) VisitLogicalOrExpression

func (v *Visitor) VisitLogicalOrExpression(ctx *parser.LogicalOrExpressionContext) interface{}

func (*Visitor) VisitMeasureArrowAssignmentStatement

func (v *Visitor) VisitMeasureArrowAssignmentStatement(ctx *parser.MeasureArrowAssignmentStatementContext) interface{}

func (*Visitor) VisitMeasureExpression

func (v *Visitor) VisitMeasureExpression(ctx *parser.MeasureExpressionContext) interface{}

func (*Visitor) VisitMultiplicativeExpression

func (v *Visitor) VisitMultiplicativeExpression(ctx *parser.MultiplicativeExpressionContext) interface{}

func (*Visitor) VisitOldStyleDeclarationStatement

func (v *Visitor) VisitOldStyleDeclarationStatement(ctx *parser.OldStyleDeclarationStatementContext) interface{}

func (*Visitor) VisitParenthesisExpression

func (v *Visitor) VisitParenthesisExpression(ctx *parser.ParenthesisExpressionContext) interface{}

func (*Visitor) VisitPowerExpression

func (v *Visitor) VisitPowerExpression(ctx *parser.PowerExpressionContext) interface{}

func (*Visitor) VisitPragma

func (v *Visitor) VisitPragma(ctx *parser.PragmaContext) interface{}
Example
text := `
	pragma qiskit.simulator noise model "qpu1.noise";
	`

lexer := parser.Newqasm3Lexer(antlr.NewInputStream(text))
p := parser.Newqasm3Parser(antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel))

tree := p.Program()
fmt.Println(tree.ToStringTree(nil, p))

qsim := q.New()
env := visitor.NewEnviron()
v := visitor.New(qsim, env)

fmt.Println(v.Visit(tree))
Output:

(program (statementOrScope (statement (pragma pragma qiskit.simulator noise model "qpu1.noise";))) <EOF>)
qiskit.simulator noise model "qpu1.noise";

func (*Visitor) VisitProgram

func (v *Visitor) VisitProgram(ctx *parser.ProgramContext) interface{}

func (*Visitor) VisitQuantumDeclarationStatement

func (v *Visitor) VisitQuantumDeclarationStatement(ctx *parser.QuantumDeclarationStatementContext) interface{}

func (*Visitor) VisitQubitType

func (v *Visitor) VisitQubitType(ctx *parser.QubitTypeContext) interface{}

func (*Visitor) VisitRangeExpression

func (v *Visitor) VisitRangeExpression(ctx *parser.RangeExpressionContext) interface{}

func (*Visitor) VisitResetStatement

func (v *Visitor) VisitResetStatement(ctx *parser.ResetStatementContext) interface{}
Example
text := `
	qubit q;
	U(pi/2, 0, pi) q;
	reset q;
	`

lexer := parser.Newqasm3Lexer(antlr.NewInputStream(text))
p := parser.Newqasm3Parser(antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel))

tree := p.Program()
fmt.Println(tree.ToStringTree(nil, p))

qsim := q.New()
env := visitor.NewEnviron()
v := visitor.New(qsim, env)

if err := v.Visit(tree); err != nil {
	fmt.Println(err)
}

for _, s := range qsim.State() {
	fmt.Println(s)
}
Output:

(program (statementOrScope (statement (quantumDeclarationStatement (qubitType qubit) q ;))) (statementOrScope (statement (gateCallStatement U ( (expressionList (expression (expression pi) / (expression 2)) , (expression 0) , (expression pi)) ) (gateOperandList (gateOperand (indexedIdentifier q))) ;))) (statementOrScope (statement (resetStatement reset (gateOperand (indexedIdentifier q)) ;))) <EOF>)
[0][  0]( 1.0000 0.0000i): 1.0000

func (*Visitor) VisitReturnSignature

func (v *Visitor) VisitReturnSignature(ctx *parser.ReturnSignatureContext) interface{}

func (*Visitor) VisitReturnStatement

func (v *Visitor) VisitReturnStatement(ctx *parser.ReturnStatementContext) interface{}

func (*Visitor) VisitScalarType

func (v *Visitor) VisitScalarType(ctx *parser.ScalarTypeContext) interface{}

func (*Visitor) VisitScope

func (v *Visitor) VisitScope(ctx *parser.ScopeContext) interface{}

func (*Visitor) VisitSetExpression

func (v *Visitor) VisitSetExpression(ctx *parser.SetExpressionContext) interface{}

func (*Visitor) VisitStatement

func (v *Visitor) VisitStatement(ctx *parser.StatementContext) interface{}

func (*Visitor) VisitStatementOrScope

func (v *Visitor) VisitStatementOrScope(ctx *parser.StatementOrScopeContext) interface{}

func (*Visitor) VisitSwitchCaseItem

func (v *Visitor) VisitSwitchCaseItem(ctx *parser.SwitchCaseItemContext) interface{}

func (*Visitor) VisitSwitchStatement

func (v *Visitor) VisitSwitchStatement(ctx *parser.SwitchStatementContext) interface{}

func (*Visitor) VisitTerminal

func (v *Visitor) VisitTerminal(node antlr.TerminalNode) interface{}

func (*Visitor) VisitUnaryExpression

func (v *Visitor) VisitUnaryExpression(ctx *parser.UnaryExpressionContext) interface{}

func (*Visitor) VisitVersion

func (v *Visitor) VisitVersion(ctx *parser.VersionContext) interface{}
Example
text := "OPENQASM 3.0;"

lexer := parser.Newqasm3Lexer(antlr.NewInputStream(text))
p := parser.Newqasm3Parser(antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel))

tree := p.Program()
fmt.Println(tree.ToStringTree(nil, p))

qsim := q.New()
env := visitor.NewEnviron()
v := visitor.New(qsim, env)

if err := v.Visit(tree); err != nil {
	fmt.Println(err)
}

fmt.Println(env.Version)
Output:

(program (version OPENQASM 3.0 ;) <EOF>)
3.0

func (*Visitor) VisitWhileStatement

func (v *Visitor) VisitWhileStatement(ctx *parser.WhileStatementContext) interface{}

Jump to

Keyboard shortcuts

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