@@ -3,6 +3,7 @@ package gate_test
33import (
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+
108166func TestU (t * testing.T ) {
109167 cases := []struct {
110168 in , want * matrix.Matrix
0 commit comments