Skip to content

Commit 0e80796

Browse files
authored
Merge pull request #59 from itsubaki/AXBXC
2 parents 97850a6 + 607c27f commit 0e80796

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

cmd/counting/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ func main() {
5353
if err != nil {
5454
panic(err)
5555
}
56-
defer f.Close()
56+
defer func() {
57+
if err := f.Close(); err != nil {
58+
fmt.Println(err)
59+
}
60+
}()
5761

58-
pprof.StartCPUProfile(f)
62+
if err := pprof.StartCPUProfile(f); err != nil {
63+
panic(err)
64+
}
5965
defer pprof.StopCPUProfile()
6066

6167
// flags

quantum/gate/gate.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ func U(theta, phi, lambda float64) *matrix.Matrix {
2727
)
2828
}
2929

30+
// SU returns a special unitary gate.
31+
func SU(theta, phi, lambda float64) *matrix.Matrix {
32+
phase := cmplx.Exp(complex(0, -1*(phi+lambda)/2))
33+
return U(theta, phi, lambda).Mul(phase)
34+
}
35+
36+
// ABC returns alpha, A, B, C such that ABC = I, exp(i * alpha) * AXBXC = U(theta, phi, lambda).
37+
func ABC(theta, phi, lambda float64) (float64, *matrix.Matrix, *matrix.Matrix, *matrix.Matrix) {
38+
alpha := (phi + lambda) / 2
39+
A := matrix.MatMul(RZ(phi), RY(theta/2))
40+
B := matrix.MatMul(RY(-theta/2), RZ(-(lambda+phi)/2))
41+
C := RZ((lambda - phi) / 2)
42+
return alpha, A, B, C
43+
}
44+
3045
// I returns an identity gate.
3146
func I(n ...int) *matrix.Matrix {
3247
return matrix.TensorProductN(matrix.New(

quantum/gate/gate_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gate_test
33
import (
44
"fmt"
55
"math"
6+
"math/cmplx"
67
"testing"
78

89
"github.com/itsubaki/q/math/matrix"
@@ -105,6 +106,63 @@ func ExampleTensorProduct() {
105106
// [(0+0i) (1+0i) (0+0i) (0+0i)]
106107
}
107108

109+
func ExampleSU() {
110+
u := gate.U(math.Pi/4, math.Pi/2, math.Pi/8)
111+
su := gate.SU(math.Pi/4, math.Pi/2, math.Pi/8)
112+
113+
usud := matrix.MatMul(u, su.Dagger())
114+
fmt.Println(usud.Mul(1 / usud.Data[0]).Equal(gate.I()))
115+
116+
det := func(m *matrix.Matrix) complex128 {
117+
return m.Data[0]*m.Data[3] - m.Data[1]*m.Data[2]
118+
}
119+
120+
fmt.Printf("%.4f\n", det(u))
121+
fmt.Printf("%.4f\n", det(su))
122+
123+
// Output:
124+
// true
125+
// (-0.3827+0.9239i)
126+
// (1.0000+0.0000i)
127+
}
128+
129+
func ExampleABC() {
130+
theta, phi, lambda := math.Pi/2, math.Pi/4, math.Pi/8
131+
132+
alpha, a, b, c := gate.ABC(theta, phi, lambda)
133+
fmt.Println(matrix.MatMul(a, b, c).Equal(gate.I()))
134+
135+
axbxc := matrix.MatMul(a, gate.X(), b, gate.X(), c)
136+
fmt.Println(axbxc.Equal(gate.SU(theta, phi, lambda)))
137+
138+
axbxcp := axbxc.Mul(cmplx.Exp(complex(0, alpha)))
139+
fmt.Println(axbxcp.Equal(gate.U(theta, phi, lambda)))
140+
141+
// Output:
142+
// true
143+
// true
144+
// true
145+
}
146+
147+
func FuzzABC(f *testing.F) {
148+
f.Add(0.0, 0.0, 0.0)
149+
f.Add(math.Pi, math.Pi, math.Pi)
150+
f.Add(math.Pi/2, math.Pi/4, math.Pi/8)
151+
152+
f.Fuzz(func(t *testing.T, theta, phi, lambda float64) {
153+
alpha, a, b, c := gate.ABC(theta, phi, lambda)
154+
if !matrix.MatMul(a, b, c).Equal(gate.I()) {
155+
t.Fatalf("ABC != I (theta=%v phi=%v lambda=%v)", theta, phi, lambda)
156+
}
157+
158+
phase := cmplx.Exp(complex(0, alpha))
159+
axbxc := matrix.MatMul(a, gate.X(), b, gate.X(), c).Mul(phase)
160+
if !axbxc.Equal(gate.U(theta, phi, lambda)) {
161+
t.Fatalf("AXBXC != U (theta=%v phi=%v lambda=%v)", theta, phi, lambda)
162+
}
163+
})
164+
}
165+
108166
func TestU(t *testing.T) {
109167
cases := []struct {
110168
in, want *matrix.Matrix

0 commit comments

Comments
 (0)