Skip to content

Commit

Permalink
Detectors now preserve total probability
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Mar 23, 2019
1 parent b40b950 commit 25849af
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/gates/Detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ let detectorShader = makePseudoShaderWithInputsAndOutputAndCode(
float detectChance = read_detection_weight(0.0) / read_total_weight(0.0);
float detection_type = float(rnd < detectChance);
float own_type = read_classification(k);
return read_ket(k) * float(detection_type == own_type);
if (detection_type == own_type) {
float matchChance = detectChance * own_type + (1.0 - own_type) * (1.0 - detectChance);
return read_ket(k) / sqrt(matchChance);
} else {
return vec2(0.0, 0.0);
}
}
`);

Expand Down
23 changes: 22 additions & 1 deletion test/circuit/CircuitStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {Suite, assertThat, assertTrue} from "test/TestUtil.js"
import {CircuitStats} from "src/circuit/CircuitStats.js"

import {CircuitDefinition} from "src/circuit/CircuitDefinition.js"
import {Complex} from "src/math/Complex.js"
import {GateColumn} from "src/circuit/GateColumn.js"
import {Gates} from "src/gates/AllGates.js"
import {Matrix} from "src/math/Matrix.js"
Expand Down Expand Up @@ -367,3 +366,25 @@ suite.testUsingWebGL("initial_states", () => {
assertThat(stats.qubitDensityMatrix(9, 4).qubitDensityMatrixToBlochVector()).isApproximatelyEqualTo([0, +1, 0]);
assertThat(stats.qubitDensityMatrix(9, 5).qubitDensityMatrixToBlochVector()).isApproximatelyEqualTo([0, -1, 0]);
});

suite.testUsingWebGL("distillation", () => {
let c = circuit(
`
-X-X--X-X--X-X--X-X-------X-X--X-X-------X-X------------HTH-0-
-X-X--X-X--X-X-------X-X--X-X-------X-X-------X-X-------HTH-0-
-X-X--X-X-------X-X--X-X-------X-X--X-X------------X-X--HTH-0-
-X-X-------X-X--X-X--X-X-----------------X-X--X-X--X-X--HTH-0-
-X-X----------------------X-X--X-X--X-X--X-X--X-X--X-X--------
-#T]--#T]--#T]--#T]--#T]--#T]--#T]--#T]--#T]--#T]--#T]--------
`,
[']', Gates.Detectors.XDetectControlClear],
['0', Gates.PostSelectionGates.PostSelectOff],
['#', Gates.Controls.XControl],
['T', Gates.OtherZ.Z4]);
for (let i = 0; i < 5; i++) {
let stats = CircuitStats.fromCircuitAtTime(c, 0);
assertThat(stats.qubitDensityMatrix(Infinity, 4).qubitDensityMatrixToBlochVector()).isApproximatelyEqualTo(
[0, Math.sqrt(0.5), -Math.sqrt(0.5)]);
assertThat(stats.survivalRate(Infinity)).isApproximatelyEqualTo(1, 0.001);
}
});
18 changes: 18 additions & 0 deletions test/gates/Detector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,21 @@ suite.testUsingWebGL("collapsed-control-clicks", () => {
}
}
});

suite.testUsingWebGL("renormalizes", () => {
// Doesn't decrease survival probability.
let c = circuit(
'-]-D-]-D-]-D-]-',
[']', Gates.Detectors.XDetector],
['0', Gates.PostSelectionGates.PostSelectOff]);
let stats = CircuitStats.fromCircuitAtTime(c, 0);
assertThat(stats.survivalRate(Infinity)).isApproximatelyEqualTo(1, 0.001);

// Renormalization doesn't increase survival probability.
let c2 = circuit(
'-]-0-D-]-D-]-D-]-',
[']', Gates.Detectors.XDetector],
['0', Gates.PostSelectionGates.PostSelectOff]);
let stats2 = CircuitStats.fromCircuitAtTime(c2, 0);
assertThat(stats2.survivalRate(Infinity)).isApproximatelyEqualTo(0.5, 0.001);
});

0 comments on commit 25849af

Please sign in to comment.