6
6
7
7
import numpy as np
8
8
import pytest
9
- from numpy .typing import ArrayLike
9
+ from numpy .typing import NDArray
10
10
11
11
from opensquirrel import I
12
12
from opensquirrel .common import ATOL
13
13
from opensquirrel .ir import (
14
14
Axis ,
15
+ AxisLike ,
15
16
Bit ,
16
17
BlochSphereRotation ,
17
18
ControlledGate ,
@@ -44,7 +45,7 @@ def test_axis_getter(self, axis: Axis) -> None:
44
45
(Axis (0 , 1 , 0 ), [0 , 1 , 0 ]),
45
46
],
46
47
)
47
- def test_axis_setter_no_error (self , axis : Axis , new_axis : ArrayLike , expected_axis : ArrayLike ) -> None :
48
+ def test_axis_setter_no_error (self , axis : Axis , new_axis : AxisLike , expected_axis : list [ float ] ) -> None :
48
49
axis .value = new_axis # type: ignore[assignment]
49
50
np .testing .assert_array_equal (axis , expected_axis )
50
51
@@ -59,6 +60,7 @@ def test_axis_setter_no_error(self, axis: Axis, new_axis: ArrayLike, expected_ax
59
60
ValueError ,
60
61
"axis requires an ArrayLike of length 3, but received an ArrayLike of length 4" ,
61
62
),
63
+ ([0 , 0 , 0 ], ValueError , "axis requires at least one element to be non-zero" ),
62
64
],
63
65
)
64
66
def test_axis_setter_with_error (
@@ -93,6 +95,63 @@ def test_eq_true(self, axis: Axis, other: Any) -> None:
93
95
def test_eq_false (self , axis : Axis , other : Any ) -> None :
94
96
assert axis != other
95
97
98
+ @pytest .mark .parametrize (
99
+ ("axis" , "expected" ),
100
+ [
101
+ ([1 , 0 , 0 ], np .array ([1 , 0 , 0 ], dtype = np .float64 )),
102
+ ([0 , 0 , 0 ], ValueError ),
103
+ ([1 , 2 ], ValueError ),
104
+ ([1 , 2 , 3 , 4 ], ValueError ),
105
+ ([0 , 1 , 0 ], np .array ([0 , 1 , 0 ], dtype = np .float64 )),
106
+ (["a" , "b" , "c" ], TypeError ),
107
+ ],
108
+ )
109
+ def test_constructor (self , axis : AxisLike , expected : Any ) -> None :
110
+ if isinstance (expected , type ) and issubclass (expected , Exception ):
111
+ with pytest .raises (expected ):
112
+ Axis (axis )
113
+ else :
114
+ assert isinstance (expected , np .ndarray )
115
+ obj = Axis (axis )
116
+ np .testing .assert_array_equal (obj .value , expected )
117
+
118
+ @pytest .mark .parametrize (
119
+ ("axis" , "expected" ),
120
+ [
121
+ ([1 , 0 , 0 ], np .array ([1 , 0 , 0 ], dtype = np .float64 )),
122
+ ([0 , 0 , 0 ], ValueError ),
123
+ ([1 , 2 ], ValueError ),
124
+ ([1 , 2 , 3 , 4 ], ValueError ),
125
+ ([0 , 1 , 0 ], np .array ([0 , 1 , 0 ], dtype = np .float64 )),
126
+ (["a" , "b" , "c" ], TypeError ),
127
+ ],
128
+ )
129
+ def test_parse (self , axis : AxisLike , expected : Any ) -> None :
130
+ if isinstance (expected , type ) and issubclass (expected , Exception ):
131
+ with pytest .raises (expected ):
132
+ Axis .parse (axis )
133
+ else :
134
+ assert isinstance (expected , np .ndarray )
135
+ obj = Axis .parse (axis )
136
+ np .testing .assert_array_equal (obj , expected )
137
+
138
+ @pytest .mark .parametrize (
139
+ ("axis" , "expected" ),
140
+ [
141
+ (np .array ([1 , 0 , 0 ], dtype = np .float64 ), np .array ([1 , 0 , 0 ], dtype = np .float64 )),
142
+ (np .array ([0 , 1 , 0 ], dtype = np .float64 ), np .array ([0 , 1 , 0 ], dtype = np .float64 )),
143
+ (np .array ([0 , 0 , 1 ], dtype = np .float64 ), np .array ([0 , 0 , 1 ], dtype = np .float64 )),
144
+ (
145
+ np .array ([1 , 1 , 1 ], dtype = np .float64 ),
146
+ np .array ([1 / np .sqrt (3 ), 1 / np .sqrt (3 ), 1 / np .sqrt (3 )], dtype = np .float64 ),
147
+ ),
148
+ ],
149
+ )
150
+ def test_normalize (self , axis : AxisLike , expected : NDArray [np .float64 ]) -> None :
151
+ obj = Axis .normalize (np .array (axis , dtype = np .float64 ))
152
+ assert isinstance (expected , np .ndarray )
153
+ np .testing .assert_array_almost_equal (obj , expected )
154
+
96
155
97
156
class TestIR :
98
157
def test_cnot_equality (self ) -> None :
0 commit comments