5
$\begingroup$

I'm studying an introduction course to quantum computation at my uni and we got homework to draw a certain circuit using qiskit. I managed to understand how to do what is asked from me, and this is the final answer:

Is there a way to force measurements at the end of the circuit?

Now I know that once the qubit is no longer used in a manipulation, you can measure it without affecting the results, however I find this circuit rather confusing and messy. Since we can always postpone our measurement to the end, I'd prefer a more organised drawing where all CNOT gates are performed, and only then the measurements - preferably from the top one to the bottom one. Is it possible to force such a thing?

To try and force the code doing that, I tried to first use a for loop to apply CNOTs on every qubit, then using a separate loop I performed the measurements on every qubit.

circuit_three = QuantumCircuit(5, 5)
circuit_three.h(0)
for i in range(1, 5):
    circuit_three.cx(0, i)
for i in range(0, 5):
    circuit_three.measure(i, i)
circuit_three.draw(output='mpl', filename='3')

The desired result is this (done with paint): Desired Outcome of circuit drawing

$\endgroup$

1 Answer 1

7
$\begingroup$

You can add a barrier before the measure:

from qiskit import *
circuit_three = QuantumCircuit(5, 5)
circuit_three.h(0)
for i in range(1, 5):
    circuit_three.cx(0, i)
circuit_three.barrier()  <--- add this line
for i in range(0, 5):    <--- side note: this can be replaced by circuit_three.measure_all()
    circuit_three.measure(i, i)
circuit_three.draw(output='mpl')

enter image description here

Then, with the argument plot_barriers, skip the plotting of the barrier:

circuit_three.draw(output='mpl', plot_barriers=False)

enter image description here

An important caveat: the barrier has meaning for the transpiler. If you are planning to execute that circuit, remember to remove the added barrier.

$\endgroup$
4
  • $\begingroup$ Super thanks, it worked as intended. I switched the measurement block to measure_all() as you suggested and it added a barrier between the CNOT gates and the measures automatically, even when i commented the .barrier() line. I didnt understand your comment about the barrier needed to be removed before executed. I read the post you sent and it seems like it is just an esthetic component that prevents the compiler from adding the gates together. I executed it and got the same count as before. So what exactly did you mean? $\endgroup$
    – Aqua-
    Commented Apr 1, 2021 at 13:45
  • 2
    $\begingroup$ @Aqua If you leave the qubit just hanging around in the circuit, decoherence will happen and your result will be less ideal. However, if you removed the barrier, the qubit will be measured immediately after all the gates on its wired has been executed, hence the extracted result will be more ideal. $\endgroup$
    – KAJ226
    Commented Apr 1, 2021 at 14:16
  • $\begingroup$ the @KAJ226 explanation hits the nail in the head! $\endgroup$
    – luciano
    Commented Apr 1, 2021 at 14:55
  • $\begingroup$ @KAJ226 Got it. make sense... Usually in uni when we deal with quantum states we use eigen states and assume nothing is there to interact with it. So what you say is that theoretically in an ideal system/circuit it should'nt matter where i perform the measurement, but then the system is never ideal so if we wait noise might add up on the qubit. Thanks did'nt think of it that way. $\endgroup$
    – Aqua-
    Commented Apr 2, 2021 at 8:37

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.