Skip to content

Commit

Permalink
Refactored ketShaderPhase to use a shader-method returning an angle
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Sep 19, 2017
1 parent 54599d1 commit ad56641
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
27 changes: 19 additions & 8 deletions src/circuit/KetShaderUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,31 @@ const ketShaderPermute = (head, body, span=null) => ketShader(
span);

/**
* @param {!String} head
* @param {!String} body
* @param {null|!int=null} span
* Returns a shader that multiplies each of the amplitudes in a superposition by computed phase factors.
*
* @param {!String} head Header code defining shader methods, uniforms, etc.
* @param {!String} body The body of a shader method returning the number of radians to phase by.
* @param {null|!int=null} span The number of qubits this operation applies to, if known ahead of time.
* @return {!{withArgs: !function(args: ...!WglArg) : !WglConfiguredShader}}
*/
const ketShaderPhase = (head, body, span=null) => ketShader(
head + `vec2 _ketgen_phase_for(float out_id) { ${body} }`,
'return cmul(amp, _ketgen_phase_for(out_id));',
`${head}
float _ketgen_phase_for(float out_id) {
${body}
}
`,
`
float angle = _ketgen_phase_for(out_id);
return cmul(amp, vec2(cos(angle), sin(angle)));
`,
span);

/**
* @param {!CircuitEvalContext} ctx
* @param {undefined|!int=undefined} span
* @param {undefined|!Array.<!string>} input_letters
* Determines some arguments to give to a shader produced by one of the ketShader methods.
*
* @param {!CircuitEvalContext} ctx The context in which the ket shader is being applied.
* @param {undefined|!int=undefined} span The number of qubits this shader applies to (if wasn't known ahead of time).
* @param {undefined|!Array.<!string>} input_letters The input gates that this shader cares about.
* @returns {!Array.<!WglArg>}
*/
function ketArgs(ctx, span=undefined, input_letters=[]) {
Expand Down
3 changes: 1 addition & 2 deletions src/gates/FourierTransformGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const CONTROLLED_PHASE_GRADIENT_SHADER = ketShaderPhase(
`
float hold = floor(out_id * 2.0 / span);
float step = mod(out_id, span / 2.0);
float angle = hold * step * factor * 6.2831853071795864769 / span;
return vec2(cos(angle), sin(angle));
return hold * step * factor * 6.2831853071795864769 / span;
`);

const FOURIER_TRANSFORM_MATRIX_MAKER = span =>
Expand Down
3 changes: 1 addition & 2 deletions src/gates/ParametrizedRotationGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const Z_TO_A_SHADER = ketShaderPhase(
${ketInputGateShaderCode('A')}
`,
`
float angle = read_input_A() * out_id * factor / _gen_input_span_A;
return vec2(cos(angle), sin(angle));
return read_input_A() * out_id * factor / _gen_input_span_A;
`);

ParametrizedRotationGates.XToA = new GateBuilder().
Expand Down
3 changes: 1 addition & 2 deletions src/gates/PhaseGradientGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const PHASE_GRADIENT_SHADER = ketShaderPhase(
}
`,
`
float angle = angle_mul(factor, out_id);
return vec2(cos(angle), sin(angle));
return angle_mul(factor, out_id);
`);

let PhaseGradientGates = {};
Expand Down
2 changes: 1 addition & 1 deletion test/circuit/KetShaderUtil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ suite.testUsingWebGL("ketShaderPermute", () => {
suite.testUsingWebGL("ketShaderPhase", () => {
let shader = ketShaderPhase(
'',
'return vec2(cos(out_id/10.0), sin(out_id/10.0));',
'return out_id/10.0;',
3);
assertThatCircuitShaderActsLikeMatrix(
ctx => shader.withArgs(...ketArgs(ctx)),
Expand Down

0 comments on commit ad56641

Please sign in to comment.