Skip to content

Commit

Permalink
Reduce size of amplitude json output
Browse files Browse the repository at this point in the history
- Use {r:#, i#} instead of {real:#, imag:#}
- Repack them to be on one line after stringifying
- Reduce indentation
  • Loading branch information
Strilanc committed Mar 24, 2019
1 parent 3a17740 commit 86763be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/math/Matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/ui/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions test/circuit/CircuitStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
24 changes: 12 additions & 12 deletions test/gates/AmplitudeDisplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit 86763be

Please sign in to comment.