# How to initialize classical register in Qiskit?

I'm working on a Hybrid classical-quantum linear solver. For this, they make a loop on a quantum circuit (ie. below), and each time they change the value of the classical register and apply a X gate on the quantum register conditioned on the state of the classical register.

Therefore, I ask myself, is there a way to initialize the classical register ?

• You cant make classical bits as a controller and make qubits as a target. But you can use if conditional statement to apply x gate on the qubits.
– Aman
Jul 22, 2019 at 18:12

## 3 Answers

Classical registers are typically used for capturing measurement results, and may also be used for conditionally applying quantum operation. See: https://github.com/Qiskit/openqasm/blob/master/spec/qasm2.rst

Given the problem you described, one approach would be to have a classical program that iteratively: 1) defines and executes a quantum circuit on a quantum processor or simulator 2) reads the results from the classical registers for guidance in defining the circuit for the next iteration

You might find this section of the documentation helpful, it goes over initializing and using registers.

https://qiskit.org/documentation/terra/quantum_circuits.html

• I already read it, but they don't talk about classical register initialization. Jul 23, 2019 at 8:27
• What about this post: quantumcomputing.stackexchange.com/questions/1413/…
– mico
Jul 23, 2019 at 12:25
• In this post, they initialize the quantum register not the classical register. Jul 24, 2019 at 15:03
• Why dont u initialize qubits and measure them by the classical registers and finally reset the qubits.
– Aman
Jul 24, 2019 at 16:50

Here comes an illustration code written by myself.

from qiskit import QuantumRegister,ClassicalRegister,QuantumCircuit,Aer,execute
from qiskit.providers.aer import QasmSimulator
qr=QuantumRegister(1)
cr0=ClassicalRegister(5)
circ=QuantumCircuit(qr,cr0)
circ.measure(qr,cr0[0])
circ.x(qr)
circ.measure(qr,cr0[2])
circ.measure(qr,cr0[3])
circ.x(qr)
circ.measure(qr,cr0[3])
circ.x(qr)
circ.measure(qr,cr0[4])
circ.h(qr)
circ.x(qr)
print(execute(circ,Aer.get_backend('qasm_simulator')).result().get_counts())


The upper code shows how to get varied values and assign them to different classical registers. Although this method still requires a qubit, at least it can save some time. Maybe you can use the partial trace method to improve this. I think you must be ok with how the c_if() function works.

• Haven't noticed that this is a rather old question, I hope this answer is now not that helpful for you. Nov 1, 2020 at 7:26