Loading examples/CMakeLists.txt +16 −0 Original line number Diff line number Diff line # Add a QCOR compilation and run test. # The test can use qcor_expect macro to check expected results. function(add_qcor_compile_and_exe_test test_name relative_source_location) add_test( NAME ${test_name} COMMAND bash -c "${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/${relative_source_location} -o ${test_name}; \ ${CMAKE_CURRENT_BINARY_DIR}/${test_name}" ) endfunction() add_test(NAME qrt_bell_multi COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/bell/bell_multi_qreg.cpp) add_test(NAME qrt_add_3_5 COMMAND ${CMAKE_BINARY_DIR}/qcor -v -c ${CMAKE_CURRENT_SOURCE_DIR}/adder/add_3_5.cpp WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/adder) add_test(NAME qrt_mixed_language COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/mixed_language.cpp) Loading Loading @@ -29,3 +41,7 @@ add_test(NAME quasimo_verified_qpe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURR add_test(NAME hadamard_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/simple_hadamard_test.cpp) add_test(NAME multi_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/multiple_controls.cpp) 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) examples/bell/bell_control.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -6,9 +6,14 @@ __qpu__ void bell(qubit q, qubit r) { } int main() { set_shots(1024); auto q = qalloc(1); auto r = qalloc(1); bell(q[0],r[0]); q.print(); r.print(); qcor_expect(q.counts().size() == 2); qcor_expect(r.counts().size() == 2); qcor_expect(q.counts()["0"] == r.counts()["0"]); qcor_expect(q.counts()["1"] == r.counts()["1"]); } No newline at end of file examples/qpu_lambda/grover_lambda_oracle.cpp +38 −3 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ __qpu__ void run_grover(qreg q, GroverPhaseOracle oracle, } int main() { set_shots(1024); const int N = 3; // Write the oracle as a quantum lambda function Loading @@ -47,10 +49,43 @@ int main() { // Allocate some qubits auto q = qalloc(N); oracle.print_kernel(q); int iterations = 1; // Call grover given the oracle and n iterations run_grover(q, oracle, 1); run_grover(q, oracle, iterations); // print the histogram q.print(); // Grover lambda: // amplification lambda auto amplification_lambda = qpu_lambda([](qreg q) { print("hey from amplification_lambda"); H(q); X(q); auto ctrl_bits = q.head(q.size() - 1); auto last_qubit = q.tail(); Z::ctrl(ctrl_bits, last_qubit); X(q); H(q); }); // Capture the grover lambda and iterations directly from the enclosing scope. auto grover_lambda = qpu_lambda([](qreg q) { H(q); for (int i = 0; i < iterations; i++) { oracle(q); amplification_lambda(q); } Measure(q); }, oracle, iterations, amplification_lambda); auto q_lambda = qalloc(N); std::cout << "Lamda result:\n"; grover_lambda.print_kernel(q_lambda); grover_lambda(q_lambda); q_lambda.print(); // Two bitstrings only. qcor_expect(q_lambda.counts().size() == 2); } No newline at end of file examples/qpu_lambda/lambda_test.cpp +34 −1 Original line number Diff line number Diff line #include "qcor.hpp" int main(int argc, char** argv) { set_shots(1024); int n = argc; double m = 22; Loading @@ -11,16 +12,48 @@ int main(int argc, char** argv) { H(q[0]); } Measure(q[0]); m++; }, n, m); // By-value capture auto b = qpu_lambda([=](qreg q) { print("[By-val] n was captured, and is ", n); print("[By-val] m was captured, and is ", m); for (int i = 0; i < n; i++) { H(q[0]); } Measure(q[0]); }, n, m); auto q = qalloc(1); a(q); auto qb = qalloc(1); b(qb); print("m after lambda", m); // m was modified by the lambda... qcor_expect(m == 23); q.print(); // Run 1 Hadamard qcor_expect(q.counts().size() == 2); qcor_expect(q.counts()["0"] > 400); qcor_expect(q.counts()["1"] > 400); qcor_expect(q.counts()["0"] + q.counts()["1"] == 1024); n = 2; m = 33.0; auto r = qalloc(1); print("running again to show capture variables are captured by reference"); auto rb = qalloc(1); print("running again to show capture variables are captured by reference and by value"); a(r); b(rb); r.print(); // H - H == I qcor_expect(r.counts()["0"] == 1024); // b still uses n = 1 (capture by value...) rb.print(); qcor_expect(rb.counts().size() == 2); qcor_expect(rb.counts()["0"] > 400); qcor_expect(rb.counts()["1"] > 400); qcor_expect(rb.counts()["0"] + rb.counts()["1"] == 1024); } examples/qpu_lambda/lambda_test_bell.cpp 0 → 100644 +24 −0 Original line number Diff line number Diff line #include "qcor.hpp" int main(int argc, char** argv) { set_shots(1024); auto x_lambda = qpu_lambda([](qubit q) { X(q); }); auto bell = qpu_lambda([](qreg q) { H(q[0]); // Call the captured lambda x_lambda.ctrl(q[0], q[1]); Measure(q); }, x_lambda); auto q = qalloc(2); bell(q); q.print(); qcor_expect(q.counts().size() == 2); qcor_expect(q.counts()["00"] > 400); qcor_expect(q.counts()["11"] > 400); // Entangled... qcor_expect(q.counts()["00"] + q.counts()["11"] == 1024); } Loading
examples/CMakeLists.txt +16 −0 Original line number Diff line number Diff line # Add a QCOR compilation and run test. # The test can use qcor_expect macro to check expected results. function(add_qcor_compile_and_exe_test test_name relative_source_location) add_test( NAME ${test_name} COMMAND bash -c "${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/${relative_source_location} -o ${test_name}; \ ${CMAKE_CURRENT_BINARY_DIR}/${test_name}" ) endfunction() add_test(NAME qrt_bell_multi COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/bell/bell_multi_qreg.cpp) add_test(NAME qrt_add_3_5 COMMAND ${CMAKE_BINARY_DIR}/qcor -v -c ${CMAKE_CURRENT_SOURCE_DIR}/adder/add_3_5.cpp WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/adder) add_test(NAME qrt_mixed_language COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/mixed_language.cpp) Loading Loading @@ -29,3 +41,7 @@ add_test(NAME quasimo_verified_qpe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURR add_test(NAME hadamard_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/simple_hadamard_test.cpp) add_test(NAME multi_ctrl_test COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/ctrl-gates/multiple_controls.cpp) 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)
examples/bell/bell_control.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -6,9 +6,14 @@ __qpu__ void bell(qubit q, qubit r) { } int main() { set_shots(1024); auto q = qalloc(1); auto r = qalloc(1); bell(q[0],r[0]); q.print(); r.print(); qcor_expect(q.counts().size() == 2); qcor_expect(r.counts().size() == 2); qcor_expect(q.counts()["0"] == r.counts()["0"]); qcor_expect(q.counts()["1"] == r.counts()["1"]); } No newline at end of file
examples/qpu_lambda/grover_lambda_oracle.cpp +38 −3 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ __qpu__ void run_grover(qreg q, GroverPhaseOracle oracle, } int main() { set_shots(1024); const int N = 3; // Write the oracle as a quantum lambda function Loading @@ -47,10 +49,43 @@ int main() { // Allocate some qubits auto q = qalloc(N); oracle.print_kernel(q); int iterations = 1; // Call grover given the oracle and n iterations run_grover(q, oracle, 1); run_grover(q, oracle, iterations); // print the histogram q.print(); // Grover lambda: // amplification lambda auto amplification_lambda = qpu_lambda([](qreg q) { print("hey from amplification_lambda"); H(q); X(q); auto ctrl_bits = q.head(q.size() - 1); auto last_qubit = q.tail(); Z::ctrl(ctrl_bits, last_qubit); X(q); H(q); }); // Capture the grover lambda and iterations directly from the enclosing scope. auto grover_lambda = qpu_lambda([](qreg q) { H(q); for (int i = 0; i < iterations; i++) { oracle(q); amplification_lambda(q); } Measure(q); }, oracle, iterations, amplification_lambda); auto q_lambda = qalloc(N); std::cout << "Lamda result:\n"; grover_lambda.print_kernel(q_lambda); grover_lambda(q_lambda); q_lambda.print(); // Two bitstrings only. qcor_expect(q_lambda.counts().size() == 2); } No newline at end of file
examples/qpu_lambda/lambda_test.cpp +34 −1 Original line number Diff line number Diff line #include "qcor.hpp" int main(int argc, char** argv) { set_shots(1024); int n = argc; double m = 22; Loading @@ -11,16 +12,48 @@ int main(int argc, char** argv) { H(q[0]); } Measure(q[0]); m++; }, n, m); // By-value capture auto b = qpu_lambda([=](qreg q) { print("[By-val] n was captured, and is ", n); print("[By-val] m was captured, and is ", m); for (int i = 0; i < n; i++) { H(q[0]); } Measure(q[0]); }, n, m); auto q = qalloc(1); a(q); auto qb = qalloc(1); b(qb); print("m after lambda", m); // m was modified by the lambda... qcor_expect(m == 23); q.print(); // Run 1 Hadamard qcor_expect(q.counts().size() == 2); qcor_expect(q.counts()["0"] > 400); qcor_expect(q.counts()["1"] > 400); qcor_expect(q.counts()["0"] + q.counts()["1"] == 1024); n = 2; m = 33.0; auto r = qalloc(1); print("running again to show capture variables are captured by reference"); auto rb = qalloc(1); print("running again to show capture variables are captured by reference and by value"); a(r); b(rb); r.print(); // H - H == I qcor_expect(r.counts()["0"] == 1024); // b still uses n = 1 (capture by value...) rb.print(); qcor_expect(rb.counts().size() == 2); qcor_expect(rb.counts()["0"] > 400); qcor_expect(rb.counts()["1"] > 400); qcor_expect(rb.counts()["0"] + rb.counts()["1"] == 1024); }
examples/qpu_lambda/lambda_test_bell.cpp 0 → 100644 +24 −0 Original line number Diff line number Diff line #include "qcor.hpp" int main(int argc, char** argv) { set_shots(1024); auto x_lambda = qpu_lambda([](qubit q) { X(q); }); auto bell = qpu_lambda([](qreg q) { H(q[0]); // Call the captured lambda x_lambda.ctrl(q[0], q[1]); Measure(q); }, x_lambda); auto q = qalloc(2); bell(q); q.print(); qcor_expect(q.counts().size() == 2); qcor_expect(q.counts()["00"] > 400); qcor_expect(q.counts()["11"] > 400); // Entangled... qcor_expect(q.counts()["00"] + q.counts()["11"] == 1024); }