5

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

tripleee
  • 131
  • 5
Aqua-
  • 53
  • 3

1 Answers1

7

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.

tripleee
  • 131
  • 5
luciano
  • 6,084
  • 1
  • 13
  • 34