2
$\begingroup$

I executed the following code in a Jupyter notebook:

from qiskit import QuantumCircuit, Aer, transpile

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)

backend = Aer.get_backend("aer_simulator")
qc.save_statevector()

tqc = transpile(qc, backend)
job = backend.run(tqc)
result = job.result()
statevector = result.get_statevector(tqc, 4)
print(statevector)

and receive the output:

Statevector([0.7071+0.j, 0.    +0.j, 0.    +0.j, 0.7071+0.j],
            dims=(2, 2))

But, according to the get_statevector() documentation, the get_statevector() method returns list of complex amplitudes. So, I would expect only a list of complex numbers in the output. What is the dims=(2,2) part of the output? Why is it appearing in the output and how can I get the output to reflect the documentation?

$\endgroup$

2 Answers 2

0
$\begingroup$

It looks like a bug in the documentation, introduced here: https://github.com/Qiskit/qiskit-terra/pull/7277

You can get the list of complex with statevector.data.

$\endgroup$
0
$\begingroup$

As the other answer points out, the documentation you linked to is incorrect. The get_statevector() method actually returns a qiskit.quantum_info.states.statevector.Statevector object (you can confirm this by changing your last line of code to print(type(statevector))) the documentation for which is found here.

As for the printed output you asked about, it is the result of calling a print() statement on an object, which can have a variety of results. In this case, calling print() on a Statevector object calls that object's built in function __repr__. Looking at the source code for the Statevector object we can see that dims=(2,2) appears because __repr__ is printing the dimensions of the _op_shape attribute.

Rather than worrying too much about that attribute of the code, however, we can see what the dims=(2,2) printout is telling us just by changing your line of code qc = QuantumCircuit(2) to qc = QuantumCircuit(n) for $n=3, 4$, etc, because when $n=3$, dims=(2,2,2), while when $n=4$, dims=(2,2,2,2), etc. Since $n$ gives the number of qubits in the system, and since qubits are two-level systems (i.e. they live in a two-dimensional vector space), we see that dims just tells us the overall dimensionality of the system, with each additional qubit increasing that dimensionality by a factor of two.

Finally, if you change your code from statevector = result.get_statevector(tqc, 4) to statevector = result.get_statevector(tqc, 4).data then your statevector variable will just be a list of complex numbers like you want.

$\endgroup$

Your Answer

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

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