|
| 1 | +/* |
| 2 | +Comparison of Runtime Performance of TurnOnSpecialize and TurnOnSpecializeV2. |
| 3 | +A single node system with one or more threads is assumed. |
| 4 | +Thread count should be set by using export OMP_NUM_THREADS=<thread_count>. |
| 5 | +*/ |
| 6 | +// TODO: Add documentation |
| 7 | + |
| 8 | +#include <array> |
| 9 | +#include <chrono> |
| 10 | +#include <vector> |
| 11 | +#include "../include/qureg.hpp" |
| 12 | + |
| 13 | +template <typename Function> |
| 14 | +void benchmark(const std::vector<std::array<int, 2>> &pairs, const char *name, Function func) |
| 15 | +{ |
| 16 | + //See https://stackoverflow.com/questions/11062804/measuring-the-runtime-of-a-c-code |
| 17 | + auto start = std::chrono::steady_clock::now(); |
| 18 | + for (const auto &p : pairs) |
| 19 | + func(p[0], p[1]); |
| 20 | + auto end = std::chrono::steady_clock::now(); |
| 21 | + auto elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(end - start); |
| 22 | + std::cout << name << ": " << elapsed.count() << " seconds\n"; |
| 23 | +} |
| 24 | + |
| 25 | +int main(int argc, char *argv[]) |
| 26 | +{ |
| 27 | + qhipster::mpi::Environment::Init(argc, argv); |
| 28 | + |
| 29 | + if (argc != 2 && argc != 3) |
| 30 | + { |
| 31 | + std::cout << "Usage: " << argv[0] << " <num_qubits> [<do_comparison> = 0]\n"; |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + int nbits = atoi(argv[1]); |
| 36 | + std::cout << "Num Qubits: " << nbits << "\n"; |
| 37 | + |
| 38 | + bool compare_states = false; |
| 39 | + if (argc == 3) |
| 40 | + compare_states = static_cast<bool>(atoi(argv[2])); |
| 41 | + |
| 42 | + if (compare_states) // Compare only for small numbers |
| 43 | + std::cout << "State comparison will be performed" << std::endl; |
| 44 | + else |
| 45 | + std::cout << "State comparison will not be performed" << std::endl; |
| 46 | + |
| 47 | + // Comparison variables |
| 48 | + QubitRegister<ComplexDP>* psi0 = nullptr; |
| 49 | + const double tol = 1e-9; |
| 50 | + |
| 51 | + std::vector<std::array<int, 2>> pairs; |
| 52 | + for (int i = 0; i < nbits; ++i) |
| 53 | + pairs.push_back({i, (i + 1) % nbits}); |
| 54 | + |
| 55 | + for (int i = 0; i < 3; ++i) |
| 56 | + { |
| 57 | + QubitRegister<ComplexDP> psi(nbits, "base", 0); |
| 58 | + if (i == 1) |
| 59 | + { |
| 60 | + psi.TurnOnSpecialize(); |
| 61 | + std::cout << "\nBenchmarking with spec v1\n------------\n"; |
| 62 | + } |
| 63 | + else if (i == 2) |
| 64 | + { |
| 65 | + std::cout << "\nBenchmarking with spec v2\n------------\n"; |
| 66 | + psi.TurnOnSpecializeV2(); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + std::cout << "\nBenchmarking without spec\n------------\n"; |
| 71 | + } |
| 72 | + // More gates can be added in the similar fashion |
| 73 | + // Covers all spec2 gates as of August 21, 2020 |
| 74 | + benchmark(pairs, "H ", [&psi](int i, int j) { psi.ApplyHadamard(i); }); |
| 75 | + benchmark(pairs, "T ", [&psi](int i, int j) { psi.ApplyT(i); }); |
| 76 | + benchmark(pairs, "RX ", [&psi](int i, int j) { psi.ApplyRotationX(i, M_PI / 3); }); |
| 77 | + benchmark(pairs, "RY ", [&psi](int i, int j) { psi.ApplyRotationY(i, M_PI / 6); }); |
| 78 | + benchmark(pairs, "RZ ", [&psi](int i, int j) { psi.ApplyRotationZ(i, M_PI / 4); }); |
| 79 | + benchmark(pairs, "X ", [&psi](int i, int j) { psi.ApplyPauliX(i); }); |
| 80 | + benchmark(pairs, "Y ", [&psi](int i, int j) { psi.ApplyPauliY(i); }); |
| 81 | + benchmark(pairs, "Z ", [&psi](int i, int j) { psi.ApplyPauliZ(i); }); |
| 82 | + |
| 83 | + benchmark(pairs, "CH ", [&psi](int i, int j) { psi.ApplyCHadamard(i, j); }); |
| 84 | + benchmark(pairs, "CRX", [&psi](int i, int j) { psi.ApplyCRotationX(i, j, M_PI / 3); }); |
| 85 | + benchmark(pairs, "CRY", [&psi](int i, int j) { psi.ApplyCRotationY(i, j, M_PI / 6); }); |
| 86 | + benchmark(pairs, "CRZ", [&psi](int i, int j) { psi.ApplyCRotationZ(i, j, M_PI / 4); }); |
| 87 | + benchmark(pairs, "CX ", [&psi](int i, int j) { psi.ApplyCPauliX(i, j); }); |
| 88 | + benchmark(pairs, "CY ", [&psi](int i, int j) { psi.ApplyCPauliY(i, j); }); |
| 89 | + benchmark(pairs, "CZ ", [&psi](int i, int j) { psi.ApplyCPauliZ(i, j); }); |
| 90 | + benchmark(pairs, "CPh", [&psi](int i, int j) { psi.ApplyCPhaseRotation(i, j, M_PI / 4); }); |
| 91 | + |
| 92 | + if (compare_states) |
| 93 | + { |
| 94 | + if (i == 0) |
| 95 | + { |
| 96 | + psi0 = new QubitRegister<ComplexDP>(psi); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + auto diff = psi0->MaxAbsDiff(psi, {1, 0}); |
| 101 | + if (diff < tol) |
| 102 | + { |
| 103 | + std::cout << "State comparison test passed for spec " << i << std::endl; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + std::cerr << "State comparison test failed for spec " << i |
| 108 | + << "\nMax diff: " << diff << "\nAborting..." << std::endl; |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (compare_states) |
| 115 | + delete psi0; |
| 116 | + |
| 117 | + qhipster::mpi::Environment::Finalize(); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments