# Qiskit - expand and/or merge registers

Suppose one of my functions create some ancillary qubits and reset them. Then, another function wants to create a larger number of ancillary qubits. So, I'd like to reuse the first set of qubits and add the difference two them. Is there any efficient way to achieve the goal?

def function1(qc):
r = 3
ancillas = QuantumRegister(r, 'ancillas')
# use the ancillas
return ancillas

def function2(qc, partial_ancillas):
r = 5
diff = r - len(partial_ancillas)
if diff > 0:
# something like ancillas = partial_ancillas.add(diff)
# or maybe ancillas = partial_ancillas + QuantumRegister(diff)
else:
ancillas = partial_ancillas
return ancillas

qc = QuantumCircuit()
anc = function1(qc)
anc = function2(qc, anc)

• There are ways to do this, as the answer explains, but they aren't so elegant. Since people, like you, want to be able to do this in a clean way, this functionality is in development and will be released soon.' – James Wootton Dec 23 '18 at 10:10

I don't think you can add qubits in a QuantumRegister already set. If your goal however is to create another ancilla registers that cannot exceed a limit of qubits, what you can do is :

def create_ancillas(r=3):

ancillas = QuantumRegister(r, 'ancillas')
return ancillas

def create_ancillas_limited(partial_ancillas,r=5):

diff = r - partial_ancillas.size
if diff > 0:
return QuantumRegister(diff, 'ancillas_2')
else:
return None

qc = QuantumCircuit()
anc1 = create_ancillas()

anc2 = create_ancillas_limited(anc1)


Maybe better if you want to limit the number of qubits in your circuits when adding ancillas is :

def add_ancillas_limited(qc,n_ancillas_to_add=3,limit=5):
if qc.width() - n_ancillas_to_add > limit:
print("Cannot exceed limit!")
else: