1
$\begingroup$

I am Using Qiskit SDK 1.0. I have a problem with get_statevector in qiskit SDK 1.0 and using BasicSimulator. My code is attached below.

simulator = BasicSimulator()#Aer.get_backend('statevector_simulator')
result = simulator.run(circ).result()#execute(circ,simulator).result()
sv = result.get_statevector(circ)
print(sv)
$\endgroup$

2 Answers 2

1
$\begingroup$

There is an example here https://docs.quantum.ibm.com/api/migration-guides/qiskit-1.0-features#basicaer-1 showing how it was done (previous) and what to do now (current), as below

from qiskit import QuantumCircuit
 
qc = QuantumCircuit(3)
qc.h(0)
qc.h(1)
qc.cx(1, 2)
qc.measure_all()
 
# Previous
from qiskit import BasicAer
backend = BasicAer.get_backend("statevector_simulator")
statevector = backend.run(qc).result().get_statevector()
 
# Current
qc.remove_final_measurements()  # no measurements allowed
from qiskit.quantum_info import Statevector
statevector = Statevector(qc)
$\endgroup$
1
  • $\begingroup$ (Not the OP) The problem with this is that, as far as I can tell it doesn't work when you need the statevectors after a partial measurement. According to the documentation, get_statevector() remains in the SDK in the 1.0 release but something with it's semantics seem to have changed $\endgroup$
    – sinanspd
    Commented Mar 17 at 22:06
0
$\begingroup$

Your code is an incorrect usage of BasicSimulator. BasicSimulator is intended to replace QasmSimulatorPy (not StatevectorSimulatorPy). For example, you can use BasicSimulator like this:

from qiskit.providers.basic_provider import BasicSimulator
from qiskit import QuantumCircuit

circ = QuantumCircuit(1)
circ.h(0)
circ.measure_all()

simulator = BasicSimulator()
result = simulator.run(circ).result()
counts = result.get_counts()
print(counts)
{'0': 517, '1': 507}

To get the statevector, you can still use Aer.get_backend('statevector_simulator') like this:

from qiskit import QuantumCircuit
from qiskit_aer import Aer

circ = QuantumCircuit(1)
circ.h(0)

simulator = Aer.get_backend('statevector_simulator')
result = simulator.run(circ).result()
sv = result.get_statevector(circ)
print(sv)
Statevector([0.70710678+0.j, 0.70710678+0.j],
            dims=(2,))

But the preferred method, as was stated in this answer https://quantumcomputing.stackexchange.com/a/37240/27580, is to simply cast the circuit as a statevector using Statevector(circ).

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