example.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import print_function
  2. import qcgpu
  3. s = qcgpu.State(3)
  4. h = qcgpu.gate.h()
  5. print(h)
  6. s.apply_gate(h, 0)
  7. print(s)
  8. # from __future__ import absolute_import
  9. # from __future__ import print_function
  10. # import pyopencl as cl
  11. # import pyopencl.array as cl_array
  12. # import numpy
  13. # import numpy.linalg as la
  14. # a = numpy.random.rand(8).astype(numpy.complex64)
  15. # ctx = cl.create_some_context()
  16. # queue = cl.CommandQueue(ctx)
  17. # a_dev = cl_array.to_device(queue, a)
  18. # prg = cl.Program(ctx, """
  19. # #include <pyopencl-complex.h>
  20. # /*
  21. # * Returns the nth number where a given digit
  22. # * is cleared in the binary representation of the number
  23. # */
  24. # static int nth_cleared(int n, int target)
  25. # {
  26. # int mask = (1 << target) - 1;
  27. # int not_mask = ~mask;
  28. # return (n & mask) | ((n & not_mask) << 1);
  29. # }
  30. # /*
  31. # * Applies a single qubit gate to the register.
  32. # * The gate matrix must be given in the form:
  33. # *
  34. # * A B
  35. # * C D
  36. # */
  37. # __kernel void apply_gate(
  38. # __global cfloat_t *amplitudes,
  39. # int target,
  40. # cfloat_t A,
  41. # cfloat_t B,
  42. # cfloat_t C,
  43. # cfloat_t D)
  44. # {
  45. # int const global_id = get_global_id(0);
  46. # int const zero_state = nth_cleared(global_id, target);
  47. # // int const zero_state = state & (~(1 << target)); // Could just be state
  48. # int const one_state = zero_state | (1 << target);
  49. # cfloat_t const zero_amp = amplitudes[zero_state];
  50. # cfloat_t const one_amp = amplitudes[one_state];
  51. # amplitudes[zero_state] = cfloat_add(cfloat_mul(A, zero_amp), cfloat_mul(B, one_amp));
  52. # amplitudes[one_state] = cfloat_add(cfloat_mul(D, one_amp), cfloat_mul(C, zero_amp));
  53. # }
  54. # __kernel void sum(__global cfloat_t *a, cfloat_t b)
  55. # {
  56. # int gid = get_global_id(0);
  57. # a[gid] = cfloat_add(a[gid], b);
  58. # }
  59. # """).build()
  60. # # kernel = prg.sum
  61. # # kernel.set_scalar_arg_dtypes([
  62. # # None,
  63. # # None,
  64. # # numpy.complex64
  65. # # ])
  66. # prg.sum(queue, (int(a.shape[0] / 2),), None, a_dev.data, numpy.complex64(3+2j))
  67. # prg.sum(queue, (2, ), None, a_dev.data, numpy.complex64(3+2j))
  68. # print(a_dev)
  69. # print((a.shape[0] / 2,))