|
@@ -4,10 +4,10 @@
|
|
|
* Returns the nth number where a given digit
|
|
|
* is cleared in the binary representation of the number
|
|
|
*/
|
|
|
-static uint nth_cleared(uint n, uint target)
|
|
|
+static int nth_cleared(int n, int target)
|
|
|
{
|
|
|
- uint mask = (1 << target) - 1;
|
|
|
- uint not_mask = ~mask;
|
|
|
+ int mask = (1 << target) - 1;
|
|
|
+ int not_mask = ~mask;
|
|
|
|
|
|
return (n & mask) | ((n & not_mask) << 1);
|
|
|
}
|
|
@@ -25,18 +25,18 @@ static uint nth_cleared(uint n, uint target)
|
|
|
*/
|
|
|
__kernel void apply_gate(
|
|
|
__global cfloat_t *amplitudes,
|
|
|
- uint target,
|
|
|
+ int target,
|
|
|
cfloat_t A,
|
|
|
cfloat_t B,
|
|
|
cfloat_t C,
|
|
|
cfloat_t D)
|
|
|
{
|
|
|
- uint const global_id = get_global_id(0);
|
|
|
+ int const global_id = get_global_id(0);
|
|
|
|
|
|
- uint const zero_state = nth_cleared(global_id, target);
|
|
|
+ int const zero_state = nth_cleared(global_id, target);
|
|
|
|
|
|
- // uint const zero_state = state & (~(1 << target)); // Could just be state
|
|
|
- uint const one_state = zero_state | (1 << target);
|
|
|
+ // int const zero_state = state & (~(1 << target)); // Could just be state
|
|
|
+ int const one_state = zero_state | (1 << target);
|
|
|
|
|
|
cfloat_t const zero_amp = amplitudes[zero_state];
|
|
|
cfloat_t const one_amp = amplitudes[one_state];
|
|
@@ -50,19 +50,19 @@ __kernel void apply_gate(
|
|
|
*/
|
|
|
__kernel void apply_controlled_gate(
|
|
|
__global cfloat_t *amplitudes,
|
|
|
- uint control,
|
|
|
- uint target,
|
|
|
+ int control,
|
|
|
+ int target,
|
|
|
cfloat_t A,
|
|
|
cfloat_t B,
|
|
|
cfloat_t C,
|
|
|
cfloat_t D)
|
|
|
{
|
|
|
- uint const global_id = get_global_id(0);
|
|
|
- uint const zero_state = nth_cleared(global_id, target);
|
|
|
- uint const one_state = zero_state | (1 << target); // Set the target bit
|
|
|
+ int const global_id = get_global_id(0);
|
|
|
+ int const zero_state = nth_cleared(global_id, target);
|
|
|
+ int const one_state = zero_state | (1 << target); // Set the target bit
|
|
|
|
|
|
- uint const control_val_zero = (((1 << control) & zero_state) > 0) ? 1 : 0;
|
|
|
- uint const control_val_one = (((1 << control) & one_state) > 0) ? 1 : 0;
|
|
|
+ int const control_val_zero = (((1 << control) & zero_state) > 0) ? 1 : 0;
|
|
|
+ int const control_val_one = (((1 << control) & one_state) > 0) ? 1 : 0;
|
|
|
|
|
|
cfloat_t const zero_amp = amplitudes[zero_state];
|
|
|
cfloat_t const one_amp = amplitudes[one_state];
|
|
@@ -87,23 +87,23 @@ __kernel void apply_controlled_gate(
|
|
|
__kernel void apply_controlled_controlled_gate(
|
|
|
__global cfloat_t *const amplitudes,
|
|
|
__global cfloat_t *amps,
|
|
|
- uint control1,
|
|
|
- uint control2,
|
|
|
- uint target,
|
|
|
+ int control1,
|
|
|
+ int control2,
|
|
|
+ int target,
|
|
|
cfloat_t A,
|
|
|
cfloat_t B,
|
|
|
cfloat_t C,
|
|
|
cfloat_t D)
|
|
|
{
|
|
|
- uint const state = get_global_id(0);
|
|
|
+ int const state = get_global_id(0);
|
|
|
cfloat_t const amp = amplitudes[state];
|
|
|
|
|
|
- uint const zero_state = state & (~(1 << target));
|
|
|
- uint const one_state = state | (1 << target);
|
|
|
+ int const zero_state = state & (~(1 << target));
|
|
|
+ int const one_state = state | (1 << target);
|
|
|
|
|
|
- uint const bit_val = (((1 << target) & state) > 0) ? 1 : 0;
|
|
|
- uint const control1_val = (((1 << control1) & state) > 0) ? 1 : 0;
|
|
|
- uint const control2_val = (((1 << control2) & state) > 0) ? 1 : 0;
|
|
|
+ int const bit_val = (((1 << target) & state) > 0) ? 1 : 0;
|
|
|
+ int const control1_val = (((1 << control1) & state) > 0) ? 1 : 0;
|
|
|
+ int const control2_val = (((1 << control2) & state) > 0) ? 1 : 0;
|
|
|
|
|
|
if (control1_val == 0 || control2_val == 0)
|
|
|
{
|
|
@@ -132,18 +132,18 @@ __kernel void apply_controlled_controlled_gate(
|
|
|
// __kernel void swap(
|
|
|
// __global cfloat_t *const amplitudes,
|
|
|
// __global cfloat_t *amps,
|
|
|
-// uint first_qubit,
|
|
|
-// uint second_qubit)
|
|
|
+// int first_qubit,
|
|
|
+// int second_qubit)
|
|
|
// {
|
|
|
-// uint const state = get_global_id(0);
|
|
|
+// int const state = get_global_id(0);
|
|
|
|
|
|
-// uint const first_bit_mask = 1 << first_qubit;
|
|
|
-// uint const second_bit_mask = 1 << second_qubit;
|
|
|
+// int const first_bit_mask = 1 << first_qubit;
|
|
|
+// int const second_bit_mask = 1 << second_qubit;
|
|
|
|
|
|
-// uint const new_second_bit = ((state & first_bit_mask) >> first_qubit) << second_qubit;
|
|
|
-// uint const new_first_bit = ((state & second_bit_mask) >> second_qubit) << first_qubit;
|
|
|
+// int const new_second_bit = ((state & first_bit_mask) >> first_qubit) << second_qubit;
|
|
|
+// int const new_first_bit = ((state & second_bit_mask) >> second_qubit) << first_qubit;
|
|
|
|
|
|
-// uint const new_state = (state & !first_bit_mask & !second_bit_mask) | new_first_bit | new_second_bit;
|
|
|
+// int const new_state = (state & !first_bit_mask & !second_bit_mask) | new_first_bit | new_second_bit;
|
|
|
|
|
|
// amps[new_state] = amplitudes[state];
|
|
|
// }
|
|
@@ -156,7 +156,7 @@ __kernel void calculate_probabilities(
|
|
|
__global cfloat_t *const amplitudes,
|
|
|
__global float *probabilities)
|
|
|
{
|
|
|
- uint const state = get_global_id(0);
|
|
|
+ int const state = get_global_id(0);
|
|
|
cfloat_t amp = amplitudes[state];
|
|
|
|
|
|
probabilities[state] = cfloat_abs(cfloat_mul(amp, amp));
|
|
@@ -168,9 +168,9 @@ __kernel void calculate_probabilities(
|
|
|
*/
|
|
|
__kernel void initialize_register(
|
|
|
__global cfloat_t *amplitudes,
|
|
|
- uint const target)
|
|
|
+ int const target)
|
|
|
{
|
|
|
- uint const state = get_global_id(0);
|
|
|
+ int const state = get_global_id(0);
|
|
|
if (state == target)
|
|
|
{
|
|
|
amplitudes[state] = cfloat_new(1, 0);
|