Skip to content

Commit

Permalink
Renamed CircuitEvalArgs to CircuitEvalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Oct 16, 2016
1 parent bde0e0a commit 10a283e
Show file tree
Hide file tree
Showing 36 changed files with 258 additions and 248 deletions.
88 changes: 44 additions & 44 deletions src/circuit/CircuitComputeUtil.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CircuitEvalArgs} from "src/circuit/CircuitEvalArgs.js"
import {CircuitEvalContext} from "src/circuit/CircuitEvalContext.js"
import {CircuitShaders} from "src/circuit/CircuitShaders.js"
import {KetTextureUtil} from "src/circuit/KetTextureUtil.js"
import {Controls} from "src/circuit/Controls.js"
Expand All @@ -22,9 +22,9 @@ function circuitDefinitionToGate(circuitDefinition, symbol="", name="", blurb=""
return Gate.withoutKnownMatrix(symbol, name, blurb).
withKnownCircuit(circuitDefinition).
withStableDuration(circuitDefinition.stableDuration()).
withCustomOperation(args => advanceStateWithCircuit(
args,
circuitDefinition.withDisabledReasonsForEmbeddedContext(args.row, args.customContextFromGates),
withCustomOperation(ctx => advanceStateWithCircuit(
ctx,
circuitDefinition.withDisabledReasonsForEmbeddedContext(ctx.row, ctx.customContextFromGates),
false)).
withHeight(circuitDefinition.numWires).
withCustomDisableReasonFinder(args => {
Expand All @@ -45,12 +45,12 @@ function circuitDefinitionToGate(circuitDefinition, symbol="", name="", blurb=""
}

/**
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @param {!CircuitDefinition} circuitDefinition
* @param {!boolean} collectStats
* @returns {!{output:!WglTexture, colQubitDensities:!Array.<!WglTexture>,customStats:!Array, customStatsMap:!Array}}
*/
function advanceStateWithCircuit(args, circuitDefinition, collectStats) {
function advanceStateWithCircuit(ctx, circuitDefinition, collectStats) {
// Prep stats collection.
let colQubitDensities = [];
let customStats = [];
Expand All @@ -76,7 +76,7 @@ function advanceStateWithCircuit(args, circuitDefinition, collectStats) {
// Apply each column in the circuit.
for (let col = 0; col < circuitDefinition.columns.length; col++) {
_advanceStateWithCircuitDefinitionColumn(
args,
ctx,
circuitDefinition,
col,
statsCallback(col));
Expand All @@ -85,98 +85,98 @@ function advanceStateWithCircuit(args, circuitDefinition, collectStats) {
if (collectStats) {
const allWiresMask = (1 << circuitDefinition.numWires) - 1;
colQubitDensities.push(KetTextureUtil.superpositionToQubitDensities(
args.stateTrader.currentTexture, Controls.NONE, allWiresMask));
ctx.stateTrader.currentTexture, Controls.NONE, allWiresMask));
}

return {
output: args.stateTrader.currentTexture,
output: ctx.stateTrader.currentTexture,
colQubitDensities,
customStats,
customStatsMap
};
}

/**
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @param {!CircuitDefinition} circuitDefinition
* @param {!int} col
* @private
* @returns {!{qubitDensities:!WglTexture, customGateStats:!Array.<!{row:!int,stat:!WglTexture}>}}
*/
function _extractStateStatsNeededByCircuitColumn(
args,
ctx,
circuitDefinition,
col) {
// Compute custom stats used by display gates.
let customGateStats = [];
for (let row of circuitDefinition.customStatRowsInCol(col)) {
let statArgs = new CircuitEvalArgs(
args.time,
let statCtx = new CircuitEvalContext(
ctx.time,
row,
circuitDefinition.numWires,
args.controls,
args.controlsTexture,
args.stateTrader,
ctx.controls,
ctx.controlsTexture,
ctx.stateTrader,
circuitDefinition.colCustomContextFromGates(col, row));
let stat = circuitDefinition.columns[col].gates[row].customStatTexturesMaker(statArgs);
let stat = circuitDefinition.columns[col].gates[row].customStatTexturesMaker(statCtx);
customGateStats.push({row, stat});
}

// Compute individual qubit densities, where needed.
let qubitDensities = KetTextureUtil.superpositionToQubitDensities(
args.stateTrader.currentTexture,
args.controls,
ctx.stateTrader.currentTexture,
ctx.controls,
circuitDefinition.colHasSingleQubitDisplayMask(col));

return {qubitDensities, customGateStats};
}

/**
* Advances the state trader inside of the given CircuitEvalArgs.
* Advances the state trader inside of the given CircuitEvalContext.
*
* @param {!CircuitEvalArgs} args Evaluation arguments, including the row this column starts at (for when the circuit
* we're applying is actually a gate embedded inside an outer circuit).
* @param {!CircuitEvalContext} ctx Evaluation arguments, including the row this column starts at (for when the circuit
* we're applying is actually a gate embedded inside an outer circuit).
* @param {!CircuitDefinition} circuitDefinition
* @param {!int} col
* @param {!function(!CircuitEvalArgs)} statsCallback
* @param {!function(!CircuitEvalContext)} statsCallback
* @returns {void}
* @private
*/
function _advanceStateWithCircuitDefinitionColumn(
args,
ctx,
circuitDefinition,
col,
statsCallback) {

let controls = args.controls.and(circuitDefinition.colControls(col).shift(args.row));
let controlTex = CircuitShaders.controlMask(controls).toBoolTexture(args.wireCount);
let controls = ctx.controls.and(circuitDefinition.colControls(col).shift(ctx.row));
let controlTex = CircuitShaders.controlMask(controls).toBoolTexture(ctx.wireCount);

let colContext = Util.mergeMaps(
args.customContextFromGates,
circuitDefinition.colCustomContextFromGates(col, args.row));

let trader = args.stateTrader;
let aroundArgs = new CircuitEvalArgs(
args.time,
args.row,
args.wireCount,
args.controls,
args.controlsTexture,
ctx.customContextFromGates,
circuitDefinition.colCustomContextFromGates(col, ctx.row));

let trader = ctx.stateTrader;
let aroundCtx = new CircuitEvalContext(
ctx.time,
ctx.row,
ctx.wireCount,
ctx.controls,
ctx.controlsTexture,
trader,
colContext);
let mainArgs = new CircuitEvalArgs(
args.time,
args.row,
args.wireCount,
let mainCtx = new CircuitEvalContext(
ctx.time,
ctx.row,
ctx.wireCount,
controls,
controlTex,
trader,
colContext);

circuitDefinition.applyBeforeOperationsInCol(col, aroundArgs);
circuitDefinition.applyMainOperationsInCol(col, mainArgs);
statsCallback(mainArgs);
circuitDefinition.applyAfterOperationsInCol(col, aroundArgs);
circuitDefinition.applyBeforeOperationsInCol(col, aroundCtx);
circuitDefinition.applyMainOperationsInCol(col, mainCtx);
statsCallback(mainCtx);
circuitDefinition.applyAfterOperationsInCol(col, aroundCtx);

controlTex.deallocByDepositingInPool("controlTex in _advanceStateWithCircuitDefinitionColumn");
}
Expand Down
32 changes: 16 additions & 16 deletions src/circuit/CircuitDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,15 +756,15 @@ class CircuitDefinition {

/**
* @param {!int} colIndex
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @return {void}
*/
applyMainOperationsInCol(colIndex, args) {
applyMainOperationsInCol(colIndex, ctx) {
if (colIndex < 0 || colIndex >= this.columns.length) {
return;
}

this._applyOpsInCol(colIndex, args, gate => {
this._applyOpsInCol(colIndex, ctx, gate => {
if (gate.definitelyHasNoEffect() || gate === Gates.Special.SwapHalf) {
return undefined;
}
Expand All @@ -773,41 +773,41 @@ class CircuitDefinition {
return gate.customOperation;
}

return args => GateShaders.applyMatrixOperation(args, gate.knownMatrixAt(args.time));
return ctx => GateShaders.applyMatrixOperation(ctx, gate.knownMatrixAt(ctx.time));
});

for (let [i, j] of this.columns[colIndex].swapPairs()) {
//noinspection JSUnusedAssignment
args.stateTrader.shadeAndTrade(
CircuitShaders.swap(args.withRow(i + args.row), j + args.row));
ctx.stateTrader.shadeAndTrade(
CircuitShaders.swap(ctx.withRow(i + ctx.row), j + ctx.row));
}
}

/**
* @param {!int} colIndex
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @return {void}
*/
applyBeforeOperationsInCol(colIndex, args) {
this._applyOpsInCol(colIndex, args, g => g.customBeforeOperation);
applyBeforeOperationsInCol(colIndex, ctx) {
this._applyOpsInCol(colIndex, ctx, g => g.customBeforeOperation);
}

/**
* @param {!int} colIndex
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @return {void}
*/
applyAfterOperationsInCol(colIndex, args) {
this._applyOpsInCol(colIndex, args, g => g.customAfterOperation);
applyAfterOperationsInCol(colIndex, ctx) {
this._applyOpsInCol(colIndex, ctx, g => g.customAfterOperation);
}

/**
* @param {!int} colIndex
* @param {!CircuitEvalArgs} args
* @param {!function(!Gate) : !function(!CircuitEvalArgs)} opGetter
* @param {!CircuitEvalContext} ctx
* @param {!function(!Gate) : !function(!CircuitEvalContext)} opGetter
* @private
*/
_applyOpsInCol(colIndex, args, opGetter) {
_applyOpsInCol(colIndex, ctx, opGetter) {
if (colIndex < 0 || colIndex >= this.columns.length) {
return;
}
Expand All @@ -821,7 +821,7 @@ class CircuitDefinition {

let op = opGetter(gate);
if (op !== undefined) {
op(args.withRow(args.row + row));
op(ctx.withRow(ctx.row + row));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* Values used by the various gate effects.
*
* The current state is stored *and updated* via the stateTrader field.
*/
class CircuitEvalArgs {
class CircuitEvalContext {
/**
* @param {!number} time
* @param {undefined|!int} qubitRow
* @param {!int} wireCount
* @param {!Controls} controls
* @param {!WglTexture} controlsTexture
* @param {undefined|!WglTextureTrader} stateTrader
* @param {!WglTextureTrader} stateTrader
* @param {!Map.<!string, *>} customContextFromGates
*/
constructor(time,
Expand All @@ -31,18 +33,18 @@ class CircuitEvalArgs {
this.controls = controls;
/** @type {!WglTexture} */
this.controlsTexture = controlsTexture;
/** @type {undefined|!WglTextureTrader} */
/** @type {!WglTextureTrader} */
this.stateTrader = stateTrader;
/** @type {!Map.<!string, *>} */
this.customContextFromGates = customContextFromGates;
}

/**
* @returns {!CircuitEvalArgs}
* @returns {!CircuitEvalContext}
* @private
*/
_clone() {
return new CircuitEvalArgs(
return new CircuitEvalContext(
this.time,
this.row,
this.wireCount,
Expand All @@ -54,7 +56,7 @@ class CircuitEvalArgs {

/**
* @param {!int} row
* @returns {!CircuitEvalArgs}
* @returns {!CircuitEvalContext}
*/
withRow(row) {
let r = this._clone();
Expand All @@ -63,4 +65,4 @@ class CircuitEvalArgs {
}
}

export {CircuitEvalArgs}
export {CircuitEvalContext}
6 changes: 3 additions & 3 deletions src/circuit/CircuitShaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ const CONTROL_SELECT_SHADER = makePseudoShaderWithInputsAndOutputAndCode(
/**
* Renders the result of applying a controlled swap operation to a superposition.
*
* @param {!CircuitEvalArgs} args
* @param {!CircuitEvalContext} ctx
* @param {!int} otherRow
*/
CircuitShaders.swap = (args, otherRow) =>
SWAP_QUBITS_SHADER.withArgs(...ketArgs(args, otherRow - args.row + 1));
CircuitShaders.swap = (ctx, otherRow) =>
SWAP_QUBITS_SHADER.withArgs(...ketArgs(ctx, otherRow - ctx.row + 1));
const SWAP_QUBITS_SHADER = ketShaderPermute('', `
float low_bit = mod(out_id, 2.0);
float mid_bits = floor(mod(out_id, span*0.5)*0.5);
Expand Down
4 changes: 2 additions & 2 deletions src/circuit/CircuitStats.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CircuitDefinition} from "src/circuit/CircuitDefinition.js"
import {CircuitEvalArgs} from "src/circuit/CircuitEvalArgs.js"
import {CircuitEvalContext} from "src/circuit/CircuitEvalContext.js"
import {CircuitShaders} from "src/circuit/CircuitShaders.js"
import {KetTextureUtil} from "src/circuit/KetTextureUtil.js"
import {Config} from "src/Config.js"
Expand Down Expand Up @@ -229,7 +229,7 @@ class CircuitStats {
let stateTrader = new WglTextureTrader(CircuitShaders.classicalState(0).toVec2Texture(numWires));
let controlTex = CircuitShaders.controlMask(Controls.NONE).toBoolTexture(numWires);
let {colQubitDensities, customStats, customStatsMap} = advanceStateWithCircuit(
new CircuitEvalArgs(
new CircuitEvalContext(
time,
0,
numWires,
Expand Down
Loading

0 comments on commit 10a283e

Please sign in to comment.