# Quantum Computing Qiskit - How to read data from classical registers

from qiskit import *

qr   = QuantumRegister(3)
cr   = ClassicalRegister(3)
circ = QuantumCircuit(qr,cr)

circ.h(qr[0])
circ.h(qr[1])
circ.h(qr[2])

circ.measure(qr,cr)



now cr[0] maybe 0 or 1

I want to know or copy its value to another normal variable to use it in my calculation, something like if(cr[0]==0) cr_val=0 else cr_val=1

• is cr_val a variable that will be used outside of qiskit, or will it be used to affect your circuit in some way? Jul 11 '19 at 17:51
• cr_val will be used to read the value of all cr cr[0],cr[1],cr[2] to calculate the decimal value of cr to print as decimal not as binary Jul 12 '19 at 4:29
• i want to copy the cr_val before the execute command, to store this value and reset the circuit and calculate again Jul 12 '19 at 8:25

### Wanting to calculate the decimal value of all cr as opposed to the binary

This can be done by using the Python built in function int(). This function will return the integer value of the input in base 10 (decimal). So you can retrieve the counts from the job by calling job.result().get_counts(<circuit_name>). This will return a dictionary with the keys being the output from the ClassicalRegisters and the values being the number of times that output was measured. If you take the keys from counts and input them into int() then it will return each output as a decimal value.

### Wanting to store values from cr before the execute command

To my knowledge, there is no way to read the values from cr before executing the circuit. The values are only populated into the ClassicalRegisters after measuring the circuit, which can only be done during the execution of the circuit.

You say you want to store the values from cr and reset the circuit and calculate again. This is possible, as you can just read the outputs you get from counts, then do something to the circuit based on those results, and then run the circuit again.

if you want to excute the whole quantum algorithm again and again bases on the previous result then use while loop.

I find two answers: (code tested and working)

from qiskit import *
qiskit.IBMQ.disable_accounts()
#backend = qiskit.IBMQ.get_backend('ibmq_16_melbourne')
backend = qiskit.IBMQ.get_backend('ibmq_qasm_simulator')

qr  = QuantumRegister  (4)# 1110
qq  = QuantumRegister  (4)# will set to 1110 dependin on cr read
cr  = ClassicalRegister(4)
cc  = QuantumCircuit(qr,qq,cr)

cc.x(qr[1]);cc.x(qr[2]);cc.x(qr[3])# 1110

#---the important code start:----------------------------------------
for i in range(4):
cc.measure(qr[i],cr[0])#read qr to set qq depending on the read of the classical register value using (if) condition
cc.reset(qq[i]) # qq[i] = 0
cc.x(qq[i]).c_if(cr,1) # if(cr == 1): qq[i] = 1 , test the whole cr decimal value, it can hold {0 to (2^n)-1}
#---the important code end--------------------------------------------

for i in range(4): cc.measure(qq[i],cr[i])#read qq

job=qiskit.execute(cc,backend,shots=1024)
result=job.result()
counts=result.get_counts(cc)
print(counts) # {'1110': 1024}


--- execute result ---

{'1110': 1024}


.

.

from qiskit import *
qiskit.IBMQ.disable_accounts()
#backend = qiskit.IBMQ.get_backend('ibmq_16_melbourne')
backend = qiskit.IBMQ.get_backend('ibmq_qasm_simulator')

qr  = QuantumRegister  (4)# 1110
cr  = ClassicalRegister(4)
cc  = QuantumCircuit(qr,cr)

cc.x(qr[1]);cc.x(qr[2]);cc.x(qr[3])# 1110

job=qiskit.execute(cc,backend,shots=1024)
result=job.result()
counts=result.get_counts(cc)
print(counts) # {'1110': 1024}

#---the important code start:----------------------------------------
lAnswer = [(k[::-1],v) for k,v in counts.items()]
lAnswer.sort(key = lambda x: x[1], reverse=True)
Y = []
for k, v in lAnswer: Y.append( [ int(c) for c in k ] )
#---the important code end--------------------------------------------

print(Y) # [[0, 1, 1, 1]]

print(Y[0][0]) # 0
print(Y[0][1]) # 1
print(Y[0][2]) # 1
print(Y[0][3]) # 1


--- execute result ---

{'1110': 1024}
[[0, 1, 1, 1]]
0
1
1
1