4
$\begingroup$

Hi i know the question was already asked but my case is slightly different. I am trying to do the qiskit tutorial:

from qiskit import QuantumCircuit, assemble, Aer
from qiskit.visualization import plot_histogram, plot_bloch_vector
from math import sqrt, pi

qc = QuantumCircuit(1) # We are redefining qc
initial_state = [0.+1.j/sqrt(2),1/sqrt(2)+0.j]
qc.initialize(initial_state, 0)
qc.draw()

qc.save_statevector()
result = sim.run(assemble(qc)).result()
state = result.get_statevector()
print("Qubit State = " + str(state))

but i get this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [29], in <cell line: 10>()
      7 qc.initialize(initial_state, 0)
      8 qc.draw()
---> 10 qc.save_statevector()
     11 result = sim.run(assemble(qc)).result()
     12 state = result.get_statevector()

AttributeError: 'QuantumCircuit' object has no attribute 'save_statevector'

I know that this is verion dependen but pip show qiskit proofs that i use version 0.36 can somebody pls tell me what else i could try? thanks

$\endgroup$

2 Answers 2

2
$\begingroup$

This issue happens because save_statevector is a Qiskit Aer instruction that is added to QuantumCircuit class when Aer is imported properly (see the details here)

Just import Aer as follows:

from qiskit.providers.aer import Aer

Another solution is to import save_statevector itself:

from qiskit.providers.aer.library import save_statevector
$\endgroup$
1
  • $\begingroup$ Thank you that solved it $\endgroup$
    – user21064
    Jun 24, 2022 at 11:08
4
$\begingroup$

As with so many programming/debugging questions, the accepted answer

Just import Aer as follows:

from qiskit.providers.aer import Aer

is already stale.

The most recent version of qiskit does not appear to even include an importable qiskit.providers.aer.

An alternative solution to the problem is to make sure that the line

sim = Aer.get_backend('aer_simulator')

Is called before the line

qc.save_statevector() 

See this discussion for more details about this strange behavior.

$\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.