1
$\begingroup$

Qiskit has qiskit.circuit.QuantumCircuit.measure_all() and qiskit.circuit.QuantumCircuit.measure(). Why would you use one over another?

$\endgroup$
1
  • $\begingroup$ As you can see from names of the functions, once you want to measure all qubits you use measure_all to save up code lines. The measure function can be applied to particular qubits if you are interested in values of several of them and not all of them. $\endgroup$ Dec 13, 2022 at 7:19

3 Answers 3

2
$\begingroup$

There are three methods to measure qubits in a Qiskit circuit object: measure, measure_all, measure_active. The latter two are wrappers on the first one, so let's start with the most basic one measure:

  • QuantumCircuit.measure: It will measure each qubit in the first argument into the classical bit given as a second argument.
from qiskit import QuantumCircuit

circuit = QuantumCircuit(2, 2)  # A circuit with 2 qubits and 2 classical bits
circuit.measure(1, 0)           # Measures qubit 1 and puts the result in bit 0
circuit.draw(cregbundle=False)
q_0: ───
     ┌─┐
q_1: ┤M├
     └╥┘
c_0: ═╩═
        
c_1: ═══

This method is very flexible and allows you to fully control where the result of a measurement is stored.

  • QuantumCircuit.measure_all: Gets no argument and can be applied to a circuit that might not have predefined classical bits. It will create the classical wires and store the result of measurement in order. It will also introduce a barrier before the measurement.
circuit = QuantumCircuit(2)
circuit.measure_all() 
circuit.draw(cregbundle=False)
         ░ ┌─┐   
   q_0: ─░─┤M├───
         ░ └╥┘┌─┐
   q_1: ─░──╫─┤M├
         ░  ║ └╥┘
meas_0: ════╩══╬═
               ║ 
meas_1: ═══════╩═

The method measure_all covers the most common scenario as a convenience function.

circuit = QuantumCircuit(2)
circuit.h(1)
circuit.measure_active() 
circuit.draw(cregbundle=False)
    q_0: ──────░────
         ┌───┐ ░ ┌─┐
    q_1: ┤ H ├─░─┤M├
         └───┘ ░ └╥┘
measure: ═════════╩═

In this example, only the qubit 1 has an operation (H in this case) and it is the only that is being measured.

$\endgroup$
2
$\begingroup$

As an example, suppose you are given the following quantum circuit, preparing and measuring the 3-qubits GHZ state:

$$ |\psi\rangle = \frac{1}{\sqrt{2}}\left(|000\rangle + |111\rangle\right) $$

from qiskit import QuantumCircuit

qc = QuantumCircuit(3, 3)
qc.h(0)
qc.cx(0, 1)
qc.cx(0, 2)

qc.barrier()
qc.measure([0,1,2], [0,1,2])

The measure_all method can be used to build exactly the same circuit as follows:

from qiskit import QuantumCircuit

qc = QuantumCircuit(3)
qc.h(0)
qc.cx(0, 1)
qc.cx(0, 2)
    
qc.measure_all()

Notice that, in this case, when you create the QuantumCircuit object you don't have to specify how many classical bits you want to add in your circuit since the measure_all method will create them for you automatically. Moreover, it also adds a barrier just before the measurements.

$\endgroup$
0
$\begingroup$

It depends on what qubits you want to measure.

For example, if the circuit is a Bell State you only need to measure one of the qubits, and later if needed, make some more "code".

Furthermore, note that we have much errors in quantum computers.

Fewer gates -> Less time execution -> Less errors -> Better solution.

$\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.