From 86763be687a2aa0d8ceb4fdfd77e6ef4e900f359 Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Sun, 24 Mar 2019 11:09:49 -0700 Subject: [PATCH] Reduce size of amplitude json output - Use {r:#, i#} instead of {real:#, imag:#} - Repack them to be on one line after stringifying - Reduce indentation --- src/math/Matrix.js | 2 +- src/ui/exports.js | 5 ++++- test/circuit/CircuitStats.test.js | 8 ++++---- test/gates/AmplitudeDisplay.test.js | 24 ++++++++++++------------ 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/math/Matrix.js b/src/math/Matrix.js index 78cc4f0d..5e3812ca 100644 --- a/src/math/Matrix.js +++ b/src/math/Matrix.js @@ -1439,7 +1439,7 @@ Matrix.HADAMARD = Matrix.square(1, 1, 1, -1).times(Math.sqrt(0.5)); * @returns {*} */ function complexVectorToReadableJson(vector) { - return seq(vector).map(e => {return {real: Complex.realPartOf(e), imag: Complex.imagPartOf(e)}; }).toArray(); + return seq(vector).map(e => {return {r: Complex.realPartOf(e), i: Complex.imagPartOf(e)}; }).toArray(); } /** diff --git a/src/ui/exports.js b/src/ui/exports.js index 087317eb..14b7bd78 100644 --- a/src/ui/exports.js +++ b/src/ui/exports.js @@ -120,7 +120,10 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { copyButton, outputTextElement, copyResultElement, - () => JSON.stringify(mostRecentStats.get().toReadableJson(!excludeAmps.checked), null, ' ')); + () => { + let raw = JSON.stringify(mostRecentStats.get().toReadableJson(!excludeAmps.checked), null, ' '); + return raw.replace(/{\s*"r": /g, '{"r":').replace(/,\s*"i":\s*([-e\d\.]+)\s*}/g, ',"i":$1}'); + }); })(); // Export offline copy. diff --git a/test/circuit/CircuitStats.test.js b/test/circuit/CircuitStats.test.js index 08cf5e14..9b72a1ca 100644 --- a/test/circuit/CircuitStats.test.js +++ b/test/circuit/CircuitStats.test.js @@ -405,10 +405,10 @@ suite.testUsingWebGL("toReadableJson", () => { assertThat(json).isApproximatelyEqualTo({ circuit: Serializer.toJson(c), output_amplitudes: [ - {real: Math.sqrt(0.5), imag: 0}, - {real: 0, imag: 0}, - {real: Math.sqrt(0.5), imag: 0}, - {real: 0, imag: 0}, + {r: Math.sqrt(0.5), i: 0}, + {r: 0, i: 0}, + {r: Math.sqrt(0.5), i: 0}, + {r: 0, i: 0}, ], time_parameter: 0.5, chance_of_surviving_to_each_column: [1, 1, 1, 1], diff --git a/test/gates/AmplitudeDisplay.test.js b/test/gates/AmplitudeDisplay.test.js index 79a096b9..1192080d 100644 --- a/test/gates/AmplitudeDisplay.test.js +++ b/test/gates/AmplitudeDisplay.test.js @@ -152,10 +152,10 @@ suite.testUsingWebGL("AmplitudesDisplayWithOtherQubit_Minus", () => { 0); let out = stats.toReadableJson(); assertThat(out.displays[0].data.ket).isApproximatelyEqualTo([ - {real: 1, imag: 0}, - {real: 0, imag: 0}, - {real: 0, imag: 0}, - {real: 0, imag: 0}, + {r: 1, i: 0}, + {r: 0, i: 0}, + {r: 0, i: 0}, + {r: 0, i: 0}, ]); assertThat(out.displays[0].data.coherence_measure).isApproximatelyEqualTo(1); }); @@ -166,10 +166,10 @@ suite.testUsingWebGL("AmplitudesDisplayWithOtherQubit_i", () => { 0); let out = stats.toReadableJson(); assertThat(out.displays[0].data.ket).isApproximatelyEqualTo([ - {real: 1, imag: 0}, - {real: 0, imag: 0}, - {real: 0, imag: 0}, - {real: 0, imag: 0}, + {r: 1, i: 0}, + {r: 0, i: 0}, + {r: 0, i: 0}, + {r: 0, i: 0}, ]); assertThat(out.displays[0].data.coherence_measure).isApproximatelyEqualTo(1); }); @@ -180,10 +180,10 @@ suite.testUsingWebGL("AmplitudesDisplayWithOtherQubit_own_i", () => { 0); let out = stats.toReadableJson(); assertThat(out.displays[0].data.ket).isApproximatelyEqualTo([ - {real: Math.sqrt(0.5), imag: 0}, - {real: 0, imag: Math.sqrt(0.5)}, - {real: 0, imag: 0}, - {real: 0, imag: 0}, + {r: Math.sqrt(0.5), i: 0}, + {r: 0, i: Math.sqrt(0.5)}, + {r: 0, i: 0}, + {r: 0, i: 0}, ]); assertThat(out.displays[0].data.coherence_measure).isApproximatelyEqualTo(1); });