Skip to content

Commit

Permalink
Fix detect-control-reset gates not being able to toggle classical wires
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Mar 24, 2019
1 parent 91ff012 commit dc2daed
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/circuit/Gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class Gate {
* @private
*/
this._controlBit = undefined;
/**
* Determines if a controlled gate's control is guaranteed to be known classically. This is used by the
* combination detect-control-reset gates as a way to communicate that they can control permutation operations
* on classical wires.
* @type {boolean}
* @private
*/
this._isClassicalControl = false;
/**
* Indicates that this gate is guaranteed to preserve probability (as opposed to e.g. post-selection).
* When gates with this property not set to true are present in a column, the simulator computes losses/gains.
Expand Down Expand Up @@ -284,6 +292,7 @@ class Gate {
g._effectCreatesSuperpositions = this._effectCreatesSuperpositions;
g._affectsOtherWires = this._affectsOtherWires;
g._controlBit = this._controlBit;
g._isClassicalControl = this._isClassicalControl;
g.isControlWireSource = this.isControlWireSource;
g._isDefinitelyUnitary = this._isDefinitelyUnitary;
g.knownPhaseTurnsFunc = this.knownPhaseTurnsFunc;
Expand Down Expand Up @@ -393,6 +402,13 @@ class Gate {
return this._controlBit !== undefined;
}

/**
* @returns {!boolean}
*/
isClassicalControl() {
return this._isClassicalControl;
}

/**
* @returns {undefined|!boolean}
*/
Expand Down Expand Up @@ -809,12 +825,15 @@ class GateBuilder {
/**
* Sets meta-properties to indicate a gate is a control.
* @param {!boolean} bit: Whether gate is a control or anti-control. Use before/after operations for flexibility.
* @param {!boolean} guaranteedClassical Whether or not the control can be used to control permutations of classical
* wires, even if placed on a coherent wire.
* @returns {!GateBuilder}
*/
markAsControlExpecting(bit) {
markAsControlExpecting(bit, guaranteedClassical=false) {
this.gate._controlBit = bit;
this.gate.isControlWireSource = true;
this.gate.interestedInControls = false;
this.gate._isClassicalControl = guaranteedClassical;
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/circuit/GateColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class GateColumn {
for (let i = 0; i < this.gates.length; i++) {
if ((inputMeasureMask & (1 << i)) === 0 &&
this.gates[i] !== undefined &&
this.gates[i].isControl()) {
this.gates[i].isControl() &&
!this.gates[i].isClassicalControl()) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gates/Detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function makeDetectControlClearGate(axis) {
setTitle(`${axis} Detect-Control-Reset`).
setBlurb(`Does a collapsing ${axis}-axis measurement.\nControls operations with the result.\nResets the target to |0⟩.`).
setDrawer(args => drawDetectClearReset(args, axis)).
markAsControlExpecting(true).
markAsControlExpecting(true, true).
markAsReachingOtherWires().
setActualEffectToUpdateFunc(() => {}).
setStatTexturesMaker(withClearedControls(detectorStatTexture)).
Expand Down
6 changes: 6 additions & 0 deletions test/circuit/CircuitDefinition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ suite.test("gateAtLocIsDisabledReason", () => {
bad(1, 0, `MR`, ['R', Gates.Detectors.XDetectControlClear]);
bad(1, 0, `MR`, ['R', Gates.Detectors.YDetectControlClear]);
good(1, 0, `MR`, ['R', Gates.Detectors.ZDetectControlClear]);
good(3, 1, `---]-
-M-X-`, [']', Gates.Detectors.XDetectControlClear]);
good(3, 1, `---]-
-M-X-`, [']', Gates.Detectors.YDetectControlClear]);
good(3, 1, `---]-
-M-X-`, [']', Gates.Detectors.ZDetectControlClear]);

// Permutation sub-groups.
good(2, 0, `--P-
Expand Down

0 comments on commit dc2daed

Please sign in to comment.