Skip to content

Commit

Permalink
Add Log2 to math/number package
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Apr 19, 2024
1 parent c3d912d commit 533836e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 27 deletions.
25 changes: 25 additions & 0 deletions math/number/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package number

import "fmt"

func Log2(N int) (int, error) {
if N == 1 {
return 0, nil
}

if N%2 != 0 {
return 0, fmt.Errorf("N must be a power of 2. N=%v", N)
}

var n int = 1
for {
if N/2 == 1 {
break
}

N = N / 2
n++
}

return n, nil
}
36 changes: 36 additions & 0 deletions math/number/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package number_test

import (
"testing"

"github.com/itsubaki/q/math/number"
)

func TestLog2(t *testing.T) {
cases := []struct {
n int
want int
}{
{1, 0},
{2, 1},
{4, 2},
{8, 3},
{16, 4},
{32, 5},
{64, 6},
{128, 7},
{256, 8},
{512, 9},
{1024, 10},
{2048, 11},
{4096, 12},
{8192, 13},
}

for _, c := range cases {
got := number.Must(number.Log2(c.n))
if got != c.want {
t.Errorf("got=%v want=%d", got, c.want)
}
}
}
26 changes: 26 additions & 0 deletions math/number/must_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package number_test

import (
"fmt"
"testing"

"github.com/itsubaki/q/math/number"
)

func TestMustPanic(t *testing.T) {
defer func() {
if rec := recover(); rec != nil {
err, ok := rec.(error)
if !ok {
t.Fail()
}

if err.Error() != "something went wrong" {
t.Fail()
}
}
}()

number.Must(-1, fmt.Errorf("something went wrong"))
t.Fail()
}
18 changes: 0 additions & 18 deletions math/number/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,6 @@ func TestParseFloat(t *testing.T) {
}
}

func TestMustPanic(t *testing.T) {
defer func() {
if rec := recover(); rec != nil {
err, ok := rec.(error)
if !ok {
t.Fail()
}

if err.Error() != "something went wrong" {
t.Fail()
}
}
}()

number.Must(-1, fmt.Errorf("something went wrong"))
t.Fail()
}

func FuzzParseFloat(f *testing.F) {
seed := []string{"123", "101", "1.0101", "abc", "a.bc"}
for i := range seed {
Expand Down
7 changes: 4 additions & 3 deletions q.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (q *Q) One() Qubit {
return q.New(0, 1)
}

// ZeroWith returns a qubit in the zero state with n qubits.
// ZeroWith returns n qubits in the zero state.
func (q *Q) ZeroWith(n int) []Qubit {
qb := make([]Qubit, n)
for i := 0; i < n; i++ {
Expand All @@ -93,7 +93,7 @@ func (q *Q) ZeroWith(n int) []Qubit {
return qb
}

// One returns a qubit in the one state with n qubits.
// One returns n qubit in the one state.
func (q *Q) OneWith(n int) []Qubit {
qb := make([]Qubit, n)
for i := 0; i < n; i++ {
Expand All @@ -103,7 +103,8 @@ func (q *Q) OneWith(n int) []Qubit {
return qb
}

// ZeroLog2 returns a qubit in the zero state with log2(N) qubits.
// ZeroLog2 returns n qubit in the zero state.
// n is greater than or equal to log2(N).
func (q *Q) ZeroLog2(N int) []Qubit {
n := int(math.Log2(float64(N))) + 1
return q.ZeroWith(n)
Expand Down
5 changes: 2 additions & 3 deletions quantum/density/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func (m *Matrix) Dimension() (int, int) {
// NumberOfBit returns the number of qubits.
func (m *Matrix) NumberOfBit() int {
p, _ := m.Dimension()
log := math.Log2(float64(p))
return int(log)
return number.Must(number.Log2(p))
}

// Apply applies a unitary matrix to the density matrix.
Expand Down Expand Up @@ -166,7 +165,7 @@ func Flip(p float64, m matrix.Matrix) (matrix.Matrix, matrix.Matrix, error) {
}

d, _ := m.Dimension()
n := int(math.Log2(float64(d)))
n := number.Must(number.Log2(d))

e0 := gate.I(n).Mul(complex(math.Sqrt(p), 0))
e1 := m.Mul(complex(math.Sqrt(1-p), 0))
Expand Down
4 changes: 1 addition & 3 deletions quantum/qubit/qubit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func One(n ...int) *Qubit {

// NumberOfBit returns the number of qubits.
func (q *Qubit) NumberOfBit() int {
d := float64(q.Dimension())
n := math.Log2(d)
return int(n)
return number.Must(number.Log2(q.Dimension()))
}

// IsZero returns true if q is zero qubit.
Expand Down

0 comments on commit 533836e

Please sign in to comment.