4
$\begingroup$

I'm using qiskit with the online IBM Quantum Lab and when I run the following code

from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_bloch_multivector

qc = QuantumCircuit(1) 

qc.h(0)
qc.h(0)
qc.h(0)

out = execute(qc,Aer.get_backend('statevector_simulator')).result().get_statevector()
print(out)
plot_bloch_multivector(out)

... it results in the following state vector for the qubit:

[0.70710678+0.00000000e+00j 0.70710678+1.57009246e-16j]

As you can see there's a very small imaginary component in the |1> amplitude.

These imaginary values pop up often with qiskit, such as:

qc.x(0)
qc.h(0)
--> [ 0.70710678+0.00000000e+00j -0.70710678+8.65956056e-17j]

or even very small non-imaginary numbers, such as:

qc.x(0)
qc.h(0)
qc.h(0)
--> [6.123234e-17+0.00000000e+00j 1.000000e+00-2.22044605e-16j]

Is this something unique to quantum mechanics/computing that actually has practical consequences when doing computation, or perhaps simply a quirk of Python's scientific notation implementation.

$\endgroup$
1
  • 7
    $\begingroup$ These are just precision errors. The calculations are using doubles, and $\frac{1}{\sqrt{2}}$ has no exact representation as a double. $\endgroup$ Apr 29, 2021 at 17:33

2 Answers 2

5
$\begingroup$

This is just a quirk of how complex numbers are implemented in Python/Numpy, etc. At the end of the day, these are represented as floating-point numbers within the target simulator. These are then transformed via various mathematical operations to implement the simulation and this eventually leads to an accumulation of round off error. For all intents and purposes, e-16 is zero, but a computer doesn't know that.

$\endgroup$
2
  • 3
    $\begingroup$ That's why the real parts are imprecise, but how are nonzero imaginary parts creeping in? The initial state vector and the gates are purely real, and the sum and product of complex numbers with exactly-zero imaginary parts have exactly zero imaginary part, even by IEEE rules. $\endgroup$
    – benrg
    Apr 29, 2021 at 20:11
  • $\begingroup$ I'm uncertain about this. This is likely related to a peculiarity related to the implementation of Aer's statevector simulator. $\endgroup$
    – user47787
    Aug 24, 2021 at 0:06
1
$\begingroup$

As mentioned by @user47787

This is yet another case of floating-point representation error at the machine level.

This behaviour is language agnostic and has nothing to do with Python or Numpy or Qiskit.

Related Reads:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.