Skip to content

Commit

Permalink
More mod-rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Jun 8, 2018
1 parent 4f7e96f commit 57ce05d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/gates/ModularMultiplicationGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
`;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gates/ModularMultiplyAccumulateGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gates/MultiplyAccumulateGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/gates/AmplitudeDisplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit 57ce05d

Please sign in to comment.