Skip to content

Commit

Permalink
Update some files
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed May 25, 2022
1 parent 431b081 commit 567142e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 76 deletions.
8 changes: 0 additions & 8 deletions pkg/ast/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ type DeclList struct {
List []Decl
}

func (d *DeclList) Append(e Decl) {
d.List = append(d.List, e)
}

func (d *DeclList) String() string {
var list []string
for _, e := range d.List {
Expand All @@ -42,10 +38,6 @@ type ParenDecl struct {

func (d *ParenDecl) declNode() {}

func (d *ParenDecl) Append(e Decl) {
d.List.Append(e)
}

func (d *ParenDecl) Literal() string {
return lexer.Tokens[lexer.LPAREN]
}
Expand Down
10 changes: 0 additions & 10 deletions pkg/ast/decl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,6 @@ func TestDeclLiteral(t *testing.T) {
}
}

func TestDeclList(t *testing.T) {
l := &ast.DeclList{}
l.Append(&ast.BadDecl{})
l.Append(&ast.BadDecl{})

if len(l.List) != 2 {
t.Errorf("invalid length=%v", len(l.List))
}
}

func TestBadDecl(t *testing.T) {
d := &ast.BadDecl{}

Expand Down
4 changes: 0 additions & 4 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func (x *ExprList) Literal() string {
return ""
}

func (x *ExprList) Append(e Expr) {
x.List = append(x.List, e)
}

func (x *ExprList) String() string {
var list []string
for _, e := range x.List {
Expand Down
10 changes: 0 additions & 10 deletions pkg/ast/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,6 @@ func TestExprLiteral(t *testing.T) {
}
}

func TestExprList(t *testing.T) {
l := &ast.ExprList{}
l.Append(&ast.BadExpr{})
l.Append(&ast.BadExpr{})

if len(l.List) != 2 {
t.Errorf("invalid length=%v", len(l.List))
}
}

func TestBadExpr(t *testing.T) {
x := &ast.BadExpr{}

Expand Down
46 changes: 38 additions & 8 deletions pkg/ast/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,13 @@ func (s *BlockStmt) String() string {
return buf.String()
}

func (s *BlockStmt) Append(e Stmt) {
s.List = append(s.List, e)
}

func (s *BlockStmt) Reverse() *BlockStmt {
var out BlockStmt
func (s *BlockStmt) Inv() *BlockStmt {
var rev BlockStmt
for i := len(s.List) - 1; i > -1; i-- {
out.Append(s.List[i])
rev.List = append(rev.List, s.List[i])
}

return &out
return rev.Add(Modifier{Kind: lexer.INV})
}

func (s *BlockStmt) Pow(y int) *BlockStmt {
Expand All @@ -273,6 +269,40 @@ func (s *BlockStmt) Pow(y int) *BlockStmt {
return &out
}

func (s *BlockStmt) Add(m Modifier) *BlockStmt {
var out BlockStmt
for _, b := range s.List {
switch s := b.(type) {
case *ApplyStmt:
out.List = append(out.List, &ApplyStmt{
Kind: s.Kind,
Modifier: append(s.Modifier, m),
Name: s.Name,
Params: s.Params,
QArgs: s.QArgs,
})
case *ExprStmt:
switch X := s.X.(type) {
case *CallExpr:
out.List = append(out.List, &ExprStmt{
X: &CallExpr{
Name: X.Name,
Modifier: append(X.Modifier, m),
Params: X.Params,
QArgs: X.QArgs,
},
})
default:
out.List = append(out.List, &ExprStmt{X: X})
}
default:
out.List = append(out.List, s)
}
}

return &out
}

type IfStmt struct{}

type BranchStmt struct{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ast/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func TestStmt(t *testing.T) {
}
}

func ExampleBlockStmt_Reverse() {
func ExampleBlockStmt_Inv() {
block := &ast.BlockStmt{
List: []ast.Stmt{
&ast.ApplyStmt{
Expand Down Expand Up @@ -494,14 +494,14 @@ func ExampleBlockStmt_Reverse() {
},
}

rev := block.Reverse()
rev := block.Inv()

fmt.Println(block)
fmt.Println(rev)

// Output:
// { H q0; cx q0, q1; }
// { cx q0, q1; H q0; }
// { inv @ cx q0, q1; inv @ H q0; }
}

func TestBadStmt(t *testing.T) {
Expand Down
42 changes: 17 additions & 25 deletions pkg/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ func (e *Evaluator) eval(n ast.Node, env *env.Environ) (obj object.Object, err e
c[i] = e[i].(*object.Int).Value
}

case *ast.BlockStmt:
return e.EvalBolck(e.ModifyStmt(n, env), env)

case *ast.ReturnStmt:
v, err := e.eval(n.Result, env)
if err != nil {
Expand Down Expand Up @@ -185,6 +182,9 @@ func (e *Evaluator) eval(n ast.Node, env *env.Environ) (obj object.Object, err e
case *ast.CallExpr:
return e.Call(n, env)

case *ast.BlockStmt:
return e.EvalBolck(e.ModifyStmt(n, env), env)

case *ast.MeasureExpr:
return e.Measure(n, env)

Expand Down Expand Up @@ -487,10 +487,10 @@ func (e *Evaluator) ApplyUAt(i int, mod []ast.Modifier, u matrix.Matrix, qargs [
}

func (e *Evaluator) ApplyU(mod []ast.Modifier, u matrix.Matrix, qargs [][]q.Qubit, env *env.Environ) error {
// Modifier
// Modify inv, pow
u = e.ModifyU(mod, u, env)

// ctrl
// Modify ctrl
ctrl := ast.ModCtrl(mod)
if len(ctrl) > 0 {
// NOTE: That is, inv @ ctrl @ U = ctrl @ inv @ U.
Expand Down Expand Up @@ -627,12 +627,17 @@ func (e *Evaluator) SetQArgs(env, outer *env.Environ, decl, args []ast.Expr) {
}
}

// fmt.Printf("decl: %v\n", decl)
// fmt.Printf("args: %v\n", args)
// fmt.Printf("outer.Modifier: %v\n", outer.Modifier)
// fmt.Printf("outer.Qubit: %v\n", outer.Qubit)
// fmt.Printf("env.Modifier: %v\n", env.Modifier)
// fmt.Printf("env.Decl: %v\n", env.Decl)
// fmt.Printf("out.Qubit: %v\n", outer.Qubit)
// fmt.Printf("env.Qubit: %v\n", env.Qubit)

// args: [c t]
// env.Modifier: [{51 {{[]}}}]
// env.Decl: [gate x q { U(pi, 0, pi) q; }]
// out.Qubit: [c t], map[c:[0] t:[1]]
// env.Qubit: [q], map[q:[0]]
}

// ModifyU returns modified(inv, pow) U
Expand All @@ -650,7 +655,7 @@ func (e *Evaluator) ModifyU(mod []ast.Modifier, u matrix.Matrix, env *env.Enviro
return u
}

// ModifyStmt returns modified(inv, pow) BlockStmt
// ModifyStmt returns modified BlockStmt
func (e *Evaluator) ModifyStmt(s *ast.BlockStmt, env *env.Environ) *ast.BlockStmt {
// NOTE: pow(2) @ inv @ u is not equal to inv @ pow(2) @ u
//
Expand Down Expand Up @@ -682,25 +687,12 @@ func (e *Evaluator) ModifyStmt(s *ast.BlockStmt, env *env.Environ) *ast.BlockStm
// inv @ pow(3) @ U(c, b, a) q;
for _, m := range env.Modifier {
switch m.Kind {
case lexer.CTRL, lexer.NEGCTRL:
s = s.Add(m)
case lexer.INV:
s = s.Reverse()

for _, b := range s.List {
switch s := b.(type) {
case *ast.ApplyStmt:
s.Modifier = append(s.Modifier, m)
case *ast.ExprStmt:
switch X := s.X.(type) {
case *ast.CallExpr:
X.Modifier = append(X.Modifier, m)
}
}
}

s = s.Inv()
case lexer.POW:
s = s.Pow(int(ast.Must(e.eval(m.Index.List.List[0], env)).(*object.Int).Value))
case lexer.CTRL, lexer.NEGCTRL:
// FIXME: copy s. add mod ctrl, negctrl
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ qubit c;
qubit t;
x c;
ctrl @ x c, t;
print;
ctrl @ x c, t;
print;
`
Expand Down
14 changes: 6 additions & 8 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ func (p *Parser) parseDeclStmt() ast.Stmt {

func (p *Parser) parseDeclList() ast.DeclList {
list := ast.DeclList{}
list.Append(p.parseDecl())
list.List = append(list.List, p.parseDecl())

for p.next().Token == lexer.COMMA {
p.next() // skip COMMA token

list.Append(p.parseDecl())
list.List = append(list.List, p.parseDecl())
}

return list
Expand Down Expand Up @@ -199,12 +198,11 @@ func (p *Parser) parseDecl() ast.Decl {

func (p *Parser) parseIdentList() ast.ExprList {
list := ast.ExprList{}
list.Append(p.parseIdent())
list.List = append(list.List, p.parseIdent())

for p.next().Token == lexer.COMMA {
p.next() // skip COMMNA token

list.Append(p.parseIdent())
list.List = append(list.List, p.parseIdent())
}

return list
Expand Down Expand Up @@ -362,7 +360,7 @@ func (p *Parser) parseGate() ast.Decl {
p.expect(lexer.LBRACE)

for p.next().Token != lexer.RBRACE {
decl.Body.Append(p.parseStmt())
decl.Body.List = append(decl.Body.List, p.parseStmt())
}
p.expect(lexer.RBRACE)

Expand Down Expand Up @@ -420,7 +418,7 @@ func (p *Parser) parseFunc() ast.Decl {
p.expect(lexer.LBRACE)

for p.next().Token != lexer.RBRACE {
decl.Body.Append(p.parseStmt())
decl.Body.List = append(decl.Body.List, p.parseStmt())
}
p.expect(lexer.RBRACE)

Expand Down

0 comments on commit 567142e

Please sign in to comment.