4
$\begingroup$

I wonder if I have to add classical bits while performing measurement for a single-qubit quantum circuit. The difference to initialize the circuit is QuantumCircuit(1) and QuantumCircuit(1,1). I noticed if I do not add the classical bit while drawing the circuit, I'm automatically assigned with a meas bit: enter image description here

Is that the same as a classical bit? Will there be any difference when I run the two versions of the circuit on the simulator and quantum device? Thanks!

$\endgroup$
2
  • $\begingroup$ Did you use the function measure_all() ? $\endgroup$
    – KAJ226
    Aug 6 at 5:51
  • $\begingroup$ @KAJ226 Yes, I did use that in QuantumCircuit(1):) $\endgroup$
    – ZR-
    Aug 6 at 5:55
5
$\begingroup$

You don't need to define classical bits in your QuantumCircuit if you are going to use the function measure_all().

The two scripts below (Script 1 and Script 2) are equivalent:

# Script 1
qc1 = QuantumCircuit(1)
qc1.h(0)
qc1.measure_all()
print(qc1)

        ┌───┐ ░ ┌─┐
   q_0: ┤ H ├─░─┤M├
        └───┘ ░ └╥┘
meas: 1/═════════╩═
                 0 
# Script 2
qc2 = QuantumCircuit(1,1)
qc2.h(0)
qc2.measure([0],[0])
print(qc2)
     ┌───┐┌─┐
q_0: ┤ H ├┤M├
     └───┘└╥┘
c: 1/══════╩═
           0 

You can check the result on qasm_simulator:

execute(qc1, Aer.get_backend('qasm_simulator'), shots = 1000).result().get_counts()
>>> {'0': 486, '1': 514}
execute(qc2, Aer.get_backend('qasm_simulator'), shots = 1000).result().get_counts()
>>> {'1': 468, '0': 532}

Thus, you don't need to define classical bits (cbits) in your QuantumCircuit if you are going to use measure_all() function.


However, you want to avoid doing something like:

# Script 3
qc3 = QuantumCircuit(1,1)
qc3.h(0)
qc3.measure_all() 
print(qc3)

        ┌───┐ ░ ┌─┐
   q_0: ┤ H ├─░─┤M├
        └───┘ ░ └╥┘
   c: 1/═════════╬═
                 ║ 
meas: 1/═════════╩═
                 0 

You still can run the circuit, it just give you an extra cbit of '0' in your readout dictionary. That is, you would see something like:

execute(qc3, Aer.get_backend('qasm_simulator'), shots = 1000).result().get_counts()
>>>  {'0 0': 496, '1 0': 504}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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