Skip to content

Commit 251d196

Browse files
committed
Add MustParseInt
1 parent 1a8f020 commit 251d196

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

cmd/counting/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ func main() {
7777
qsim.InvQFT(c...)
7878

7979
// results
80-
N, size := 1<<len(r), 1<<t
81-
for _, s := range q.Top(qsim.State(c, r, s, a), top) {
82-
phi := float64(s.Int()[0]) / float64(size) // phi = k / 2**t
80+
N := 1 << len(r)
81+
for _, state := range q.Top(qsim.State(c, r, s, a), top) {
82+
phi := ldexp(state.Int()[0], -t) // phi = k / 2**t
8383
theta := 2 * math.Pi * phi // theta = 2*pi*phi
8484
M := float64(N) * math.Pow(math.Sin(theta/2), 2) // M = N*(sin(theta/2))**2
8585

86-
fmt.Printf("%v; phi=%.4f, theta=%.4f, M=%.4f, M'=%.4f\n", s, phi, theta, M, float64(N)-M)
86+
fmt.Printf("%v; phi=%.4f, theta=%.4f, M=%.4f, M'=%.4f\n", state, phi, theta, M, float64(N)-M)
8787
}
8888
}
89+
90+
func ldexp(a, b int) float64 {
91+
return math.Ldexp(float64(a), b)
92+
}

cmd/grover/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func main() {
103103
}
104104

105105
// quantum states
106-
for _, s := range q.Top(qsim.State(r, s, a), top) {
107-
fmt.Println(s)
106+
for _, state := range q.Top(qsim.State(r, s, a), top) {
107+
fmt.Println(state)
108108
}
109109

110110
// measure

cmd/shor/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ func main() {
8585
print("measure reg1", qsim, r0, r1)
8686

8787
var prop float64
88-
for _, s := range qsim.State(r0) {
89-
i, m := s.Int()[0], s.BinaryString()[0]
90-
ss, r, d, ok := number.FindOrder(a, N, fmt.Sprintf("0.%s", m))
88+
for _, state := range qsim.State(r0) {
89+
i, m := state.Int()[0], state.BinaryString()[0]
90+
s, r, d, ok := number.FindOrder(a, N, fmt.Sprintf("0.%s", m))
9191
if !ok || number.IsOdd(r) {
92-
fmt.Printf(" i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f);\n", i, N, a, t, ss, r, m, d)
92+
fmt.Printf(" i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f);\n", i, N, a, t, s, r, m, d)
9393
continue
9494
}
9595

9696
p0 := number.GCD(number.Pow(a, r/2)-1, N)
9797
p1 := number.GCD(number.Pow(a, r/2)+1, N)
9898
if number.IsTrivial(N, p0, p1) {
99-
fmt.Printf(" i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f); p=%v, q=%v.\n", i, N, a, t, ss, r, m, d, p0, p1)
99+
fmt.Printf(" i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f); p=%v, q=%v.\n", i, N, a, t, s, r, m, d, p0, p1)
100100
continue
101101
}
102102

103-
fmt.Printf("* i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f); p=%v, q=%v.\n", i, N, a, t, ss, r, m, d, p0, p1)
104-
prop += s.Probability()
103+
fmt.Printf("* i=%4d: N=%d, a=%d, t=%d; s/r=%4d/%4d ([0.%v]~%.4f); p=%v, q=%v.\n", i, N, a, t, s, r, m, d, p0, p1)
104+
prop += state.Probability()
105105
}
106106

107107
fmt.Printf("total probability: %.8f\n", prop)
@@ -111,9 +111,9 @@ func print(desc string, qsim *q.Q, reg ...any) {
111111
fmt.Println(desc)
112112

113113
max := slices.Max(qsim.Probability())
114-
for _, s := range qsim.State(reg...) {
115-
p := strings.Repeat("*", int(s.Probability()/max*32))
116-
fmt.Printf("%s: %s\n", s, p)
114+
for _, state := range qsim.State(reg...) {
115+
p := strings.Repeat("*", int(state.Probability()/max*32))
116+
fmt.Printf("%s: %s\n", state, p)
117117
}
118118

119119
fmt.Println()

math/number/parse.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ func ParseFloat(binary string) (float64, error) {
4444

4545
return float64(p) + frac, nil
4646
}
47+
48+
// MustParseInt returns int from binary string.
49+
// It panics if error occurs.
50+
func MustParseInt(binary string) int {
51+
return int(Must(strconv.ParseInt(binary, 2, 0)))
52+
}

quantum/density/matrix.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"iter"
66
"math"
7-
"strconv"
87
"strings"
98

109
"github.com/itsubaki/q/math/epsilon"
@@ -202,8 +201,8 @@ func (m *Matrix) PartialTrace(idx ...Qubit) *Matrix {
202201
continue
203202
}
204203

205-
r := int(number.Must(strconv.ParseInt(kr, 2, 0)))
206-
c := int(number.Must(strconv.ParseInt(lr, 2, 0)))
204+
r := number.MustParseInt(kr)
205+
c := number.MustParseInt(lr)
207206
rho.AddAt(r, c, m.At(i, j))
208207

209208
// fmt.Printf("[%v][%v] = [%v][%v] + [%v][%v]\n", r, c, r, c, i, j)

quantum/qubit/qubit.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package qubit
33
import (
44
"math"
55
"math/cmplx"
6-
"strconv"
76
"strings"
87

98
"github.com/itsubaki/q/math/epsilon"
@@ -683,8 +682,8 @@ func (q *Qubit) BinaryString() string {
683682
}
684683

685684
// Int measures the quantum state and returns its int representation.
686-
func (q *Qubit) Int() int64 {
687-
return number.Must(strconv.ParseInt(q.BinaryString(), 2, 0))
685+
func (q *Qubit) Int() int {
686+
return number.MustParseInt(q.BinaryString())
688687
}
689688

690689
// String returns the string representation of q.

quantum/qubit/qubit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func TestClone(t *testing.T) {
990990
func TestInt(t *testing.T) {
991991
cases := []struct {
992992
in *qubit.Qubit
993-
want int64
993+
want int
994994
}{
995995
{qubit.Zero(), 0},
996996
{qubit.One(), 1},

quantum/qubit/state.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"math"
66
"math/cmplx"
7-
"strconv"
87

98
"github.com/itsubaki/q/math/epsilon"
109
"github.com/itsubaki/q/math/number"
@@ -15,14 +14,14 @@ type State struct {
1514
amp complex128
1615
prob float64
1716
binaryString []string
18-
intValue []int64
17+
intValue []int
1918
}
2019

2120
// NewState returns a new State.
2221
func NewState(amp complex128, binary ...string) State {
23-
intv := make([]int64, len(binary))
22+
intv := make([]int, len(binary))
2423
for i, b := range binary {
25-
intv[i] = number.Must(strconv.ParseInt(b, 2, 0))
24+
intv[i] = number.MustParseInt(b)
2625
}
2726

2827
return State{
@@ -45,7 +44,7 @@ func (s State) BinaryString() []string {
4544
return s.binaryString
4645
}
4746

48-
func (s State) Int() []int64 {
47+
func (s State) Int() []int {
4948
return s.intValue
5049
}
5150

0 commit comments

Comments
 (0)