Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.1k stars 2.34k forks source link

storing itermediate distribution in a grovers search #13173

Open Pavanmk1310 opened 1 week ago

Pavanmk1310 commented 1 week ago

storing the distribution values of each iteration of a Grover circuit. if a Grovers circuit has three iterations is there a way to store the distributions of each of the three iterations separately.

the following block of code gives you the final distribution only:

To run on local simulator:

Use the SatetvectorSampler from qiskit.primitives instead
sampler = Sampler(backend=backend)
sampler.options.default_shots = 10_000
result = sampler.run([circuit_isa]).result()
dist = result[0].data.meas.get_counts()
Shivansh20128 commented 1 week ago

Hi! I am assuming you are trying to work with the Grovers Algorithm code on the tutorials page in IBM. Your query is if we could get a distribution after every Grover operation we run. Let me answer this in two parts. If we want to obtain distributions, we need to make the measurements first. As you can see in the code as well, the distribution given has been prepared from the result after taking a measurement. In quantum mechanics, when we take a measurement, the quantum state of the physical system yields a numerical/classical result. Or in other words, in loses its quantum state. So if we try to take a measurement after every grover operation to get the distributions, we will get the wrong results because after every measurement, the quantum state changes to classical state, and loses all the information it had previously about the quantum state. If you still want to get distributions after every grover operation has been applied, you can prepare different quantum circuits starting with a single grover operation, then two, and so on and take measurements and plot distribution, just like you did here. So in the end, you will get a distribution for each circuit. I think it should give you a fair idea (if not exact) about the improvement after each grover operation has been applied. I think that answers your query. Feel free to ask if need to more information. Thank you

Pavanmk1310 commented 6 hours ago

Thank you for your insight. I had not previously considered that extracting information from a quantum system could violate the fundamental principle of measurement, potentially disturbing the quantum state and affecting the accuracy of any given observations

Shivansh20128 commented 1 hour ago

You're welcome :)

Shivansh20128 commented 1 hour ago

@Eric-Arellano I think we can close this issue now. Thanks