Skip to content

Commit

Permalink
Update some files
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Jan 16, 2025
1 parent b7f2339 commit 1ea7961
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ parse:
go run cmd/parse/main.go < _testdata/bell.qasm

test:
go test -v -cover $(shell go list ./... | grep -v /cmd | grep -v /gen) -v -coverprofile=coverage.txt -covermode=atomic
go test -v -cover $(shell go list ./... | grep -v /cmd | grep -v /gen | grep -v -E "qasm$$") -v -coverprofile=coverage.txt -covermode=atomic
11 changes: 0 additions & 11 deletions cmd/parse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"os"

"github.com/antlr4-go/antlr/v4"
"github.com/itsubaki/q"
"github.com/itsubaki/qasm/gen/parser"
"github.com/itsubaki/qasm/io"
"github.com/itsubaki/qasm/visitor"
)

func main() {
Expand All @@ -19,13 +17,4 @@ func main() {

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

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

switch ret := v.Visit(tree).(type) {
case error:
panic(ret)
}
}
42 changes: 42 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/antlr4-go/antlr/v4"
"github.com/itsubaki/q"
"github.com/itsubaki/qasm/gen/parser"
"github.com/itsubaki/qasm/visitor"
)

func main() {
var filepath string
flag.StringVar(&filepath, "f", "", "filepath")
flag.Parse()

if filepath == "" {
fmt.Printf("Usage: %s -f filepath\n", os.Args[0])
return
}

text, err := os.ReadFile(filepath)
if err != nil {
panic(err)
}

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

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

if err := v.Visit(p.Program()); err != nil {
panic(err)
}

for _, s := range qsim.State() {
fmt.Println(s)
}
}
4 changes: 3 additions & 1 deletion visitor/gate.go → visitor/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func Controlled(u matrix.Matrix, c []int) matrix.Matrix {

// NegControlled returns a controlled-u gate with control bit.
// u is a (2**n x 2**n) unitary matrix and returns a (2**n x 2**n) matrix.
func NegControlled(u matrix.Matrix, n int, c []int) matrix.Matrix {
func NegControlled(u matrix.Matrix, c []int) matrix.Matrix {
d, _ := u.Dimension()
n := number.Log2(d)
x := gate.TensorProduct(gate.X(), n, c)
cu := Controlled(u, c)
return matrix.Apply(x, cu, x)
Expand Down
12 changes: 4 additions & 8 deletions visitor/gate_test.go → visitor/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func TestControlled(t *testing.T) {

func TestNegControlled(t *testing.T) {
cases := []struct {
in matrix.Matrix
want matrix.Matrix
n, bit int
in matrix.Matrix
want matrix.Matrix
bit int
}{
{
in: gate.TensorProduct(gate.X(), 2, []int{1}),
Expand All @@ -90,7 +90,6 @@ func TestNegControlled(t *testing.T) {
gate.ControlledNot(2, []int{0}, 1),
gate.TensorProduct(gate.X(), 2, []int{0}),
),
n: 2,
bit: 0,
},
{
Expand All @@ -100,7 +99,6 @@ func TestNegControlled(t *testing.T) {
gate.ControlledNot(2, []int{1}, 0),
gate.TensorProduct(gate.X(), 2, []int{1}),
),
n: 2,
bit: 1,
},
{
Expand All @@ -110,7 +108,6 @@ func TestNegControlled(t *testing.T) {
gate.ControlledNot(3, []int{0, 1}, 2),
gate.TensorProduct(gate.X(), 3, []int{1}),
),
n: 3,
bit: 1,
},
{
Expand All @@ -120,13 +117,12 @@ func TestNegControlled(t *testing.T) {
gate.ControlledNot(3, []int{0, 2}, 1),
gate.TensorProduct(gate.X(), 3, []int{2}),
),
n: 3,
bit: 2,
},
}

for _, c := range cases {
got := visitor.NegControlled(c.in, c.n, []int{c.bit})
got := visitor.NegControlled(c.in, []int{c.bit})
if !got.Equals(c.want) {
t.Fail()
}
Expand Down
Loading

0 comments on commit 1ea7961

Please sign in to comment.