Skip to content

Commit 5edf0ea

Browse files
committed
Reduce append
1 parent 17bab97 commit 5edf0ea

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

math/matrix/matrix.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func New(v ...[]complex128) Matrix {
2222
cols = len(v[0])
2323
}
2424

25-
data := make([]complex128, 0, rows*cols)
25+
data := make([]complex128, rows*cols)
2626
for i := range rows {
27-
data = append(data, v[i]...)
27+
copy(data[i*cols:(i+1)*cols], v[i])
2828
}
2929

3030
return Matrix{
@@ -115,7 +115,7 @@ func (m Matrix) Transpose() Matrix {
115115
out := Zero(p, q)
116116
for i := range p {
117117
for j := range q {
118-
out.Set(i, j, m.At(j, i))
118+
out.Set(j, i, m.At(i, j))
119119
}
120120
}
121121

@@ -305,12 +305,14 @@ func (m Matrix) TensorProduct(n Matrix) Matrix {
305305
p, q := m.Dimension()
306306
a, b := n.Dimension()
307307

308-
data := make([]complex128, 0, p*a*q*b)
308+
var idx int
309+
data := make([]complex128, p*a*q*b)
309310
for i := range p {
310311
for k := range a {
311312
for j := range q {
312313
for l := range b {
313-
data = append(data, m.At(i, j)*n.At(k, l))
314+
data[idx] = m.At(i, j) * n.At(k, l)
315+
idx++
314316
}
315317
}
316318
}

math/vector/vector.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ func (v Vector) Mul(z complex128) Vector {
6666

6767
// TensorProduct returns the tensor product of v and w.
6868
func (v Vector) TensorProduct(w Vector) Vector {
69-
out := make(Vector, 0, len(v)*len(w))
70-
for i := range v {
71-
for j := range w {
72-
out = append(out, v[i]*w[j])
69+
p, q := len(v), len(w)
70+
71+
var idx int
72+
out := make(Vector, p*q)
73+
for i := range p {
74+
for j := range q {
75+
out[idx] = v[i] * w[j]
76+
idx++
7377
}
7478
}
7579

@@ -90,18 +94,21 @@ func (v Vector) InnerProduct(w Vector) complex128 {
9094

9195
// OuterProduct returns the outer product of v and w.
9296
func (v Vector) OuterProduct(w Vector) matrix.Matrix {
97+
p, q := len(v), len(w)
9398
dual := w.Dual()
9499

95-
data := make([]complex128, 0, len(v))
100+
var idx int
101+
data := make([]complex128, p*q)
96102
for i := range v {
97103
for j := range dual {
98-
data = append(data, v[i]*dual[j])
104+
data[idx] = v[i] * dual[j]
105+
idx++
99106
}
100107
}
101108

102109
return matrix.Matrix{
103-
Rows: len(v),
104-
Cols: len(w),
110+
Rows: p,
111+
Cols: q,
105112
Data: data,
106113
}
107114
}

quantum/density/matrix.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func (m *Matrix) At(i, j int) complex128 {
5555

5656
// Qubits returns the qubits of the density matrix.
5757
func (m *Matrix) Qubits() []Qubit {
58-
var qubits []Qubit
59-
for i := range m.NumQubits() {
60-
qubits = append(qubits, Qubit(i))
58+
n := m.NumQubits()
59+
60+
qubits := make([]Qubit, n)
61+
for i := range n {
62+
qubits[i] = Qubit(i)
6163
}
6264

6365
return qubits

quantum/gate/gate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,14 @@ func QFT(n int) matrix.Matrix {
299299
g := I(n)
300300

301301
for i := range n {
302-
h := make([]matrix.Matrix, 0)
303-
for j := 0; j < n; j++ {
302+
h := make([]matrix.Matrix, n)
303+
for j := range n {
304304
if i == j {
305-
h = append(h, H())
305+
h[j] = H()
306306
continue
307307
}
308308

309-
h = append(h, I())
309+
h[j] = I()
310310
}
311311

312312
g = g.Apply(matrix.TensorProduct(h...))

quantum/qubit/state.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type State struct {
2020

2121
// NewState returns a new State.
2222
func NewState(amp complex128, binary ...string) State {
23-
var intv []int64
24-
for _, bin := range binary {
25-
intv = append(intv, number.Must(strconv.ParseInt(bin, 2, 0)))
23+
intv := make([]int64, len(binary))
24+
for i, bin := range binary {
25+
intv[i] = number.Must(strconv.ParseInt(bin, 2, 0))
2626
}
2727

2828
return State{

0 commit comments

Comments
 (0)