Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probabilities can be truncated to 0.00000% in stateAsString(true) #86

Open
DanBlackwell opened this issue Apr 11, 2024 · 4 comments
Open

Comments

@DanBlackwell
Copy link

Not necessarily an issue - perhaps an enhancement though. Due to the truncation (or maybe rounding - haven't looked behind the scenes) some probabilities (as well as vector values) for viable states end up being output as 0.

Example program:

const QuantumCircuit = require('quantum-circuit');

let qc = new QuantumCircuit();
let qasm = "OPENQASM 2.0;\n" +
           "include \"qelib1.inc\";\n" +
           "qreg r1[1];\n" +
           "rx(0.0000000001) r1;\n";

qc.importQASM(qasm, (error) => {
    qc.run();
    console.log(qc.stateAsString(true));
})

Output:

 1.00000000+0.00000000i|0>	100.00000%
 0.00000000+0.00000000i|1>	  0.00000%
@perak
Copy link
Collaborator

perak commented Jul 12, 2024

@DanBlackwell thank you for reporting.

Late answer, I know...

Internal state is correct, it is truncated only when printed.

qc.stateAsSimpleArray();

Returns:

[
    { re: 1, im: 0 },
    { re: 0, im: -5e-11 }
]

And:

qc.stateAsArray(true);

Returns:

[
  {
    index: 0,
    indexBinStr: '0',
    amplitude: { re: 1, im: 0 },
    amplitudeStr: ' 1.00000000+0.00000000i',
    magnitude: 1,
    chance: 100,
    chanceStr: '100.00000',
    phase: 0,
    phaseStr: '0.00000'
  },
  {
    index: 1,
    indexBinStr: '1',
    amplitude: { re: 0, im: -5e-11 },
    amplitudeStr: ' 0.00000000+0.00000000i',
    magnitude: 2.5000000000000002e-21,
    chance: 2.5e-19,
    chanceStr: '0.00000',
    phase: -1.5707963267948966,
    phaseStr: '-1.57080'
  }
]

Goal of stateAsString() method is to show state which is easy to read, and 8 decimal places is enough info for quick view. This is suitable only for small circuits anyway, because nobody will read state with e.g. 2^20 entries :). In small circuits, 8-th decimal place doesn't make any difference in measurement outcome.

However, it could be more informative if it prints something like:

 1.00000000+0.00000000i|0>	 100.00000%
>0.00000000+0.00000000i|1>	  >0.00000%

OR:

 1.00000000+0.00000000i|0>	 100.00000%
+0.00000000+0.00000000i|1>	  +0.00000%

Or do you have better idea?

@DanBlackwell
Copy link
Author

These look like good solutions to me; if the print formatting is to a fixed number of decimals places, I think the following may be more informative:

 1.00000000+0.00000000i|0>	 100.00000%
+0.00000000+0.00000000i|1>	  <0.00001%

to me >0.0% makes me think 0% < probability <= 100%, while <0.00001% suggests 0% < probability <= 0.00001%. I think that ultimately between the three suggestions it's a matter of personal preference.

@perak
Copy link
Collaborator

perak commented Jul 12, 2024

Oh yes, that's better solution for probability. Thanks.

But, amplitude...

We have real and imaginary part and adding any mark there will make it more confusing (especially for imaginary part).

Maybe simply to keep amplitude unchanged and mark only probability:

 1.00000000+0.00000000i|0>	 100.00000%
 0.00000000+0.00000000i|1>	  <0.00001%

@DanBlackwell
Copy link
Author

Hmm, besides going to e-notation I'm not sure there really is a nice solution to this. If it were me, I'd be tempted to add a [TRUNCATED] to the start or end of any line where the value is too small to be printed properly (or log something to stderr or whatever the JS equivalent is).

I think that as long as the documentation notes that this is possible, that's probably good enough imo. Looking at the documentation here, I initially thought that the true parameter was being ignored when getting 0% out; if it had noted that truncation is possible I'd have not filed this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants