Skip to content

Commit 17bab97

Browse files
authored
Merge pull request #32 from itsubaki/update-matrix-type
Update Matrix type
2 parents 3c90e70 + 63462e0 commit 17bab97

File tree

11 files changed

+286
-289
lines changed

11 files changed

+286
-289
lines changed

math/matrix/matrix.go

Lines changed: 127 additions & 102 deletions
Large diffs are not rendered by default.

math/matrix/matrix_test.go

Lines changed: 37 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package matrix_test
22

33
import (
44
"fmt"
5-
"math/cmplx"
6-
"sync"
75
"testing"
86

97
"github.com/itsubaki/q/math/matrix"
@@ -22,48 +20,6 @@ func BenchmarkApplyN8(b *testing.B) {
2220
}
2321
}
2422

25-
func BenchmarkApplyConcurrencyN8(b *testing.B) {
26-
apply := func(n, m matrix.Matrix) matrix.Matrix {
27-
p, _ := m.Dimension()
28-
a, b := n.Dimension()
29-
30-
wg := sync.WaitGroup{}
31-
out := make(matrix.Matrix, a)
32-
for i := 0; i < a; i++ {
33-
wg.Add(1)
34-
go func(i int, out *matrix.Matrix) {
35-
defer wg.Done()
36-
37-
v := make([]complex128, b)
38-
for j := 0; j < b; j++ {
39-
var c complex128
40-
for k := 0; k < p; k++ {
41-
c = c + n[i][k]*m[k][j]
42-
}
43-
44-
v = append(v, c)
45-
}
46-
47-
(*out)[i] = v
48-
}(i, &out)
49-
}
50-
51-
wg.Wait()
52-
return out
53-
}
54-
55-
n := 8
56-
x := matrix.TensorProductN(matrix.New(
57-
[]complex128{0, 1},
58-
[]complex128{1, 0},
59-
), n)
60-
61-
b.ResetTimer()
62-
for i := 0; i < b.N; i++ {
63-
apply(x, x)
64-
}
65-
}
66-
6723
func BenchmarkDaggerN8(b *testing.B) {
6824
n := 8
6925
m := matrix.TensorProductN(matrix.New(
@@ -77,42 +33,6 @@ func BenchmarkDaggerN8(b *testing.B) {
7733
}
7834
}
7935

80-
func BenchmarkDaggerConcurrencyN8(b *testing.B) {
81-
n := 8
82-
m := matrix.TensorProductN(matrix.New(
83-
[]complex128{0, 1},
84-
[]complex128{1, 0},
85-
), n)
86-
87-
dagger := func(m matrix.Matrix) {
88-
p, q := m.Dimension()
89-
90-
wg := sync.WaitGroup{}
91-
out := make(matrix.Matrix, p)
92-
for i := 0; i < p; i++ {
93-
wg.Add(1)
94-
95-
go func(i int, out *matrix.Matrix) {
96-
defer wg.Done()
97-
98-
v := make([]complex128, q)
99-
for j := 0; j < q; j++ {
100-
v[j] = cmplx.Conj(m[j][i])
101-
}
102-
103-
(*out)[i] = v
104-
}(i, &out)
105-
}
106-
107-
wg.Wait()
108-
}
109-
110-
b.ResetTimer()
111-
for i := 0; i < b.N; i++ {
112-
dagger(m)
113-
}
114-
}
115-
11636
func BenchmarkTensorProductN8(b *testing.B) {
11737
n := 8
11838
m := matrix.New(
@@ -126,14 +46,34 @@ func BenchmarkTensorProductN8(b *testing.B) {
12646
}
12747

12848
func ExampleZero() {
129-
fmt.Println(matrix.Zero(0, 0))
130-
fmt.Println(matrix.Zero(1, 1))
131-
fmt.Println(matrix.Zero(2, 2))
49+
fmt.Println(matrix.Zero(0, 0).Data)
50+
fmt.Println(matrix.Zero(1, 1).Data)
51+
fmt.Println(matrix.Zero(2, 2).Data)
13252

13353
// Output:
13454
// []
135-
// [[(0+0i)]]
136-
// [[(0+0i) (0+0i)] [(0+0i) (0+0i)]]
55+
// [(0+0i)]
56+
// [(0+0i) (0+0i) (0+0i) (0+0i)]
57+
}
58+
59+
func ExampleMatrix_Seq2() {
60+
m := matrix.New(
61+
[]complex128{1, 1},
62+
[]complex128{2, 2},
63+
[]complex128{3, 3},
64+
)
65+
66+
for i, r := range m.Seq2() {
67+
fmt.Println(r)
68+
69+
if i == 1 {
70+
break
71+
}
72+
}
73+
74+
// Output:
75+
// [(1+0i) (1+0i)]
76+
// [(2+0i) (2+0i)]
13777
}
13878

13979
func ExampleMatrix_Real() {
@@ -172,7 +112,7 @@ func ExampleMatrix_Mul() {
172112
[]complex128{1 + 1i, 0},
173113
)
174114

175-
for _, r := range m.Mul(3i) {
115+
for _, r := range m.Mul(3i).Seq2() {
176116
fmt.Println(r)
177117
}
178118

@@ -188,12 +128,12 @@ func ExampleMatrix_Apply() {
188128
)
189129

190130
fmt.Println("x:")
191-
for _, r := range x {
131+
for _, r := range x.Seq2() {
192132
fmt.Println(r)
193133
}
194134

195135
fmt.Println("xx:")
196-
for _, r := range x.Apply(x) {
136+
for _, r := range x.Apply(x).Seq2() {
197137
fmt.Println(r)
198138
}
199139

@@ -212,7 +152,7 @@ func ExampleTensorProduct() {
212152
[]complex128{1, 0},
213153
)
214154

215-
for _, r := range matrix.TensorProduct(x, x) {
155+
for _, r := range matrix.TensorProduct(x, x).Seq2() {
216156
fmt.Println(r)
217157
}
218158

@@ -229,7 +169,7 @@ func ExampleTensorProductN() {
229169
[]complex128{1, 0},
230170
)
231171

232-
for _, r := range matrix.TensorProductN(x, 2) {
172+
for _, r := range matrix.TensorProductN(x, 2).Seq2() {
233173
fmt.Println(r)
234174
}
235175

@@ -246,7 +186,7 @@ func ExampleApply() {
246186
[]complex128{1, 0},
247187
)
248188

249-
for _, r := range matrix.Apply(x, x) {
189+
for _, r := range matrix.Apply(x, x).Seq2() {
250190
fmt.Println(r)
251191
}
252192

@@ -267,7 +207,7 @@ func ExampleApply_xy() {
267207
)
268208

269209
// x.Apply(y) is yx
270-
for _, r := range x.Apply(y) {
210+
for _, r := range x.Apply(y).Seq2() {
271211
fmt.Println(r)
272212
}
273213

@@ -282,12 +222,12 @@ func ExampleApplyN() {
282222
[]complex128{1, 0},
283223
)
284224

285-
for _, r := range matrix.ApplyN(x) {
225+
for _, r := range matrix.ApplyN(x).Seq2() {
286226
fmt.Println(r)
287227
}
288228
fmt.Println()
289229

290-
for _, r := range matrix.ApplyN(x, 2) {
230+
for _, r := range matrix.ApplyN(x, 2).Seq2() {
291231
fmt.Println(r)
292232
}
293233

@@ -306,12 +246,12 @@ func ExampleMatrix_TensorProduct() {
306246
)
307247

308248
fmt.Println("x:")
309-
for _, r := range x {
249+
for _, r := range x.Seq2() {
310250
fmt.Println(r)
311251
}
312252

313253
fmt.Println("xx:")
314-
for _, r := range x.TensorProduct(x) {
254+
for _, r := range x.TensorProduct(x).Seq2() {
315255
fmt.Println(r)
316256
}
317257

@@ -405,10 +345,7 @@ func TestAntiCommutator(t *testing.T) {
405345
[]complex128{0, -1i},
406346
[]complex128{1i, 0},
407347
),
408-
matrix.New(
409-
[]complex128{0, 0},
410-
[]complex128{0, 0},
411-
),
348+
matrix.Zero(2, 2),
412349
},
413350
}
414351

math/vector/vector.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ func (v Vector) InnerProduct(w Vector) complex128 {
9292
func (v Vector) OuterProduct(w Vector) matrix.Matrix {
9393
dual := w.Dual()
9494

95-
out := make(matrix.Matrix, len(v))
95+
data := make([]complex128, 0, len(v))
9696
for i := range v {
97-
out[i] = make([]complex128, len(dual))
9897
for j := range dual {
99-
out[i][j] = v[i] * dual[j]
98+
data = append(data, v[i]*dual[j])
10099
}
101100
}
102101

103-
return out
102+
return matrix.Matrix{
103+
Rows: len(v),
104+
Cols: len(w),
105+
Data: data,
106+
}
104107
}
105108

106109
// IsOrthogonal returns true if v and w are orthogonal.
@@ -123,10 +126,10 @@ func (v Vector) Apply(m matrix.Matrix) Vector {
123126
p, q := m.Dimension()
124127

125128
out := make(Vector, p)
126-
for i := 0; i < p; i++ {
129+
for i := range p {
127130
var c complex128
128-
for j := 0; j < q; j++ {
129-
c = c + m[i][j]*v[j]
131+
for j := range q {
132+
c = c + m.At(i, j)*v[j]
130133
}
131134

132135
out[i] = c

math/vector/vector_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func BenchmarkApplyConcurrencyN12(b *testing.B) {
3838

3939
var c complex128
4040
for j := 0; j < q; j++ {
41-
c = c + m[i][j]*v[j]
41+
c = c + m.At(i, j)*v[j]
4242
}
4343

4444
(*out)[i] = c
@@ -126,10 +126,14 @@ func ExampleVector_InnerProduct() {
126126
func ExampleVector_OuterProduct() {
127127
v := vector.New(1, 0)
128128
vv := v.OuterProduct(v)
129-
fmt.Println(vv)
129+
130+
for _, r := range vv.Seq2() {
131+
fmt.Println(r)
132+
}
130133

131134
// Output:
132-
// [[(1+0i) (0+0i)] [(0+0i) (0+0i)]]
135+
// [(1+0i) (0+0i)]
136+
// [(0+0i) (0+0i)]
133137
}
134138

135139
func ExampleVector_TensorProduct() {

quantum/density/flip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ var (
1717
// Flip returns the flip channel.
1818
func Flip(p float64, m matrix.Matrix) (matrix.Matrix, matrix.Matrix, error) {
1919
if p < 0 || p > 1 {
20-
return nil, nil, ErrInvalidRange
20+
return matrix.Matrix{}, matrix.Matrix{}, ErrInvalidRange
2121
}
2222

2323
d, d2 := m.Dimension()
2424
if d != d2 {
25-
return nil, nil, ErrNotSquare
25+
return matrix.Matrix{}, matrix.Matrix{}, ErrNotSquare
2626
}
2727

2828
if !number.IsPowOf2(d) {
29-
return nil, nil, ErrInvalidDimension
29+
return matrix.Matrix{}, matrix.Matrix{}, ErrInvalidDimension
3030
}
3131

3232
n := number.Log2(d)

quantum/density/flip_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ func ExampleFlip() {
5050
func ExampleBitFlip() {
5151
m0, m1, _ := density.BitFlip(0.5)
5252

53-
for _, r := range m0 {
53+
for _, r := range m0.Seq2() {
5454
fmt.Println(r)
5555
}
5656
fmt.Println()
5757

58-
for _, r := range m1 {
58+
for _, r := range m1.Seq2() {
5959
fmt.Println(r)
6060
}
6161

@@ -70,12 +70,12 @@ func ExampleBitFlip() {
7070
func ExamplePhaseFlip() {
7171
m0, m1, _ := density.PhaseFlip(0.5)
7272

73-
for _, r := range m0 {
73+
for _, r := range m0.Seq2() {
7474
fmt.Println(r)
7575
}
7676
fmt.Println()
7777

78-
for _, r := range m1 {
78+
for _, r := range m1.Seq2() {
7979
fmt.Println(r)
8080
}
8181

@@ -90,12 +90,12 @@ func ExamplePhaseFlip() {
9090
func ExampleBitPhaseFlip() {
9191
m0, m1, _ := density.BitPhaseFlip(0.5)
9292

93-
for _, r := range m0 {
93+
for _, r := range m0.Seq2() {
9494
fmt.Println(r)
9595
}
9696
fmt.Println()
9797

98-
for _, r := range m1 {
98+
for _, r := range m1.Seq2() {
9999
fmt.Println(r)
100100
}
101101

0 commit comments

Comments
 (0)