-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector_test.go
193 lines (152 loc) · 3.95 KB
/
vector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package qrad
import (
"fmt"
"math"
"testing"
)
func TestVectorAdd(t *testing.T) {
a1 := []Complex{
complex(6, -4), complex(7, 3), complex(4.2, -8.1), complex(0, -3),
}
b1 := []Complex{
complex(16, 2.3), complex(0, -7), complex(6, 0), complex(0, -4),
}
s1 := []Complex{
complex(22, -1.7), complex(7, -4), complex(10.2, -8.1), complex(0, -7),
}
av1 := Vector{Elements: a1}
bv1 := Vector{Elements: b1}
sv1 := NewVector()
sv1.Add(av1, bv1)
for i := range s1 {
if !s1[i].Equals(sv1.At(i)) {
t.Log(s1[i], sv1.At(i))
t.Error("Failed to add matrix")
}
}
}
func TestVectorSub(t *testing.T) {
a1 := []Complex{
complex(6, -4), complex(7, 3), complex(4.2, -8.1), complex(0, -3),
}
s1 := []Complex{
complex(0, 0), complex(0, 0), complex(0, 0), complex(0, 0),
}
av1 := Vector{Elements: a1}
sv1 := NewVector()
sv1.Sub(av1, av1)
for i := range s1 {
if !s1[i].Equals(sv1.At(i)) {
t.Log(s1[i], sv1.At(i))
t.Error("Failed to add matrix")
}
}
}
func TestVectorMulScalar(t *testing.T) {
a1 := []Complex{
complex(6, 3), complex(0, 0), complex(5, 1), complex(4, 0),
}
b1 := Complex(complex(3, 2))
s1 := []Complex{
complex(12, 21), complex(0, 0), complex(13, 13), complex(12, 8),
}
av1 := Vector{Elements: a1}
sv1 := NewVector()
sv1.MulScalar(b1, av1)
for i := range s1 {
if !s1[i].Equals(sv1.At(i)) {
t.Log(s1[i], sv1.At(i))
t.Error("Failed to add matrix")
}
}
}
func TestVectorMulMatrix(t *testing.T) {
a1 := [][]Complex{
[]Complex{complex(4, 0), complex(-1, 0)},
[]Complex{complex(2, 0), complex(1, 0)},
}
b1 := []Complex{complex(1, 0), complex(2, 0)}
s1 := []Complex{
complex(2, 0), complex(4, 0),
}
am1 := NewMatrixFromElements(a1)
bv1 := NewVectorFromElements(b1)
sv1 := NewVector()
sv1.MulMatrix(*bv1, *am1)
for i := range s1 {
if !s1[i].Equals(sv1.At(i)) {
t.Log(s1[i], sv1.At(i))
t.Error("Failed to mul matrix")
}
}
}
func TestVectorTensorProduct(t *testing.T) {
a := NewVectorFromElements([]Complex{complex(2, 0), complex(3, 0)})
b := NewVectorFromElements([]Complex{complex(4, 0), complex(6, 0), complex(3, 0)})
s := NewVectorFromElements([]Complex{complex(8, 0), complex(12, 0),
complex(6, 0), complex(12, 0), complex(18, 0), complex(9, 0)})
g := NewVector()
g.TensorProduct(*a, *b)
for h := 0; h < g.Size(); h++ {
if !s.At(h).Equals(g.At(h)) {
t.Error("Failed to take tensor product")
}
}
}
func TestVectorMeasure(t *testing.T) {
// 1. Make sure bell state normalized correctly
bellstate := NewVectorFromElements([]Complex{
Complex(complex(1/math.Sqrt(2), 0)),
Complex(complex(0, 0)),
Complex(complex(0, 0)),
Complex(complex(1/math.Sqrt(2), 0)),
})
if !NearEqual(bellstate.Norm(), 1) {
fmt.Println(bellstate.Norm())
t.Error("failed to get norm")
}
// Ensure probablities sum to 1
bellstate.Probabilities()
probSums := float64(0)
for _, v := range bellstate.Probabilities() {
probSums += v
}
if !NearEqual(probSums, 1) {
t.Error("Failed to sum probabilities to 1")
}
// Ensure measurement is fair-ish
results := make(map[int]int)
for i := 0; i < 1000; i++ {
results[bellstate.Measure()]++
}
if results[1] != 0 || results[2] != 0 {
t.Error("bellstate shouldn't collapse to these states")
}
// really these should be equal...
if results[0] == 0 || results[3] == 0 {
t.Error("we should be getting at least a few results each")
}
}
func TestVectorBitMeasure(t *testing.T) {
results := make(map[int]int)
for i := 0; i < 1; i++ {
q := NewCircuit([]int{0, 0, 0})
q.Append(H, []int{0, 1, 2})
q.Execute()
// q.State.PrintProbabilities()
results[q.State.MeasureQubit(0)]++
// q.State.PrintProbabilities()
}
// fmt.Println(results)
}
func TestReverseEndianness(t *testing.T) {
if ReverseEndianness(5, 4) != 10 {
t.Error("failed to reverse endianness")
}
if ReverseEndianness(10, 4) != 5 {
t.Error("Failed to reverse endianness", ReverseEndianness(10, 4))
}
if ReverseEndianness(13, 4) != 11 {
t.Error("Failed to reverse endianness")
}
}