-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from Strilanc/dev
Final preparations for April release
- Loading branch information
Showing
31 changed files
with
429 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {Gate} from "src/circuit/Gate.js" | ||
import {ketArgs, ketShaderPermute, ketInputGateShaderCode} from "src/circuit/KetShaderUtil.js" | ||
import {Util} from "src/base/Util.js" | ||
import {WglArg} from "src/webgl/WglArg.js" | ||
import {modulusTooBigChecker} from "src/gates/ModularIncrementGates.js" | ||
|
||
let ModularAdditionGates = {}; | ||
|
||
const MODULAR_ADDITION_SHADER = ketShaderPermute( | ||
` | ||
uniform float factor; | ||
${ketInputGateShaderCode('A')} | ||
${ketInputGateShaderCode('R')} | ||
`, | ||
` | ||
float r = read_input_R(); | ||
if (out_id >= r) { | ||
return out_id; | ||
} | ||
float d = read_input_A(); | ||
d *= factor; | ||
d = mod(d, r); | ||
float result = mod(out_id + r - d, r); | ||
// Despite sanity, I consistently get result=33 instead of result=0 when out_id=0, d=0, r=33. | ||
// HACK: Fix it by hand. | ||
if (result >= r) { | ||
result -= r; | ||
} | ||
return result; | ||
`); | ||
|
||
ModularAdditionGates.PlusAModRFamily = Gate.buildFamily(1, 16, (span, builder) => builder. | ||
setSerializedId("+AmodR" + span). | ||
setSymbol("+A\nmod R"). | ||
setTitle("Modular Addition Gate"). | ||
setBlurb("Adds input A into the target, mod input R.\nOnly affects values below R."). | ||
setRequiredContextKeys("Input Range A", "Input Range R"). | ||
setExtraDisableReasonFinder(modulusTooBigChecker("R", span)). | ||
setActualEffectToShaderProvider(ctx => MODULAR_ADDITION_SHADER.withArgs( | ||
...ketArgs(ctx, span, ['A', 'R']), | ||
WglArg.float("factor", +1))). | ||
setKnownEffectToParametrizedPermutation((t, a, b) => t < b ? (t + a) % b : t)); | ||
|
||
ModularAdditionGates.MinusAModRFamily = Gate.buildFamily(1, 16, (span, builder) => builder. | ||
setSerializedId("-AmodR" + span). | ||
setSymbol("−A\nmod R"). | ||
setTitle("Modular Subtraction Gate"). | ||
setBlurb("Subtracts input A out of the target, mod input R.\nOnly affects values below R."). | ||
setRequiredContextKeys("Input Range A", "Input Range R"). | ||
setExtraDisableReasonFinder(modulusTooBigChecker("R", span)). | ||
setActualEffectToShaderProvider(ctx => MODULAR_ADDITION_SHADER.withArgs( | ||
...ketArgs(ctx, span, ['A', 'R']), | ||
WglArg.float("factor", -1))). | ||
setKnownEffectToParametrizedPermutation((t, a, b) => t < b ? Util.properMod(t - a, b) : t)); | ||
|
||
ModularAdditionGates.all = [ | ||
...ModularAdditionGates.PlusAModRFamily.all, | ||
...ModularAdditionGates.MinusAModRFamily.all, | ||
]; | ||
|
||
export {ModularAdditionGates} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {Gate} from "src/circuit/Gate.js" | ||
import {ketArgs, ketShaderPermute, ketInputGateShaderCode} from "src/circuit/KetShaderUtil.js" | ||
import {Util} from "src/base/Util.js" | ||
import {WglArg} from "src/webgl/WglArg.js" | ||
|
||
let ModularIncrementGates = {}; | ||
|
||
/** | ||
* @param {!string} inputKey | ||
* @param {!int} span | ||
* @returns {!function(!GateCheckArgs) : (undefined|!string)} | ||
*/ | ||
let modulusTooBigChecker = (inputKey, span) => args => { | ||
let r = args.context.get('Input Range ' + inputKey); | ||
let d = args.context.get('Input Default ' + inputKey); | ||
if (r !== undefined && r.length > span) { | ||
return "mod\ntoo\nbig"; | ||
} | ||
if (r === undefined && d !== undefined && d > 1<<span) { | ||
return "mod\ntoo\nbig"; | ||
} | ||
return undefined; | ||
}; | ||
|
||
const MODULAR_INCREMENT_SHADER = ketShaderPermute( | ||
` | ||
uniform float amount; | ||
${ketInputGateShaderCode('R')} | ||
`, | ||
` | ||
float r = read_input_R(); | ||
return out_id >= r | ||
? out_id | ||
// HACK: sometimes mod(value-equal-to-r, r) returns r instead of 0. The perturbation works around it. | ||
: floor(mod(out_id + r - amount, r - 0.000001));`); | ||
|
||
ModularIncrementGates.IncrementModRFamily = Gate.buildFamily(1, 16, (span, builder) => builder. | ||
setSerializedId("incmodR" + span). | ||
setSymbol("+1\nmod R"). | ||
setTitle("Modular Increment Gate"). | ||
setBlurb("Adds 1 into the target, but wraps R-1 to 0.\n" + | ||
"Only affects values less than R."). | ||
setRequiredContextKeys("Input Range R"). | ||
setExtraDisableReasonFinder(modulusTooBigChecker("R", span)). | ||
setActualEffectToShaderProvider(ctx => MODULAR_INCREMENT_SHADER.withArgs( | ||
...ketArgs(ctx, span, ['R']), | ||
WglArg.float("amount", +1))). | ||
setKnownEffectToParametrizedPermutation((t, a) => t < a ? (t + 1) % a : t)); | ||
|
||
ModularIncrementGates.DecrementModRFamily = Gate.buildFamily(1, 16, (span, builder) => builder. | ||
setSerializedId("decmodR" + span). | ||
setSymbol("−1\nmod R"). | ||
setTitle("Modular Decrement Gate"). | ||
setBlurb("Subtracts 1 out of the target, but wraps 0 to R-1.\n" + | ||
"Only affects values less than R."). | ||
setRequiredContextKeys("Input Range R"). | ||
setExtraDisableReasonFinder(modulusTooBigChecker("R", span)). | ||
setActualEffectToShaderProvider(ctx => MODULAR_INCREMENT_SHADER.withArgs( | ||
...ketArgs(ctx, span, ['R']), | ||
WglArg.float("amount", -1))). | ||
setKnownEffectToParametrizedPermutation((t, a) => t < a ? Util.properMod(t - 1, a) : t)); | ||
|
||
ModularIncrementGates.all = [ | ||
...ModularIncrementGates.IncrementModRFamily.all, | ||
...ModularIncrementGates.DecrementModRFamily.all, | ||
]; | ||
|
||
export {ModularIncrementGates, modulusTooBigChecker} |
Oops, something went wrong.