I'm running the simulation of some circuit on the Qiskit qasm_simulator backend. My end goal is running this simulation several times and finally extract for each iteration the final qubit state and save it in some array (the goal is to get an average of the amplitude of the qubit state over several realisations). I understand that I should use some version of the .result() method, but I can't understand how basing myself on Qiskit documentation. I literally want to access the complex vector describing the result state of my computation after each of the (in this case) 100 iterations, I don't want to plot an histogram of the results.
I'm attaching a brief version of the part of the script where I'd need to add this bit of code.
-
1$\begingroup$ Looks like you want to get the statevector out of the circuit. For that you can use the statevector simulator $\endgroup$– Junye HuangJan 24 at 14:45
-
1$\begingroup$ Hi and welcome to Quantum Computing SE. Please post codes as a text not screenshots. You can use ``` to start and Finish section with the code. $\endgroup$– Martin VeselyJan 25 at 7:14
1 Answer
Looks like you are trying to get the statevector of the circuit. For that you can use the statevector_simulator
.
# A Bell circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)
# Get statevector
statevector_simulator = Aer.get_backend('statevector_simulator')
job_sv = statevector_simulator.run(transpile(qc, statevector_simulator), shots=100)
result_sv = job_sv.result()
statevector = result_sv.get_statevector()
Output
Statevector([0.70710678+0.j, 0. +0.j, 0. +0.j,
0.70710678+0.j],
dims=(2, 2))
If you want to get the sample counts AND the statevector, you can run two jobs in each iteration, one for the qasm_simulator
for counts and statevector_simulator
for statevector. Alternatively you can also use the Statevector
class in qiskit.quantum_info
module for both purposes.
from qiskit.quantum_info import Statevector
sv = Statevector(qc)
shots = sv.sample_counts(100)
print(sv)
print(shots)
Output
Statevector([0.70710678+0.j, 0. +0.j, 0. +0.j,
0.70710678+0.j],
dims=(2, 2))
{'00': 55, '11': 45}
-
$\begingroup$ Thanks for the answer. I was trying to avoid using the Statevector class as (for what I've understood) in a real quantum computation protocol it would be like "cheating", as you can really access the state vector by measurement. $\endgroup$ Jan 24 at 15:06
-
$\begingroup$ What you said is correct. You won’t be able to get the state vector if you run it on a real quantum system. That’s what qasm_simulator trying to mimic. When you measure the circuit the quantum state is collapsed to the measurement outcome so you won’t be able find out the state. You could use some techniques such as state tomography to estimate the state from the measurement but I think this is not what you want to achieve. $\endgroup$ Jan 24 at 15:15
-
$\begingroup$ Can you provide context for why you want to get the state and what you want to achieve? $\endgroup$ Jan 24 at 15:15
-
$\begingroup$ Fundamentally I'm simulating an unitary depending on a variable theta which should, in turn, mimic a Trotterized time evolution of some famous Hamiltonian. I am not saying I'd like to do a canonical Trotter expansion starting from a given H, what I'm saying is that the repeated application of this circuit should be equivalent to a Trotterized time evolution of said Hamiltonian. Hence I'm doing something like $U(\theta_{n})...U(\theta_{2})U(\theta_{1})$. So far I've made it, what I also wanted to implement was averaging over different shots for each theta. $\endgroup$ Jan 24 at 15:28