5
$\begingroup$

Suppose we have a Qiskit circuit like

from qiskit import QuantumCircuit

qc = QuantumCircuit(1,1)
qc.h(0)
qc.measure(0,0)

If we want to find the probabilities for each outcome (by simulation) the usual method is to run it for many shots.

from qiskit import Aer

backend = Aer.get_backend('aer_simulator')

shots = 1024
counts = backend.run(qc,shots=shots).result().get_counts()

This will tell us how many shots came out for each result, and then we can estimate the probabilities from this.

probs = {string:count/shots for string,count in counts.items()}

However, since all measurements are at the end in this case, we can actually get the exact probabilities without sampling. Just replace the measurement with save_state() and extract the result from the data attribute of result.

qc = QuantumCircuit(1,1)
qc.h(0)
qc.save_state()

probs = backend.run(qc,shots=1).result().data()['stabilizer'].probabilities_dict()

Is it possible to do something similar for a circuit with measurements throughout, such as the following?

from qiskit import QuantumCircuit

qc = QuantumCircuit(1,2)
qc.h(0)
qc.measure(0,0)
qc.h(0)
qc.measure(0,1)

Putting save_state after measurements seems to produce an output for given shots. So a naive adaption of the same process doesn't work without sampling.

$\endgroup$

2 Answers 2

1
$\begingroup$

As I have understood, what you are asking for is to see the probability outcome of state in different places of a circuit without actually performing a measurement in those places. The following code using qiskit.quantum_info.Statevector.probabilities can be helpful.

For more details on qiskit.quantum_info.Statevector you can look here.

$\endgroup$
1
$\begingroup$

Create your own QuantumCircuit without measurement gates and classical bits,

from qiskit import QuantumRegister, QuantumCircuit, Aer
from qiskit.quantum_info import Statevector
import numpy as np

q = QuantumRegister(1)

qc = QuantumCircuit(q)

qc.u(np.pi/4,np.pi/4,np.pi/4, q)

qc.draw()
    ┌────────────────┐
q0: ┤ U(π/4,π/4,π/4) ├
    └────────────────┘

and then measure probabilities:

backend = Aer.get_backend('statevector_simulator')

outputstate = backend.run(qc, shots=1).result().get_statevector()

probs = Statevector(outputstate).probabilities()

print(probs)
[0.85355339 0.14644661]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.