38 SECTION(
"correctness" ) {
40 int qubit = GENERATE( range(0,NUM_QUBITS) );
41 int outcome = GENERATE( 0, 1 );
44 GENERATE( range(0,10) );
46 SECTION(
"state-vector" ) {
54 for (
size_t ind=0; ind<vecRef.size(); ind++) {
55 int bit = (ind >> qubit) & 1;
57 prob += pow(abs(vecRef[ind]), 2);
61 for (
size_t ind=0; ind<vecRef.size(); ind++) {
62 int bit = (ind >> qubit) & 1;
64 vecRef[ind] /= sqrt(prob);
69 qreal res = collapseToOutcome(vec, qubit, outcome);
70 REQUIRE( res == Approx(prob) );
73 SECTION(
"density-matrix" ) {
81 for (
size_t ind=0; ind<matRef.size(); ind++) {
82 int bit = (ind >> qubit) & 1;
84 tr += matRef[ind][ind];
86 REQUIRE( imag(tr) == Approx(0).margin(REAL_EPS) );
87 qreal prob = real(tr);
90 for (
size_t r=0; r<matRef.size(); r++) {
91 for (
size_t c=0; c<matRef.size(); c++) {
92 int ketBit = (c >> qubit) & 1;
93 int braBit = (r >> qubit) & 1;
95 if (ketBit == outcome && braBit == outcome)
102 qreal res = collapseToOutcome(mat, qubit, outcome);
103 REQUIRE( res == Approx(prob) );
107 SECTION(
"input validation" ) {
109 SECTION(
"qubit index" ) {
111 int qubit = GENERATE( -1, NUM_QUBITS );
113 REQUIRE_THROWS_WITH( collapseToOutcome(mat, qubit, outcome), ContainsSubstring(
"Invalid target qubit") );
115 SECTION(
"outcome value" ) {
118 int outcome = GENERATE( -1, 2 );
119 REQUIRE_THROWS_WITH( collapseToOutcome(mat, qubit, outcome), ContainsSubstring(
"qubit measurement outcome") && ContainsSubstring(
"invalid") );
121 SECTION(
"outcome probability" ) {
124 REQUIRE_THROWS_WITH( collapseToOutcome(vec, 0, 1), ContainsSubstring(
"impossibly unlikely") );
126 REQUIRE_THROWS_WITH( collapseToOutcome(vec, 0, 0), ContainsSubstring(
"impossibly unlikely") );
144 SECTION(
"correctness" ) {
146 int qubit = GENERATE( range(0,NUM_QUBITS) );
149 GENERATE( range(0,10) );
151 SECTION(
"state-vector" ) {
156 int outcome = measure(vec, qubit);
157 REQUIRE( (outcome == 0 || outcome == 1) );
161 for (
size_t ind=0; ind<vecRef.size(); ind++) {
162 int bit = (ind >> qubit) & 1;
164 prob += pow(abs(vecRef[ind]), 2);
167 REQUIRE( prob > REAL_EPS );
170 for (
size_t ind=0; ind<vecRef.size(); ind++) {
171 int bit = (ind >> qubit) & 1;
173 vecRef[ind] /= sqrt(prob);
179 SECTION(
"density-matrix" ) {
184 int outcome = measure(mat, qubit);
185 REQUIRE( (outcome == 0 || outcome == 1) );
189 for (
size_t ind=0; ind<matRef.size(); ind++) {
190 int bit = (ind >> qubit) & 1;
192 prob += real(matRef[ind][ind]);
195 REQUIRE( prob > REAL_EPS );
198 for (
size_t r=0; r<matRef.size(); r++) {
199 for (
size_t c=0; c<matRef.size(); c++) {
200 int ketBit = (c >> qubit) & 1;
201 int braBit = (r >> qubit) & 1;
203 if (ketBit == outcome && braBit == outcome)
204 matRef[r][c] /= prob;
213 SECTION(
"input validation" ) {
215 SECTION(
"qubit index" ) {
217 int qubit = GENERATE( -1, NUM_QUBITS );
218 REQUIRE_THROWS_WITH( measure(vec, qubit), ContainsSubstring(
"Invalid target qubit") );
236 SECTION(
"correctness" ) {
238 int qubit = GENERATE( range(0,NUM_QUBITS) );
241 GENERATE( range(0,10) );
243 SECTION(
"state-vector" ) {
249 int outcome = measureWithStats(vec, qubit, &res);
250 REQUIRE( (outcome == 0 || outcome == 1) );
254 for (
size_t ind=0; ind<vecRef.size(); ind++) {
255 int bit = (ind >> qubit) & 1;
257 prob += pow(abs(vecRef[ind]), 2);
260 REQUIRE( prob == Approx(res) );
263 for (
size_t ind=0; ind<vecRef.size(); ind++) {
264 int bit = (ind >> qubit) & 1;
266 vecRef[ind] /= sqrt(prob);
272 SECTION(
"density-matrix" ) {
278 int outcome = measureWithStats(mat, qubit, &res);
279 REQUIRE( (outcome == 0 || outcome == 1) );
283 for (
size_t ind=0; ind<matRef.size(); ind++) {
284 int bit = (ind >> qubit) & 1;
286 prob += real(matRef[ind][ind]);
289 REQUIRE( prob == Approx(res) );
292 for (
size_t r=0; r<matRef.size(); r++) {
293 for (
size_t c=0; c<matRef.size(); c++) {
294 int ketBit = (c >> qubit) & 1;
295 int braBit = (r >> qubit) & 1;
297 if (ketBit == outcome && braBit == outcome)
298 matRef[r][c] /= prob;
307 SECTION(
"input validation" ) {
309 SECTION(
"qubit index" ) {
311 int qubit = GENERATE( -1, NUM_QUBITS );
313 REQUIRE_THROWS_WITH( measureWithStats(vec, qubit, &res), ContainsSubstring(
"Invalid target qubit") );