7
$\begingroup$

Why is the order reversed on measurement?

from qiskit import(
  QuantumCircuit,
  execute,
  Aer)
from qiskit.visualization import plot_histogram

# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(3, 3)

# Add a X gate on qubit 0
circuit.x(0)

# Add a CX (CNOT) gate on control qubit 0 and target qubit 1
circuit.cx(0, 1)

circuit.barrier()
# Map the quantum measurement to the classical bits
circuit.measure([0,1,2], [0,1,2])

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)

# Grab results from the job
result = job.result()

# Returns counts
counts = result.get_counts(circuit)
print("\nTotal count:",counts)

# Draw the circuit
circuit.draw()

Got result:

Total count for 00 and 11 are: {'011': 1000}

But I'm expecting '110'.

$\endgroup$
6
$\begingroup$

I still run into this issue too. If you consider $|q0\rangle$ to be the most significant bit (MSB) you have to map it to the most significant classical bit as well, which is in your case a bit no. 2. Or you can flip your quatnum circuit upside down and then $|q0\rangle$ become the least significant bit (LSB) and the measurement will meet your expectation.

A code

circuit.measure([0,1,2], [0,1,2])

is valid in case $|q0\rangle$ is LSB and

circuit.measure([0,1,2], [2,1,0])

in case $|q0\rangle$ is MSB.

I think that the reason for this arrangement is simply a convention, so you can choose whether $|q0\rangle$ is MSB or LSB and set the measurement procedure accordingly.

$\endgroup$
1
  • $\begingroup$ Just note, the same problem occurs in QASM. $\endgroup$ Nov 21 '19 at 22:40
0
$\begingroup$

Notice one thing that the index number of a qubit represents the significance of that bit. So, $|q_0\rangle$ is always the LSB and $|q_2\rangle$ will always be your MSB. As per your circuit, the output displayed by qiskit is correct. But, I think you were expecting a $|110\rangle$ based on the circuit that qiskit drew:qiskit circuit

Now, why qiskit draws the circuit in this way, I've no idea! But,from now on, whenever you draw a circuit use the code:

circuit.draw(reverse_bits='true')

This time you will get something like this: qiskit circuit 2

And, this would remove the confusion.

$\endgroup$
0
$\begingroup$

Analogy to numbers in binary format:

number 2 = b10 (2 bits)

number 6 = b110 (added 1 bit , and it is the highest bit)

If 6 is represented as an array: arr=[0,1,1], that is what we would expect but it is the opposite order as binary representation.

For qubits, the register is created the same way.

Mathematicians could explain nomenclature better but in bra-ket notation, for a 2 qubits system, you have {|00>,|01>,|10>,|11> } = {|0>,|1>,|2>,|3>}, for bigger systems you can say that solution is |13> instead of writing |00001101>. Since binary in computers are written the smallest bit in the 0 position, the output from the circuit has the same structure than the shorter, human-readable decimal version.

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