Loading examples/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -45,3 +45,4 @@ add_qcor_compile_and_exe_test(qrt_bell_ctrl bell/bell_control.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_simple qpu_lambda/lambda_test.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_bell qpu_lambda/lambda_test_bell.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_grover qpu_lambda/grover_lambda_oracle.cpp) add_qcor_compile_and_exe_test(qrt_qpu_arith_adder arithmetic/simple.cpp) No newline at end of file examples/arithmetic/simple.cpp +8 −5 Original line number Diff line number Diff line Loading @@ -9,19 +9,22 @@ __qpu__ void test_adder(qreg a, qreg b, qubit cin, qubit cout) { Measure(cout); } // Compile: int main(int argc, char **argv) { set_shots(1024); // Set the inputs to the adder auto a = qalloc(2); auto b = qalloc(2); auto carry_in = qalloc(1); auto carry_out = qalloc(1); // Execute: // test_adder(); // test_adder::print_kernel(a, b, carry_in[0], carry_out[0]); test_adder(a, b, carry_in[0], carry_out[0]); test_adder::print_kernel(a, b, carry_in[0], carry_out[0]); test_adder(b, a, carry_in[0], carry_out[0]); b.print(); // 00 carry_out.print(); // 1 qcor_expect(b.counts().size() == 1); qcor_expect(b.counts()["00"] == 1024); carry_out.print(); // 1 qcor_expect(carry_out.counts().size() == 1); qcor_expect(carry_out.counts()["1"] == 1024); return 0; } No newline at end of file lib/impl/integer_arithmetic.hpp +19 −7 Original line number Diff line number Diff line Loading @@ -5,17 +5,29 @@ __qpu__ void majority(qubit a, qubit b, qubit c) { X::ctrl(c, b); X::ctrl(c, a); X::ctrl({a, b}, c); X::ctrl({b, a}, c); } // Note: this is not the adjoint of majority.... // Could be a good use case for per-qubit uncompute... __qpu__ void unmajority(qubit a, qubit b, qubit c) { X::ctrl({b, a}, c); X::ctrl(c, a); X::ctrl(a, b); } // Add a to b and save to b // c_in and c_out are carry bits (in and out) __qpu__ void ripple_add(qreg a, qreg b, qubit c_in, qubit c_out) { compute { majority(c_in, b[0], a[0]); for (auto j : range(a.size() - 1)) { majority(a[j], b[j + 1], a[j + 1]); } X::ctrl(a.tail(), c_out); for (int j = a.size() - 2; j >= 0; --j) { unmajority(a[j], b[j + 1], a[j + 1]); } action { X::ctrl(a.tail(), c_out); } unmajority(c_in, b[0], a[0]); } No newline at end of file Loading
examples/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -45,3 +45,4 @@ add_qcor_compile_and_exe_test(qrt_bell_ctrl bell/bell_control.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_simple qpu_lambda/lambda_test.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_bell qpu_lambda/lambda_test_bell.cpp) add_qcor_compile_and_exe_test(qrt_qpu_lambda_grover qpu_lambda/grover_lambda_oracle.cpp) add_qcor_compile_and_exe_test(qrt_qpu_arith_adder arithmetic/simple.cpp) No newline at end of file
examples/arithmetic/simple.cpp +8 −5 Original line number Diff line number Diff line Loading @@ -9,19 +9,22 @@ __qpu__ void test_adder(qreg a, qreg b, qubit cin, qubit cout) { Measure(cout); } // Compile: int main(int argc, char **argv) { set_shots(1024); // Set the inputs to the adder auto a = qalloc(2); auto b = qalloc(2); auto carry_in = qalloc(1); auto carry_out = qalloc(1); // Execute: // test_adder(); // test_adder::print_kernel(a, b, carry_in[0], carry_out[0]); test_adder(a, b, carry_in[0], carry_out[0]); test_adder::print_kernel(a, b, carry_in[0], carry_out[0]); test_adder(b, a, carry_in[0], carry_out[0]); b.print(); // 00 carry_out.print(); // 1 qcor_expect(b.counts().size() == 1); qcor_expect(b.counts()["00"] == 1024); carry_out.print(); // 1 qcor_expect(carry_out.counts().size() == 1); qcor_expect(carry_out.counts()["1"] == 1024); return 0; } No newline at end of file
lib/impl/integer_arithmetic.hpp +19 −7 Original line number Diff line number Diff line Loading @@ -5,17 +5,29 @@ __qpu__ void majority(qubit a, qubit b, qubit c) { X::ctrl(c, b); X::ctrl(c, a); X::ctrl({a, b}, c); X::ctrl({b, a}, c); } // Note: this is not the adjoint of majority.... // Could be a good use case for per-qubit uncompute... __qpu__ void unmajority(qubit a, qubit b, qubit c) { X::ctrl({b, a}, c); X::ctrl(c, a); X::ctrl(a, b); } // Add a to b and save to b // c_in and c_out are carry bits (in and out) __qpu__ void ripple_add(qreg a, qreg b, qubit c_in, qubit c_out) { compute { majority(c_in, b[0], a[0]); for (auto j : range(a.size() - 1)) { majority(a[j], b[j + 1], a[j + 1]); } X::ctrl(a.tail(), c_out); for (int j = a.size() - 2; j >= 0; --j) { unmajority(a[j], b[j + 1], a[j + 1]); } action { X::ctrl(a.tail(), c_out); } unmajority(c_in, b[0], a[0]); } No newline at end of file