1
$\begingroup$

I am quite struggling with the usage of QuantumInstance API. This API is present in qiskit version 0.32.1 Below is small code:

from qiskit.utils import QuantumInstance 
from qiskit import Aer
simulator_used = "qasm_simulator"
backend = Aer.get_backend(simulator_used)



quantum_instance = QuantumInstance(backend = backend,
                                   shots = 2000,
                                   seed_simulator= 28,
                                   seed_transpiler = 28,
                                   basis_gates = None,
                                   optimization_level =1)

Suppose, I want to make a simulator as statevector_simulator. Then do I have to make shots =1?

Suppose I want to use the QuantumInstance in the code below. Then how should I modify the code below? If QuantumInstance cannot be used in the code below, then where should it be used?

from qiskit import Aer
from qiskit import execute
from qiskit import QuantumCircuit


circ = QuantumCircuit(3,3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)

backend = Aer.get_backend("qasm_simulator")  

job = execute(circ, backend, shots = 100000)
result = job.result()
counts = result.get_counts(circ)
print(counts)
$\endgroup$

1 Answer 1

1
$\begingroup$

If you want to use the statevector_simulator, you should generally set shots=1. The statevector simulator gives you the exact quantum state vector, which represents a deterministic outcome. Running it multiple times doesn't add additional information.

Here's how you would modify your code to use QuantumInstance:

from qiskit.utils import QuantumInstance 
from qiskit import Aer

# ... (your circuit definition)

simulator_used = "statevector_simulator"  # Change for statevector
backend = Aer.get_backend(simulator_used)

quantum_instance = QuantumInstance(backend=backend,
                                   shots=1,  # 1 shot for statevector
                                   seed_simulator=28,
                                   seed_transpiler=28)

job = quantum_instance.execute(circ)  # Use QuantumInstance to execute
result = job.result()

# With the statevector simulator, get the statevector directly
statevector = result.get_statevector(circ)
print(statevector)
$\endgroup$
5
  • $\begingroup$ Thank for the response @davidryan. I think, result = job.result() should be replaced with result = job.results. printing result will give [ExperimentResult(shots=1, success=True, meas_level=2, data=ExperimentResultData(counts={'0x0': 1}, statevector=[0.70710678+0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0.70710678+0.j]), ....................]. Now how to get statevector out of this list? $\endgroup$
    – Manu
    Mar 15 at 22:25
  • $\begingroup$ result = job.result() gives me the error: AttributeError: Attribute result is not defined $\endgroup$
    – Manu
    Mar 15 at 22:29
  • 1
    $\begingroup$ QuantumInstance was designed to give a common execution capability to algorithms from Qiskit Aqua which were moved into Qiskit and later all refactored to use Primitives (and now are also moved out of Qiskit into their own repo). QuantumInstance was deprecated, along with the old algorithms that used it, with the change to primitives. It is no longer in the latest Qiskit, and unless you had old code where you wanted to see that run, I would recommend not even trying to use it - well you can't with the latest Qiskit version! $\endgroup$
    – Steve Wood
    Mar 16 at 15:56
  • $\begingroup$ Thank you for the response @SteveWood. I have this code: from qiskit.algorithms import VQE vqe = VQE(ansatz=ansatz_opt, optimizer= optimizer, quantum_instance=quantum_instance, initial_point=initial_point_values ) where it needs quantum instance. I am not sure how to replace it or replace exactly with what? $\endgroup$
    – Manu
    Mar 17 at 7:29
  • 1
    $\begingroup$ That is the old VQE that did take a QuantumInstance. The latest VQE is in Qiskit Algorithms now - here is a tutorial showing VQE - there are others too qiskit-community.github.io/qiskit-algorithms/tutorials/…. It uses an Estimator primitive to do the quantum computation now, rather than how it did things before which was more directly executing circuits itself via the QuantumInstance. $\endgroup$
    – Steve Wood
    Mar 17 at 12:33

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.