-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgates.go
166 lines (143 loc) · 3.73 KB
/
gates.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
package qrad
import (
"math"
"math/cmplx"
)
type Gate struct {
Matrix Matrix
Name string
// Draw
IsBoxed bool
Symbol string
}
func (g Gate) Operands() int {
return int(math.Log2(float64(g.Matrix.Height)))
}
func (g Gate) IsControl() bool {
return g.Symbol == "C" && g.Matrix.Height == 0
}
var NotGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(0, 0)), Complex(complex(1, 0))},
{Complex(complex(1, 0)), Complex(complex(0, 0))},
}),
Name: "Pauli X",
Symbol: "X",
IsBoxed: true,
}
var X = NotGate
var PauliXGate = NotGate
var HadamardGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1/math.Sqrt(2), 0)), Complex(complex(1/math.Sqrt(2), 0))},
{Complex(complex(1/math.Sqrt(2), 0)), Complex(complex(-1/math.Sqrt(2), 0))},
}),
Name: "Hadamard",
Symbol: "H",
IsBoxed: true,
}
var H = HadamardGate
var IdentityGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(complex(1, 0))},
}),
Name: "Identity",
Symbol: "I",
IsBoxed: true,
}
var I = IdentityGate
func ConstructNIdentity(s int) Gate {
out := NewMatrix()
for i := 0; i < s; i++ {
out.TensorProduct(*out, I.Matrix)
}
return Gate{Matrix: *out, Symbol: "I", Name: "Identity", IsBoxed: true}
}
var PauliZGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(complex(-1, 0))},
}),
Name: "Pauli Z",
Symbol: "Z",
IsBoxed: true,
}
var SGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(cmplx.Exp(1i * math.Pi / 2))},
}),
IsBoxed: true,
Name: "S",
Symbol: "S",
}
var SdGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(cmplx.Exp(-1i * math.Pi / 2))},
}),
Name: "Sd",
Symbol: "Sd",
IsBoxed: true,
}
var TGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(cmplx.Exp(1i * math.Pi / 4))},
}),
Name: "T",
Symbol: "T",
IsBoxed: true,
}
var TdGate = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(cmplx.Exp(-1i * math.Pi / 4))},
}),
Name: "T",
Symbol: "Td",
IsBoxed: true,
}
var CNOT = Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0)), Complex(complex(0, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(complex(1, 0)), Complex(complex(0, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(complex(0, 0)), Complex(complex(0, 0)), Complex(complex(1, 0))},
{Complex(complex(0, 0)), Complex(complex(0, 0)), Complex(complex(1, 0)), Complex(complex(0, 0))},
}),
Name: "Controlled Not",
Symbol: "CNOT",
IsBoxed: false,
}
func ROT(angle float64, symbol string) Gate {
return Gate{
Matrix: *NewMatrixFromElements([][]Complex{
{Complex(complex(1, 0)), Complex(complex(0, 0))},
{Complex(complex(0, 0)), Complex(cmplx.Exp(1i * complex(angle, 0)))},
}),
Name: "ROT",
Symbol: symbol,
IsBoxed: false,
}
}
func SWAP(gap int) Gate {
bits := gap + 2
m := ConstructNIdentity(bits).Matrix
length := int(math.Pow(2, float64(bits)))
offset := length/2 - 1
for i := 1; i < length/2; i += 2 {
m.Set(i, i, NewComplex(0, 0))
m.Set(i+offset, i, NewComplex(1, 0))
m.Set(i, i+offset, NewComplex(1, 0))
}
for i := length / 2; i < length; i += 2 {
m.Set(i, i, NewComplex(0, 0))
}
return Gate{
Matrix: m,
Name: "SWAP",
Symbol: "X",
IsBoxed: false,
}
}