Skip to content

Commit

Permalink
Renamed |S> initial state to |i>
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Mar 23, 2019
1 parent 25849af commit 631df9b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
18 changes: 4 additions & 14 deletions src/circuit/CircuitDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {equate_Maps} from "src/base/Equate.js";
import {Gate} from "src/circuit/Gate.js"
import {GateColumn} from "src/circuit/GateColumn.js"
import {GateShaders} from "src/circuit/GateShaders.js"
import {Gates} from "src/gates/AllGates.js"
import {Gates, INITIAL_STATES_TO_GATES} from "src/gates/AllGates.js"
import {Point} from "src/math/Point.js"
import {seq, Seq} from "src/base/Seq.js"
import {Util} from "src/base/Util.js"
Expand Down Expand Up @@ -147,7 +147,7 @@ class CircuitDefinition {
withSwitchedInitialStateOn(wire) {
let m = new Map([...this.customInitialValues.entries()]);
let v = m.get(wire);
let cycle = [undefined, '1', '+', '-', 'S', 'S†'];
let cycle = [...INITIAL_STATES_TO_GATES.keys()];
let newVal = cycle[(cycle.indexOf(v) + 1) % cycle.length];
if (newVal === undefined) {
m.delete(wire);
Expand Down Expand Up @@ -901,10 +901,10 @@ class CircuitDefinition {
applyInitialStateOperations(ctx) {
for (let wire = 0; wire < this.numWires; wire++) {
let state = this.customInitialValues.get(wire);
if (!STATE_TO_GATES.has(state)) {
if (!INITIAL_STATES_TO_GATES.has(state)) {
throw new DetailedError('Unrecognized initial state.', {state});
}
for (let gate of STATE_TO_GATES.get(state)) {
for (let gate of INITIAL_STATES_TO_GATES.get(state)) {
GateShaders.applyMatrixOperation(ctx.withRow(ctx.row + wire), gate.knownMatrixAt(ctx.time))
}
}
Expand Down Expand Up @@ -1117,16 +1117,6 @@ function firstLastMatchInRange(rangeLen, predicate){
return [first, last];
}

/** @type {!Map.<undefined|!string, !Array.<!Gate>>} */
const STATE_TO_GATES = new Map([
[undefined, []],
['1', [Gates.HalfTurns.X]],
['+', [Gates.HalfTurns.H]],
['-', [Gates.HalfTurns.H, Gates.HalfTurns.Z]],
['S', [Gates.HalfTurns.H, Gates.QuarterTurns.SqrtZForward]],
['S†', [Gates.HalfTurns.H, Gates.QuarterTurns.SqrtZBackward]]
]);

CircuitDefinition.EMPTY = new CircuitDefinition(0, []);

export {CircuitDefinition}
4 changes: 2 additions & 2 deletions src/circuit/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {DetailedError} from "src/base/DetailedError.js"
import {Format} from "src/base/Format.js"
import {Gate, GateBuilder} from "src/circuit/Gate.js"
import {GateColumn} from "src/circuit/GateColumn.js"
import {Gates} from "src/gates/AllGates.js"
import {Gates, INITIAL_STATES_TO_GATES} from "src/gates/AllGates.js"
import {Matrix} from "src/math/Matrix.js"
import {Util} from "src/base/Util.js"
import {notifyAboutRecoveryFromUnexpectedError} from "src/fallback.js"
Expand Down Expand Up @@ -428,7 +428,7 @@ function _fromJson_InitialState(json) {
// 0 is the default. Don't need to do anything.
} else if (v === 1) {
result.set(i, '1');
} else if (['+', '-', 'S', 'S†'].indexOf(v) !== -1) {
} else if (INITIAL_STATES_TO_GATES.has(v)) {
result.set(i, v);
} else {
throw new DetailedError('Unrecognized initial state key.', {v, json});
Expand Down
12 changes: 11 additions & 1 deletion src/gates/AllGates.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,14 @@ Gates.BottomToolboxGroups = [
},
];

export {Gates}
/** @type {!Map.<undefined|!string, !Array.<!Gate>>} */
const INITIAL_STATES_TO_GATES = new Map([
[undefined, []],
['1', [Gates.HalfTurns.X]],
['+', [Gates.HalfTurns.H]],
['-', [Gates.HalfTurns.H, Gates.HalfTurns.Z]],
['i', [Gates.HalfTurns.H, Gates.QuarterTurns.SqrtZForward]],
['-i', [Gates.HalfTurns.H, Gates.QuarterTurns.SqrtZBackward]]
]);

export {Gates, INITIAL_STATES_TO_GATES}
2 changes: 1 addition & 1 deletion test/circuit/CircuitStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ suite.testUsingWebGL('classical-bit-rotate-with-classical-control-does-fire', ()

suite.testUsingWebGL("initial_states", () => {
let circuit = Serializer.fromJson(CircuitDefinition, {
init: [0, 1, '+', '-', 'S', 'S†'],
init: [0, 1, '+', '-', 'i', '-i'],
cols: [],
});
let stats = CircuitStats.fromCircuitAtTime(circuit, 0);
Expand Down

0 comments on commit 631df9b

Please sign in to comment.