Environment
- qiskit.version: 0.22.0
- Python version: 3.8.0
- Operating system: Ubuntu 18.04
What is happening?
The instruction measure_all
in the file qrng.py masures the qubits by adding an extra ClassicalRegister
, making the circuit larger than it should be. At the same time, the existing ClassicalRegister
, variable c
, is not used at all.
How can we reproduce the issue?
Adding a print on the result measured in the circuit:
from qiskit import *
backend = Aer.get_backend('qasm_simulator')
q = QuantumRegister(48)
c = ClassicalRegister(48)
circuit = QuantumCircuit(q,c)
circuit.h(q)
for i in range(47):
circuit.cx(q[i], q[47])
circuit.measure_all()
import string
table = string.ascii_uppercase + string.ascii_lowercase + string.digits
def hash8():
hash_result = ''
result = execute(circuit, backend, shots=1).result()
count = result.get_counts(circuit)
print(count)
bits = max(count, key=lambda i: count[i])[:48]
start = 0
end = 6
while (end <= 48):
rand = int(bits[start:end], 2) % 63
start += 6
end += 6
hash_result += table[rand]
return hash_result
hash8()
Output:
{'101011111101100100111001000010110010010000111110 000000000000000000000000000000000000000000000000': 1}
Which shows how the program is running a program which is twice as large as it should be.
What should happen?
The circuit should be measuring the qubits directly, without adding an extra register.
Any suggestions?
Following the documentation of the measure_all
method, it is possible to measure the qubits directly, without adding an extra register. The code should be changed to:
circuit.measure_all(add_bits=False)
or with the measure
method:
This produces the following:
{'101011111101100100111001000010110010010000111110': 1}
As a consequence the slicing can be removed from the hash8
function.
# ORIGINAL
# bits = max(count, key=lambda i: count[i])[:48]
# NEW
bits = max(count, key=lambda i: count[i])