If you are using Qiskit v0.9 or higher, then snapshot_statevector
has been superseded by the save_statevector
circuit method. So you can use your initial code but need to just save the statevector at some point:
from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator
qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)
qc.save_statevector()
backend = QasmSimulator()
backend_options = {'method': 'statevector'}
job = execute(qc, backend, backend_options=backend_options)
job_result = job.result()
print(job_result.get_statevector(qc))
If you need to save statevector at multiple places, then the result must be read from result.data(0)[]
, result.get_statevector
won't work here:
from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator
qc = QuantumCircuit(2,2)
qc.h(0)
qc.save_statevector(label='v1')
qc.cx(0,1)
qc.save_statevector(label='v2')
backend = QasmSimulator()
backend_options = {'method': 'statevector'}
job = execute(qc, backend, backend_options=backend_options)
result = job.result()
sv1 = result.data(0)['v1']
sv2 = result.data(0)['v2']
print('Statevector 1', sv1)
print('Statevector 2', sv2)
There is also a very handy set of operations in qiskit.quantum_info
, that allows to get and display the statevectors:
from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator
import qiskit.quantum_info as qi
qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)
stv1 = qi.Statevector.from_instruction(qc)
sim = QasmSimulator()
options = {'method': 'statevector'}
execute(qc, sim, backend_options=options)
stv1.draw('latex', prefix='Statevector1:')
This way you can get a nicely formatted result:
Statevector1:
\begin{bmatrix}
\tfrac{1}{\sqrt{2}} & 0 & 0 & \tfrac{1}{\sqrt{2}} \\
\end{bmatrix}