53 {
54
55 SECTION( LABEL_CORRECTNESS ) {
56
57 GENERATE( range(0,10) );
58
59 std::string charSet = GENERATE( "IXYZ", "ixyz", "0123", "iX2z" );
60
61 int numPaulis = GENERATE( 1, 2, 5, 10, 20, 30, 31, 32, 33, 60, 64 );
62 auto targets = getRandomSubRange(0, 64, numPaulis);
63
64
65 vector<char> pauliChars(numPaulis, 'I');
66 vector<int> pauliInts(numPaulis, 0);
67 long long unsigned lowValue = 0;
68 long long unsigned highValue = 0;
69
70 for (int i=0; i<numPaulis; i++) {
72 pauliInts[i] = v;
73 pauliChars[i] = charSet[v];
74
75 int t = targets[i];
76 (t < 32)?
77 (lowValue += v * getPow2(2 * t)):
78 (highValue += v * getPow2(2 * (t-32)));
79 }
80
81 SECTION( LABEL_C_INTERFACE ) {
82
83 SECTION( "from chars" ) {
84
85 CAPTURE( targets, pauliChars );
86
88
89 REQUIRE( str.lowPaulis == lowValue );
90 REQUIRE( str.highPaulis == highValue );
91 }
92
93 SECTION( "from ints" ) {
94
95 CAPTURE( targets, pauliChars );
96
98
99 REQUIRE( str.lowPaulis == lowValue );
100 REQUIRE( str.highPaulis == highValue );
101 }
102
103 SECTION( "from literal" ) {
104
105
106
107 int targ = targets[0];
108 CAPTURE( targ );
109
110 const char* in = "X";
112 REQUIRE( str.lowPaulis == ((targ < 32)? getPow2(2*targ) : 0) );
113 REQUIRE( str.highPaulis == ((targ >= 32)? getPow2(2*(targ-32)) : 0) );
114 }
115 }
116
117 SECTION( LABEL_CPP_INTERFACE ) {
118
119
120 pauliChars.push_back('\0');
121 std::string in = std::string(pauliChars.data());
122
123 SECTION( "from string" ) {
124
125 CAPTURE( targets, pauliChars );
126
128
129 REQUIRE( str.lowPaulis == lowValue );
130 REQUIRE( str.highPaulis == highValue );
131 }
132
133 SECTION( "from vector" ) {
134
135 CAPTURE( targets, pauliChars );
136
138
139 REQUIRE( str.lowPaulis == lowValue );
140 REQUIRE( str.highPaulis == highValue );
141 }
142
143 SECTION( "from literal" ) {
144
145
146
147 int targ = targets[0];
148 CAPTURE( targ );
149
151 REQUIRE( str.lowPaulis == ((targ < 32)? getPow2(2*targ) : 0) );
152 REQUIRE( str.highPaulis == ((targ >= 32)? getPow2(2*(targ-32)) : 0) );
153 }
154
155 SECTION( "from only string" ) {
156
157 CAPTURE( targets, pauliChars );
158
159 char chars[65];
160 chars[64] = '\0';
161 for (int i=0; i<64; i++)
162 chars[i] = 'I';
163
164
165 for (int i=0; i<numPaulis; i++)
166 chars[64-targets[i]-1] = pauliChars[i];
167
168 std::string all = std::string(chars);
170
171 CAPTURE( all );
172 REQUIRE( str.lowPaulis == lowValue );
173 REQUIRE( str.highPaulis == highValue );
174 }
175 }
176 }
177
178 SECTION( LABEL_VALIDATION ) {
179
180 SECTION( "invalid target" ) {
181
182 int target = GENERATE( -1, 64, 65, 9999 );
183
184 REQUIRE_THROWS_WITH(
getPauliStr(
"X", {target}), ContainsSubstring(
"Invalid index") );
185 }
186
187 SECTION( "duplicated target" ) {
188
189 REQUIRE_THROWS_WITH(
getPauliStr(
"XY", {0,0}), ContainsSubstring(
"duplicate") );
190 }
191
192 SECTION( "invalid number of paulis" ) {
193
194 int numPaulis = GENERATE( -1, 0 );
195
196 REQUIRE_THROWS_WITH(
getPauliStr(
"X",
nullptr, numPaulis), ContainsSubstring(
"must contain at least one Pauli operator") );
197 }
198
199 SECTION( "string terminated early" ) {
200
201 REQUIRE_THROWS_WITH(
getPauliStr(
"X", {1,2}), ContainsSubstring(
"different number of Pauli operators") && ContainsSubstring(
"qubit indices") );
202 }
203
204 SECTION( "unrecognised char" ) {
205
206 REQUIRE_THROWS_WITH(
getPauliStr(
"hi", {1,2}), ContainsSubstring(
"unrecognised Pauli character") );
207 }
208 }
209}
int getRandomInt(int min, int maxExcl)