diff --git a/src/gates/ModularMultiplicationGates.js b/src/gates/ModularMultiplicationGates.js index 239aeacc..8e40a068 100644 --- a/src/gates/ModularMultiplicationGates.js +++ b/src/gates/ModularMultiplicationGates.js @@ -50,7 +50,7 @@ const MODULAR_INVERSE_SHADER_CODE = ` if (r.y != 1.0) { return -1.0; } - return mod(mod(s.y, modulus) + modulus, modulus); + return floor(mod(floor(mod(s.y + 0.5, modulus)) + modulus + 0.5, modulus)); } `; @@ -70,7 +70,7 @@ const POW_MOD_SHADER_CODE = ` float f = 1.0; for (int k = 0; k < ${Config.MAX_WIRE_COUNT}; k++) { - if (mod(exponent, 2.0) == 1.0) { + if (floor(mod(exponent + 0.5, 2.0)) == 1.0) { exponent -= 1.0; f = big_mul_mod(f, base, modulus); } @@ -169,7 +169,7 @@ const MODULAR_MULTIPLICATION_SHADER = ketShaderPermute( ` float input_a = read_input_A(); float modulus = read_input_R(); - input_a = mod(input_a, modulus); + input_a = floor(mod(input_a + 0.5, modulus)); float v = modular_multiplicative_inverse(input_a, modulus); if (v == -1.0 || out_id >= modulus) { return out_id; @@ -186,7 +186,7 @@ const MODULAR_INVERSE_MULTIPLICATION_SHADER = ketShaderPermute( ` float input_a = read_input_A(); float modulus = read_input_R(); - input_a = mod(input_a, modulus); + input_a = floor(mod(input_a + 0.5, modulus)); if (modular_multiplicative_inverse(input_a, modulus) == -1.0 || out_id >= modulus) { return out_id; } diff --git a/src/gates/ModularMultiplyAccumulateGates.js b/src/gates/ModularMultiplyAccumulateGates.js index 9ea58fc6..0f2b36d8 100644 --- a/src/gates/ModularMultiplyAccumulateGates.js +++ b/src/gates/ModularMultiplyAccumulateGates.js @@ -36,7 +36,7 @@ const MODULAR_MULTIPLY_ACCUMULATE_SHADER = ketShaderPermute( float d = big_mul_mod(factor * a, b, r); - float in_id = mod(out_id - d, r); + float in_id = floor(mod(out_id - d + 0.5, r)); if (in_id < 0.0) { in_id += r; } diff --git a/src/gates/MultiplyAccumulateGates.js b/src/gates/MultiplyAccumulateGates.js index e5f7f2d0..5bb9c486 100644 --- a/src/gates/MultiplyAccumulateGates.js +++ b/src/gates/MultiplyAccumulateGates.js @@ -64,7 +64,7 @@ const MULTIPLY_ACCUMULATE_SHADER = ketShaderPermute( ` float d1 = read_input_A(); float d2 = read_input_B(); - float d = mod(big_mul_mod(d1, d2, span)*factor, span); + float d = floor(mod(big_mul_mod(d1, d2, span)*factor + 0.5, span)); return mod(out_id + span - d, span);`); MultiplyAccumulateGates.Legacy_MultiplyAddFamily = Gate.buildFamily(3, 16, (span, builder) => builder. diff --git a/test/gates/AmplitudeDisplay.test.js b/test/gates/AmplitudeDisplay.test.js index ca597e83..c5b403e3 100644 --- a/test/gates/AmplitudeDisplay.test.js +++ b/test/gates/AmplitudeDisplay.test.js @@ -44,7 +44,7 @@ suite.testUsingWebGL("amplitudesToPolarKets", () => { 25,new Complex(3,4).phase(),25,0, 2,Math.PI*3/4,2,0, 0.25,Math.PI/2,0.25,0 - ]), 0.0001); + ]), 0.001); input.deallocByDepositingInPool(); });