1818import numpy as np
1919import pytest
2020
21+ import qualtran .rotation_synthesis .math_config as mc
2122from qualtran .rotation_synthesis .matrix import su2_ct
2223from qualtran .rotation_synthesis .rings import zsqrt2 , zw
2324
@@ -54,9 +55,9 @@ def test_multiply(seq):
5455 for i in seq :
5556 g = g @ _GATES [i ]
5657 k += i >= 3 # is a T gate.
57- g_numpy = (g_numpy @ _GATES [i ].numpy ( )) / _SQRT2
58+ g_numpy = (g_numpy @ _GATES [i ].matrix . astype ( complex )) / _SQRT2
5859 assert g .det () == 2 * _LAMBDA ** k
59- np .testing .assert_allclose (g .numpy ( ) / _SQRT2 , g_numpy , atol = 1e-9 )
60+ np .testing .assert_allclose (g .matrix . astype ( complex ) / _SQRT2 , g_numpy , atol = 1e-9 )
6061
6162
6263@pytest .mark .parametrize ("g" , _make_random_su (10 , 3 , random_cliffords = False , seed = 0 ))
@@ -76,9 +77,9 @@ def test_adjoint(g):
7677 ["g" , "g_numpy" ], [[su2_ct .Tx , _TX_numpy ], [su2_ct .Ty , _TY_numpy ], [su2_ct .Tz , _TZ_numpy ]]
7778)
7879def test_t_gates (g , g_numpy ):
79- np .testing .assert_allclose (g .numpy ( ) / _SQRT2 / np .sqrt (2 + _SQRT2 ), g_numpy )
80+ np .testing .assert_allclose (g .matrix . astype ( complex ) / _SQRT2 / np .sqrt (2 + _SQRT2 ), g_numpy )
8081 np .testing .assert_allclose (
81- g .adjoint ().numpy ( ) / _SQRT2 / np .sqrt (2 + _SQRT2 ), g_numpy .T .conjugate ()
82+ g .adjoint ().matrix . astype ( complex ) / _SQRT2 / np .sqrt (2 + _SQRT2 ), g_numpy .T .conjugate ()
8283 )
8384
8485
@@ -101,8 +102,33 @@ def test_generate_cliffords():
101102 cirq_cliffords = [
102103 cirq .unitary (c ) for c in cirq .SingleQubitCliffordGate .all_single_qubit_cliffords
103104 ]
104- assert np .allclose (np .abs ([np .linalg .det (c .numpy ( )) for c in cliffords ]), 2 )
105+ assert np .allclose (np .abs ([np .linalg .det (c .matrix . astype ( complex )) for c in cliffords ]), 2 )
105106 sqrt2 = np .sqrt (2 )
106107 for c in cliffords :
107- u = c .numpy ( ) / sqrt2
108+ u = c .matrix . astype ( complex ) / sqrt2
108109 assert np .any ([are_close_up_to_global_phase (u , c ) for c in cirq_cliffords ])
110+
111+
112+ @pytest .mark .parametrize ("g" , _make_random_su (50 , 5 , random_cliffords = True , seed = 0 ))
113+ @pytest .mark .parametrize ("config" , [None , mc .NumpyConfig ])
114+ def test_rescale (g : su2_ct .SU2CliffordT , config ):
115+ np .testing .assert_allclose (g .numpy (config ), g .rescale ().numpy (config ))
116+
117+
118+ def test_num_t_gates ():
119+ for clifford in su2_ct .generate_cliffords ():
120+ assert clifford .num_t_gates () == 0
121+
122+ for t in su2_ct .Ts :
123+ assert (clifford @ t ).num_t_gates () == 1
124+
125+ for t1 in su2_ct .Ts :
126+ for t2 in su2_ct .Ts :
127+ assert (t1 @ t2 ).num_t_gates () == 2
128+
129+ for t in su2_ct .Ts :
130+ # still two T gates
131+ assert (t @ t .adjoint ()).num_t_gates () == 2
132+
133+ # We need to call .rescale to remove the common factor and reduce the T count.
134+ assert (t @ t .adjoint ()).rescale ().num_t_gates () == 0
0 commit comments