Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <time.h>
  4. #include <complex>
  5.  
  6. void matmulpnoif(std::complex<float> arr[], std::complex<float> out[], int numqbits, std::complex<float> a,
  7. std::complex<float> b, std::complex<float> c, std::complex<float> d, int target)
  8. {
  9. long length = 1 << (numqbits);
  10. long offset = 1 << (target - 1);
  11. long state = 0;
  12. while (state < length)
  13. {
  14. out[state] = arr[state] * a + arr[state + offset] * b;
  15. out[state + offset] = arr[state] * c + arr[state + offset] * d;
  16. state += 1 + offset * (((state%offset) + 1) / offset);
  17. }
  18. }
  19.  
  20. void matmulpsingle(std::complex<float> arr[], std::complex<float> out[], int numqbits, std::complex<float> a,
  21. std::complex<float> b, std::complex<float> c, std::complex<float> d, int target)
  22. {
  23. long length = 1 << (numqbits);
  24. int shift = target - 1;
  25. long offset = 1 << shift;
  26. for (long state = 0; state < length; ++state)
  27. {
  28. if ((state >> shift) & 1)
  29. {
  30. out[state] = arr[state - offset] * c + arr[state] * d;
  31. }
  32. else
  33. {
  34. out[state] = arr[state] * a + arr[state + offset] * b;
  35. }
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. using namespace std;
  42. int numqbits = 25;
  43. long arraylength = 1 << numqbits;
  44. complex<float>* amplitudes = new complex<float>[arraylength];
  45. for (long i = 0; i < arraylength; ++i)
  46. {
  47. amplitudes[i] = complex<float>(0., 0.);
  48. }
  49. amplitudes[0] = complex<float>(1., 0.);
  50. complex<float> a(0., 0.);
  51. complex<float> b(1., 0.);
  52. complex<float> c(0., 0.);
  53. complex<float> d(1., 0.);
  54. int target = 1;
  55. int repititions = 10;
  56. clock_t startTime;
  57. //while (target <= numqbits) {
  58. startTime = clock();
  59. for (int j = 0; j < repititions; ++j) {
  60. complex<float>* outputs = new complex<float>[arraylength];
  61. matmulpsingle(amplitudes, outputs, numqbits, a, b, c, d, target);
  62. delete[] outputs;
  63. }
  64. cout << float(clock() - startTime) / (float)(CLOCKS_PER_SEC*repititions) << " seconds." << endl;
  65. startTime = clock();
  66. for (int k = 0; k < repititions; ++k) {
  67. complex<float>* outputs = new complex<float>[arraylength];
  68. matmulpnoif(amplitudes, outputs, numqbits, a, b, c, d, target);
  69. delete[] outputs;
  70. }
  71. cout << float(clock() - startTime) / (float)(CLOCKS_PER_SEC*repititions) << " seconds." << endl;
  72. target+=1;
  73. //}
  74. delete[] amplitudes;
  75. return 0;
  76. }
Add Comment
Please, Sign In to add comment