From 5332bcedc5d0fb24d3b0fc488c1b71dcc258bdd6 Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Sat, 8 Jun 2019 11:08:38 -0700 Subject: [PATCH] Fix conditional incoherent amplitude displays not normalizing magnitudes Fixes https://github.com/Strilanc/Quirk/issues/434 --- src/gates/AmplitudeDisplay.js | 4 +++- test/gates/AmplitudeDisplay.test.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/gates/AmplitudeDisplay.js b/src/gates/AmplitudeDisplay.js index d76795ce..3068f9d9 100644 --- a/src/gates/AmplitudeDisplay.js +++ b/src/gates/AmplitudeDisplay.js @@ -131,7 +131,9 @@ function processOutputs(span, pixelGroups, circuitDefinition) { let incoherentUnity = 0; for (let i = 0; i < n; i++) { incoherentUnity += rawIncoherentKetPixels[i]; - incoherentKetPixels[i << 1] = Math.sqrt(rawIncoherentKetPixels[i]); + } + for (let i = 0; i < n; i++) { + incoherentKetPixels[i << 1] = Math.sqrt(rawIncoherentKetPixels[i] / incoherentUnity); } if (isNaN(incoherentUnity) || incoherentUnity < 0.000001) { return { diff --git a/test/gates/AmplitudeDisplay.test.js b/test/gates/AmplitudeDisplay.test.js index da0b773f..c982125c 100644 --- a/test/gates/AmplitudeDisplay.test.js +++ b/test/gates/AmplitudeDisplay.test.js @@ -229,3 +229,15 @@ suite.testUsingWebGL("AmplitudesDisplayIncoherent_hadamard", () => { ]); assertThat(out.displays[0].data.coherence_measure).isLessThan(0.85); }); + +suite.testUsingWebGL("AmplitudesDisplayIncoherent_conditioned", () => { + let stats = CircuitStats.fromCircuitAtTime( + Serializer.fromJson(CircuitDefinition, {"cols":[[1,"•","Z"],["•","Amps1"]],"init":["+","+","+"]}), + 0); + let out = stats.toReadableJson(); + assertThat(out.displays[0].data.incoherentKet).isApproximatelyEqualTo([ + Math.sqrt(0.5), + Math.sqrt(0.5), + ]); + assertThat(out.displays[0].data.coherence_measure).isLessThan(0.85); +});