I find two answers: (code tested and working)
- Answer (1) (read the register value before the execute command)
from qiskit import *
qiskit.IBMQ.disable_accounts()
IBMQ.enable_account('change to your api','change to your url')
#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}
.
.
- Answer (2) (read the register value after the execute command)
from qiskit import *
qiskit.IBMQ.disable_accounts()
IBMQ.enable_account('change to your api','change to your url')
#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
for i in range(4): cc.measure(qr[i],cr[i])#read
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
cr_val
a variable that will be used outside of qiskit, or will it be used to affect your circuit in some way? $\endgroup$cr_val
will be used to read the value of all crcr[0],cr[1],cr[2]
to calculate the decimal value of cr to print as decimal not as binary $\endgroup$